insert a new element into a right position

Are you missing a feature? Request its implementation here.
Guest

insert a new element into a right position

Post by Guest »

hi,
I have written a xsl to transform a xml to another xml.
The xsl insert a new element if it isn't present in a node.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="">
<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="c:Insinuazione xmlns:c="http://www.giustizia.it/Concorsuali/StatoPassivo">
<xsl:apply-templates select="c:Insinuazione/c:Identificativo"/>
<xsl:copy>
<xsl:if test="count(c:NumeroInsinuazione)=0">
<xsl:element name="NumeroInsinuazione"/>
</xsl:if>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:stylesheet>

The element inserted is "NumeroInsinuazione", but the xsl inserts it as first element of the node and I want to insert it as second (later the first element already present into the node).

Example:
Now I get:
...
<Insinuazione>
<NumeroInsinuazione/>
<Indentificativo/>
...
</Insinuazione>


I want get:
...
<Insinuazione>
<Indentificativo/>
<NumeroInsinuazione/>
...
</Insinuazione>

is it possible do it?

thanks
ciccio
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi Ciccio,

You are still fighting the same problem :).

Here it is:

Code: Select all


<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="">
<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="c:Insinuazione" xmlns:c="http://www.giustizia.it/Concorsuali/StatoPassivo">
<xsl:copy>
<!--Copy the attributes -->
<xsl:apply-templates select="@*"/>
<!-- Copy the first element -->
<xsl:apply-templates select="c:Identificativo"/>
<!-- Generate c:NumeroInsinuazione if necessary-->
<xsl:if test="count(c:NumeroInsinuazione)=0">
<xsl:element name="c:NumeroInsinuazione"/>
</xsl:if>
<!-- Copy everything except the first element c:Identificativo -->
<xsl:apply-templates select="node()[not(self::c:Identificativo)]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Best Regards,
George
meciccio
Posts: 6
Joined: Wed Aug 11, 2004 6:00 pm

Post by meciccio »

yes, I'm fighting the same problem yet, but I'm close to resolve it definitively.
I mean, this last post i think should be the last to transform the xml as I want.

When I wrote "I'm close to resolve" of course you know you have done most of work.

thank
ciccio
Post Reply