[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Modify XML using XSL


Subject: Re: [xsl] Modify XML using XSL
From: "Santanu Choudhury" <santanuc@xxxxxxxxxx>
Date: Thu, 13 Apr 2006 14:33:48 +0530

Hi ,

i am using following XSL everyting is working out well except point 3 , delete.

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>

<!-- copy everything that has no other pattern defined -->

<xsl:template match="*">

<xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>

</xsl:template>

<!-- Check count for title under nobel node , if less than 2 add one more node -->

<xsl:template match="/books/nobel[count(/title) &lt; 2]">

<xsl:copy>

<xsl:copy-of select="@*"/>

<xsl:apply-templates/>

<title>"The War"</title>

</xsl:copy>

</xsl:template>


<!-- Check count for title under comics node , if greater than 1 delete last node -->


<xsl:template match="/books/comics[count(/title) &gt; 1]">

<xsl:copy>

<xsl:copy-of select="@*"/>

<xsl:apply-templates select="/title[position() != last()]"/>

</xsl:copy>

</xsl:template>

</xsl:stylesheet>

Any help will be highly appreciated.



Thanks,

-Santanu.



----- Original Message ----- From: "Santanu Choudhury" <santanuc@xxxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Thursday, April 13, 2006 1:14 PM
Subject: Re: [xsl] Modify XML using XSL



thanks for the reply.i am providing you a sample xml,


<?xml version="1.0" encoding="UTF-8"?> <books> <nobel> <title> DaVinci Code</title> </nobel> <magazine> <title>Filmfare </title> <title>Femina</title> </magazine> <comics> <title>Tarzan</title> <title>Phantom</title> </comics> </books>

for this XML i need to write an XSL , the xsl will do the following things,

1)generate new xml containing all the elements from the above ; and
2) see number of elements under <nobel> element, if it is less than 2, it will create new element "<title>The Jackal</title>" and append as the last element under <nobel> element; and
3)see number of elements under <comics> element, if it is greater than 1, it will delete the last element under <comics>element;


Thanks,





----- Original Message ----- From: "Michael Kay" <mike@xxxxxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Thursday, April 13, 2006 1:05 PM
Subject: RE: [xsl] Modify XML using XSL



I need to modify an existing XML using XSL. What the XSL should do is,

You've structured the question nicely so it can be translated directly into
XSLT: one template rule for each of your three cases.


I suspect that when you say "nodes" you mean "elements": if not, change "*"
to "node()".

1)Copy the existing xml as it is.

That's an identity template rule: <xsl:template match="*"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:apply-templates/> </xsl:copy> </xsl:template>

2) Count number of sub nodes under a certain node , if count
is less than
some value (say 2) append another new subnode to that node
for the new XML.

<xsl:template match="*[count(*) &lt; 2]"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:apply-templates/> <subnode/> </xsl:copy> </xsl:template>

3) Count number of sub nodes under a certain node , if count
is greater than
some value (say 1) remove the last node from that node for
the new XML.


<xsl:template match="*[count(*) &gt; 1]"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:apply-templates select="*[position() != last()]"/> </xsl:copy> </xsl:template>

Michael Kay
http://www.saxonica.com/


Current Thread
Keywords