Adding xml nodes at a specific point using xslt

I have the following xml and you want to insert additional xml into it:

<root>
    <steps>
        <step name="step1" type="process">
            <steps>
                <step name="substep1">
                </step>
            </steps>
        </step>
        <step name="step2" type="process">
            <steps>
                <step name="substep1">
                <!-- more substeps...-->
                </step>
            </steps>
        </step>
        <step name="step3" type="process">
            <steps>
                <step name="substep1">
                </step>
                <step name="substep2">
                </step>
                <!-- more substeps...-->
            </steps>
        </step>
        <!-- THE BELOW IS WHAT I WISH TO ADD... and it has to be here -->
        <step name="reference">
            <!-- These stuff have been hardcoded in my xsl so its fine -->
        </step>
        <!-- ends -->
    </steps>
    <references>
        <reference name="reference1">
        </reference>
        .
        .
        .
    </references>
</root>

As I wrote in the xml example, I want to add an extra step element as the last step in most outter steps. I have an xml fragment already hardcoded in my xsl, so all I have to do is find the best logic to go to that specific point in the xml tree so that I can call the template and add this snipet.

What is the recommended / best approach for this?

Thank.

+3
source share
1 answer

This conversion is :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 exclude-result-prefixes="xsl">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pAddition">
   <step name="reference">
     <XXX/>
   </step>
 </xsl:param>

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

 <xsl:template match="/*/steps/step[position()=last()]">
  <xsl:call-template name="identity"/>
  <xsl:copy-of select="$pAddition"/>
 </xsl:template>
</xsl:stylesheet>

when applied to this XML document :

<root>
    <steps>
        <step name="step1" type="process">
            <steps>
                <step name="substep1">
                </step>
            </steps>
        </step>
        <step name="step2" type="process">
            <steps>
                <step name="substep1">
                <!-- more substeps...-->
                </step>
            </steps>
        </step>
        <step name="step3" type="process">
            <steps>
                <step name="substep1">
                </step>
                <step name="substep2">
                </step>
                <!-- more substeps...-->
            </steps>
        </step>
    </steps>
    <references>
        <reference name="reference1">
        </reference>
        .
        .
        .
    </references>
</root>

, :

<root>
   <steps>
      <step name="step1" type="process">
         <steps>
            <step name="substep1"/>
         </steps>
      </step>
      <step name="step2" type="process">
         <steps>
            <step name="substep1"><!-- more substeps...--></step>
         </steps>
      </step>
      <step name="step3" type="process">
         <steps>
            <step name="substep1"/>
            <step name="substep2"/><!-- more substeps...-->
         </steps>
      </step>
      <step name="reference">
         <XXX/>
      </step>
   </steps>
   <references>
      <reference name="reference1"/>
        .
        .
        .
    </references>
</root>

:

  • ** .

  • XML- ( ) xsl:param. XML xslt document().

  • , , node, . xsl:param, , , .

+6

Source: https://habr.com/ru/post/1759448/


All Articles