2011-03-07 49 views
3

请在下面找到我的xform页面。如何向所有子节点插入属性

<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" 
    xmlns:exforms="http://www.exforms.org/exf/1-0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <xhtml:head> 
     <xforms:instance id="instanceData"> 
      <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
       <fruits> 
        <fruit> 
         <fruit-name>Mango</fruit-name> 
        </fruit> 
        <fruit> 
         <fruit-name>Apple</fruit-name> 
        </fruit> 
        <fruit> 
         <fruit-name>Banana</fruit-name> 
        </fruit> 
       </fruits> 
      </form> 
     </xforms:instance> 
    </xhtml:head> 
</xhtml:html> 

我想插入一个属性味道=“好”是所有水果名称标签,如下

<fruit-name taste="good"> 

我尝试以下方法来达到相同的,但它总是插入属性只有第一个水果名。

<xforms:insert ev:event="xforms-model-construct-done" 
    context="instance('instanceData')/fruits/fruit/fruit-name" 
    origin="xxforms:attribute('taste','good')" /> 

<xforms:insert ev:event="xforms-model-construct-done" 
    context="instance('instanceData')/fruits/fruit[position() &gt; 0]/fruit-name" 
    origin="xxforms:attribute('taste','good')" /> 

请建议一种方法将此属性插入镜头中的所有水果名称节点。 由于水果列表是动态的,我们需要有一个动态的解决方案。

+0

问得好,+1。查看我的答案,获得完整,简短且容易的XSLT解决方案。 :) – 2011-03-08 04:46:15

回答

0

我不知道的XForms,但这个任务是XSLT非常简单:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="fruit-name"> 
    <fruit-name taste="good"> 
    <xsl:apply-templates select="node()|@*"/> 
    </fruit-name> 
</xsl:template> 
</xsl:stylesheet> 

当这种转变是在所提供的XML文档应用:

<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xforms="http://orbeon.org/oxf/xml/xforms" 
    xmlns:exforms="http://www.exforms.org/exf/1-0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <xhtml:head> 
     <xforms:instance id="instanceData"> 
      <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
       <fruits> 
        <fruit> 
         <fruit-name>Mango</fruit-name> 
        </fruit> 
        <fruit> 
         <fruit-name>Apple</fruit-name> 
        </fruit> 
        <fruit> 
         <fruit-name>Banana</fruit-name> 
        </fruit> 
       </fruits> 
      </form> 
     </xforms:instance> 
    </xhtml:head> 
</xhtml:html> 

在想,正确的结果产生:

<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:xforms="http://orbeon.org/oxf/xml/xforms" 
xmlns:exforms="http://www.exforms.org/exf/1-0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <xhtml:head> 
    <xforms:instance id="instanceData"> 
     <form xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
     <fruits> 
      <fruit> 
      <fruit-name taste="good">Mango</fruit-name> 
      </fruit> 
      <fruit> 
      <fruit-name taste="good">Apple</fruit-name> 
      </fruit> 
      <fruit> 
      <fruit-name taste="good">Banana</fruit-name> 
      </fruit> 
     </fruits> 
     </form> 
    </xforms:instance> 
    </xhtml:head> 
</xhtml:html> 
0

xxforms:iterateextension attribute这里是你的朋友。以下将做的伎俩。在这种情况下,它比XSLT更简单;)。

<xforms:insert ev:event="xforms-model-construct-done" 
       xxforms:iterate="fruits/fruit/fruit-name" 
       context="." origin="xxforms:attribute('taste','good')"/> 
相关问题