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(_at_)saxonica(_dot_)com>
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
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(*) < 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(*) > 1]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="*[position() != last()]"/>
</xsl:copy>
</xsl:template>
Michael Kay
http://www.saxonica.com/
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--