如何向所有子节点插入属性

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>

我希望将属性taste="good"插入到所有如下所示的水果名称标签中:
<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')" />

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

好问题,+1。请查看我的答案,其中包含完整、简短和易于理解的XSLT解决方案。 :) - Dimitre Novatchev
2个回答

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:iterate扩展属性在这里是你的好朋友。以下代码可以解决问题。而且在这种情况下,它甚至比XSLT更简单 ;)

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接