Insert an element at a specific point in the output document during XSLT conversion

I am wondering if you can access the resulting document during processing.

The reason I ask is because I am converting the input document and want to insert elements depending on some conditions, but this should have happened when I crossed the tree and I am almost ready to create it.

The converted xml looks something like this:

<xform>
    <xforms>
        <model>
            <instance>
                <data />
                <data />
            </instance>
        </model>
        <bind />
        <bind />
        <bind />
    </xforms>
</xform>

I assume that during the conversion (before the above xml has been serialized), to access the tag <instance>and insert additional elements <data>.

Note The input document is different from the above xml - the above xml is what the conversion should do.

, <xform> <bind>.

, (, 2 2 ):

<xform>
    <xforms>
        <model>
            <instance>
                <data />
                <data />
                <data>new data node</data>
                <data>second new data node</data>
            </instance>
        </model>
        <bind />
        <bind />
        <bind />
        <bind>new bind node</bind>
        <bind>second new bind node</bind>
    </xforms>
</xform>

.

+3
2

, , , , . , , ,

<xsl:template match="/">
  <xsl:result-document href="example.xml">
<xform>
 <xforms>
  <model>
   <instance>
    <data>
    </data>
   </instance>
  </model>
  <bind />
  <bind />
  <bind />
 </xforms>
</xform>
  </xsl:result-document>
</xsl:template>

, , , ,

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

<xsl:template match="/">
  <xsl:variable name="temp1">
<xform>
 <xforms>
  <model>
   <instance>
    <data>
    </data>
   </instance>
  </model>
  <bind />
  <bind />
  <bind />
 </xforms>
</xform>
</xsl:variable>
  <xsl:result-document href="example.xml">
    <xsl:apply-templates select="$temp1/*"/>
  </xsl:result-document>
</xsl:template>

<xsl:template match="instance">
  <xsl:copy>
    <xsl:apply-templates/>
    <data>...</data>
  </xsl:copy>
</xsl:template>

, , .

+3

, :

<xsl:stylesheet version="2.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()|@*" mode="#default pass2">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*" mode="#current"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="/">
  <xsl:variable name="vPass1">
    <xsl:apply-templates/>
  </xsl:variable>

  <xsl:apply-templates select="$vPass1/node()" mode="pass2"/>
 </xsl:template>

 <xsl:template match="instance" mode="pass2">
  <instance>
    <xsl:apply-templates mode="pass2"/>
    <data>2</data>
    <data>3</data>
  </instance>
 </xsl:template>

 <xsl:template match="model" mode="pass2">
  <model>
   <xsl:apply-templates mode="pass2"/>
   <bind>1</bind>
   <bind>2</bind>
   <bind>3</bind>
  </model>
 </xsl:template>
</xsl:stylesheet>

, XML-:

<xform>
    <xforms>
        <model>
            <instance>
                <data>
                </data>
            </instance>
        </model>
        <bind />
        <bind />
        <bind />
    </xforms>
</xform>

, , $vPass1. $vPass1 data instance bind model, :

<xform>
   <xforms>
      <model>
         <instance>
            <data/>
            <data>2</data>
            <data>3</data>
         </instance>
         <bind>1</bind>
         <bind>2</bind>
         <bind>3</bind>
      </model>
      <bind/>
      <bind/>
      <bind/>
   </xforms>
</xform>
+1

All Articles