Reputation: 205
I am wondering if anyone can help me with a problem I am having.
I am using XSLT version 1 to transform some source XML, part of which looks like this:
<tr parent="ID0E4B" zylevel="3" type="categoryhead">
<td colname="1">Utilities 1.61%</td>
<td colname="2">1.61</td>
<td colname="3">300,000</td>
</tr>
<tr parent="ID0EOB" zylevel="2" type="categorytotal" >
<td colname="1">Total </td>
<td colname="2"/>
<td colname="3">17,567,240</td>
</tr>
I would like to be able to create a tr node similar to the tr node in the source xml and insert it between the two tr nodes in the above example. The node to insert should like this:
<tr parent="ID0EGWAE" zylevel="4" type="detail">
<td colname="1">Other securities</td>
<td colname="2">1.61</td>
<td colname="3">335,207</td>
</tr>
Right now the XSLT processor is about to print the second tr node in the topmost example. The desired output should look like this:
<tr parent="ID0E4B" zylevel="3" type="categoryhead">
<td colname="1">Utilities 1.61%</td>
<td colname="2">1.61</td>
<td colname="3">300,000</td>
</tr>
<tr parent="ID0EGWAE" zylevel="4" type="detail">
<td colname="1">Other securities</td>
<td colname="2">1.61</td>
<td colname="3">335,207</td>
</tr>
<tr parent="ID0EOB" zylevel="2" type="categorytotal" >
<td colname="1">Total </td>
<td colname="2"/>
<td colname="3">17,567,240</td>
</tr>
Any help that anyone has would be much appreciate it. I have to admit to being clueless on this one.
Upvotes: 0
Views: 75
Reputation: 1278
Try this:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()"><!-- identity template -->
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:template>
<xsl:template match="tr">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
<xsl:if test="position()=1"><!-- Inserting given content after the first tr -->
<tr parent="ID0EGWAE" zylevel="4" type="detail">
<td colname="1">Other securities</td>
<td colname="2">1.61</td>
<td colname="3">335,207</td>
</tr>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1