if and count for xsl

Having trouble installing Oxygen? Got a bug to report? Post it all here.
Guest

if and count for xsl

Post by Guest »

hi to everyone,
i have got this xml:




<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v2004 rel. 3 U (http://www.xmlspy.com) by MA (GE) -->
<!--Sample XML file generated by XMLSPY v2004 rel. 3 U (http://www.xmlspy.com)-->
<n:DepositoInsinuazioni xmlns:n="http://www.giustizia.it/Concorsuali/StatoPassivo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.giustizia.it/Concorsuali/StatoPassivo
D:\MINISTERO\Krakatoa\src\interop\xml\Insinuazioni\InsinuaETipi.xsd">
<n:Procedura n:Tribunale="0370060094" n:RuoloGenerale="215" n:Anno="2004" n:Sentenza="68" n:DataSentenza="2004-03-10"/>
<n:Insinuazione>
<n:Identificativo n:CodiceFiscale="VRDMRA59A25E379B"/>
<n:Mandato>
<n:Mandatario n:CodiceFiscale="01234567890"/>
<n:NumeroMandato>100</n:NumeroMandato>
<n:DataMandato>2004-04-02</n:DataMandato>
</n:Mandato>
</n:Insinuazione>
<n:Insinuazione>
<n:Identificativo n:CodiceFiscale="VRDMRA59A25E379B"/>
<n:NumeroInsinuazione/>
<n:Mandato>
<n:Mandatario n:CodiceFiscale="01234567890"/>
<n:NumeroMandato>100</n:NumeroMandato>
<n:DataMandato>2004-04-02</n:DataMandato>
</n:Mandato>
</n:Insinuazione>
</n:DepositoInsinuazioni>

I want insert a new element <n:NumeroInsinuazione/> like an ancestor of <n:Insinuazione> where this new element is not present already.

Whit the xsl below insert a new element into each node <n:Insinuazione>.

<?xml version="1.0"?>
<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="n:Insinuazione/n:Identificativo" xmlns:n="http://www.giustizia.it/Concorsuali/StatoPassivo">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>

<xsl:element name="n:NumeroInsinuazione"/>

</xsl:template>
</xsl:stylesheet>

I tryed to modify this xsl adding a if (see below), but this dosen't work, becouse I think that the xpath into the if condition isn't correct.
How can insert the element <n:NumeroInsinuazione/> jast where I want?

<?xml version="1.0"?>
<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="n:Insinuazione/n:Identificativo" xmlns:n="http://www.giustizia.it/Concorsuali/StatoPassivo">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
<xsl:if test="count(/n:NumeroInsinuazione)=0">
<xsl:element name="n:NumeroInsinuazione"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>


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

Post by george »

Hi,

I'm not sure I understand your question exactly, anyway if you want to add the n:NumeroInsinuazione element inside n:Insinuazione if that is not present already then the following stylesheet should do it.

Code: Select all


<?xml version="1.0"?>
<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="n:Insinuazione" xmlns:n="http://www.giustizia.it/Concorsuali/StatoPassivo">
<xsl:copy>
<xsl:if test="count(n:NumeroInsinuazione)=0">
<xsl:element name="n:NumeroInsinuazione"/>
</xsl:if>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Best Regards,
George
meciccio
Posts: 6
Joined: Wed Aug 11, 2004 6:00 pm

Post by meciccio »

george you are my angel caretaker.

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

Post by george »

A small update: if you have attributes on the n:Insinuazione element then the stylsheet should be changed as follows to copy first the attributes then check and add the n:NumeroInsinuazione element and then copy the rest of the n:Insinuazione element content:

Code: Select all



<?xml version="1.0"?>
<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="n:Insinuazione" xmlns:n="http://www.giustizia.it/Concorsuali/StatoPassivo">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:if test="count(n:NumeroInsinuazione)=0">
<xsl:element name="n:NumeroInsinuazione"/>
</xsl:if>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Best Regards,
George
Post Reply