How to insert found element into preceding element?
Posted: Thu Oct 27, 2016 6:22 pm
I have a fairly flat XML input file. Some elements have to be nested based on criterion if found.
My input file:
My expected output file:
My XSLT thus far:
I cannot seem to find the correct method to nest elements to match my expected output. The nested IF statement in above example did not work. I thought of using some check for preceding-sibling.
I appreciate any assistance in solving this transform.
Thanks,
David Coe
My input file:
Code: Select all
<document>
<proc-title>Process Title 1</proc-title>
<note>Process NOTE</note>
<trim.para>Process note text.</trim.para>
<stepl1>Step 1</stepl1>
<stepl1>Step 2</stepl1>
<stepl2>Step 2a</stepl2>
<stepl2>Step 2b</stepl2>
<stepl3>Step 2b-1</stepl3>
<stepl3>Step 2b-2</stepl3>
<stepl1>Step 3</stepl1>
<note>Step 3 NOTE</note>
<trim.para>Step 3 note text.</trim.para>
<stepl1>Step 4</stepl1>
<stepl1>Step 5</stepl1>
</document>
Code: Select all
<proc>
<title>Process Title 1</title>
<note><trim.para>Process note text.</trim.para></note>
<stepl1><para>Step 1</para></stepl1>
<stepl1><para>Step 2</para>
<stepl2><para>Step 2a</para></stepl2>
<stepl2><para>Step 2b</para>
<stepl3><para>Step 2b-1</para></stepl3>
<stepl3><para>Step 2b-2</para></stepl3>
</stepl2>
<stepl1><specpara><para>Step 3</para>
<note><trim.para>Step 3 note text.</trim.para></note></specpara>
</stepl1>
<stepl1><para>Step 4</para></stepl1>
<stepl1><para>Step 5</para></stepl1>
</proc>
Code: Select all
<xsl:template name="proc">
<proc>
<title>
<xsl:value-of select="."/>
</title>
<xsl:if test="following-sibling::stepl1">
<xsl:for-each select="following-sibling::stepl1">
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:for-each>
<xsl:if test="following-sibling::stepl2">
<xsl:for-each select="following-sibling::stepl2">
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:for-each>
</xsl:if>
...
</xsl:if>
<xsl:for-each select="following-sibling::trim.para">
<note>
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
</note>
</xsl:for-each>
</proc>
</xsl:template>
I appreciate any assistance in solving this transform.
Thanks,
David Coe