Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: Help - transform driving me crazy! :-s [Thread Next] Re: Help - transform driving me crazy! :-sTo: NULL Date: 3/9/2005 4:11:00 PM
Matthew Gibbons wrote:
> I made a small mistake in my original post - see below - but the question
> still stands...
>
> I have a well-formed XML document with a sub-tree structure like this:
>
> <a>
> <b/> <!-- empty tag -->
> text <c>text</c> text
> <b/> <!-- empty tag -->
> <b/> <!-- empty tag -->
> text
> <b/> <!-- empty tag -->
> </a>
>
> I want to transform this to:
>
> <b>
> text <c>text</c> text
> </b>
> <b>
> text
> </b>
This stylesheet gives you the intended structure:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:key name="new-b-parent" match="a/text() | a/*[local-name() != 'b']"
use="generate-id(preceding-sibling::b[1])" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="a">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="b[position() mod 2 = 1]" />
</xsl:copy>
</xsl:template>
<xsl:template match="b">
<xsl:copy>
<xsl:apply-templates select="key('new-b-parent', generate-id(.))" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
there might still be issues with white space text nodes/indentation not
being what you want.
--
Martin Honnen
http://JavaScript.FAQTs.com/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
