Re: [xsl] Modify XML using XSL

Subject: Re: [xsl] Modify XML using XSL
From: "Santanu Choudhury" <santanuc@xxxxxxxxxx>
Date: Thu, 13 Apr 2006 13:14:38 +0530
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