2011-03-07 53 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')"/> 
相關問題