Subject: Re: [xsl] Modify XML using XSL From: "Santanu Choudhury" <santanuc@xxxxxxxxxx> Date: Thu, 13 Apr 2006 13:14:38 +0530 |
<?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>
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/
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Determining the context n, Wendell Piez | Thread | Re: [xsl] Modify XML using XSL, Santanu Choudhury |
RE: [xsl] Modify XML using XSL, Michael Kay | Date | [xsl] sorting question, Kent Seegmiller |
Month |