How to insert an attribute in all child nodes

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 add the taste = "good" attribute to all fruit name names as shown below

<fruit-name taste="good">

I tried the following ways to achieve the same, but it always inserts the attribute for the first name of the fruit 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 in all fruit name nodes when shooting. Since the list of fruits is dynamic, we need to have a dynamic solution for it.

+3
source share
2 answers

I do not know XForms, but this task is very simple with 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>

when this conversion is applied to the provided XML document:

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

the desired, correct result is output:

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

All Articles