Page 1 of 1

insert a new element into a right position

Posted: Thu Sep 16, 2004 5:47 pm
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

Posted: Thu Sep 16, 2004 7:00 pm
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

Posted: Thu Sep 16, 2004 7:31 pm
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