The following stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template> <xsl:template match="varList[following-sibling::*[1][self::simpleSteps]]" /> <xsl:template match="simpleSteps"> <xsl:copy> <xsl:apply-templates select="@*" /> <xsl:copy-of select="preceding-sibling::varList[1]" /> <xsl:apply-templates select="node()" /> </xsl:copy> </xsl:template> </xsl:stylesheet>
At this input:
<executionPlan name="Test"> <paramList> <param name="param1" default="" /> </paramList> <varList> <var name="bla" default=":[param1]" /> </varList> <varList> <var name="bla2" default=":[param2]" /> </varList> <simpleSteps limitToHostSet="bla"> <execNative> <exec cmd="/bin/sh" /> </execNative> </simpleSteps> </executionPlan>
It produces:
<executionPlan name="Test"> <paramList> <param name="param1" default="" /> </paramList> <varList> <var name="bla" default=":[param1]" /> </varList> <simpleSteps limitToHostSet="bla"> <varList> <var name="bla2" default=":[param2]" /> </varList> <execNative> <exec cmd="/bin/sh" /> </execNative> </simpleSteps> </executionPlan>
Edit: Only the previous varList moved to its associated simpleSteps . All other varList elements varList copied unchanged.
It is unexpectedly not clear to me whether this is the desired behavior, or there may be several varList elements already inside the simpleSteps element that should not be changed. See my original solution for this case:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template> <xsl:template match="varList" /> <xsl:template match="simpleSteps"> <xsl:copy> <xsl:apply-templates select="@*" /> <xsl:copy-of select="../varList" /> <xsl:apply-templates select="node()" /> </xsl:copy> </xsl:template> </xsl:stylesheet>
At this input:
<executionPlan name="Test"> <paramList> <param name="param1" default="" /> </paramList> <varList> <var name="bla" default=":[param1]" /> </varList> <simpleSteps limitToHostSet="bla"> <varList> <var name="bla7" default=":[param7]" /> </varList> <execNative> <exec cmd="/bin/sh" /> </execNative> </simpleSteps> </executionPlan>
It produces:
<executionPlan name="Test"> <paramList> <param name="param1" default="" /> </paramList> <simpleSteps limitToHostSet="bla"> <varList> <var name="bla" default=":[param1]" /> </varList> <varList> <var name="bla7" default=":[param7]" /> </varList> <execNative> <exec cmd="/bin/sh" /> </execNative> </simpleSteps> </executionPlan>
source share