3

Please find below my xform page.

<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>

I would like to insert a attribute taste="good" to all fruit-name tags as below

<fruit-name taste="good">

I tried the following ways to achieve the same but it always inserts the attribute to first fruit-name only.

<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')" />

Please suggest a way to insert this attribute to all fruit-name nodes at shot. Since the fruits list dynamic, we need to have a dynamic solution for it.

4

2 回答 2

0

xxforms:iterate 扩展属性是你的朋友。以下将解决问题。在这种情况下,它甚至比 XSLT 更简单 ;)。

<xforms:insert ev:event="xforms-model-construct-done"
               xxforms:iterate="fruits/fruit/fruit-name"
               context="." origin="xxforms:attribute('taste','good')"/>
于 2011-03-08T07:00:28.937 回答
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>
于 2011-03-08T04:45:37.770 回答