XSL: inserting a new element below a sibling

Here should go questions about transforming XML with XSLT and FOP.
cbridle
Posts: 2
Joined: Mon Apr 04, 2005 3:52 pm

XSL: inserting a new element below a sibling

Post by cbridle »

Hi,

I need to be able to insert a new element which will be a sibling an an existing element.

The xml is too large to post but i will provide an exampole for this purpose. The XML structuire would be simular to this:

<task>
<temp>
</temp>
<trial>
</trial>
<floppy>
</floppy>
<spain>
</spain>
</task>

what i need to be able to do is add a new element below the floppy element and above the spain element.

The xsl i have only inserts it immediately below the <task> element.

Thanks for any help.
cbridle
Posts: 2
Joined: Mon Apr 04, 2005 3:52 pm

Post by cbridle »

for some reason the formatting has been affected, the <task> element would be the top level, and the other elements would be children of the <task> element, i'll try again, like this:

<task>
<temp>
</temp>
<trial>
</trial>
<floppy>
</floppy>
<spain>
</spain>
</task>
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi,

Try something like below:

Code: Select all


<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="spain">
<newElement/>
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Regards,
George
Post Reply