Subject: RE: Modify XML using XSL
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 13 Apr 2006 08:35:25 +0100
|
> 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/
|