"Simon" <no@no.no> wrote in message
news:c5**********@sparta.btinternet.com...
Hi,
I have generated an xml document, and would like to be able to transform
it to another such that the contents of a chosen node type are flattened
(i.e. tags removed). e.g.
<shop>
<name>super</name>
<sells>
<drink>squash</drink>
<drink>beer</drink>
<food>
<fresh>bread</fresh>
<fresh>apples</fresh>
<frozen>peas</frozen>
</food>
</sells>
</shop>
apply 'flatten' to <sells> to give...
<shop>
<name>super</name>
<sells>
squash
beer
bread
apples
peas
</sells>
</shop>
This seems like the kind of thing I could use xsl for. Could someone
please give me a xsl example showing how this would be done?
This is quite straightforward. This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[ancestor::sells]">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()[ancestor::sells]">
<xsl:value-of select="concat('
',.)"/>
</xsl:template>
</xsl:stylesheet>
when applied on your source.xml:
<shop>
<name>super</name>
<sells>
<drink>squash</drink>
<drink>beer</drink>
<food>
<fresh>bread</fresh>
<fresh>apples</fresh>
<frozen>peas</frozen>
</food>
</sells>
</shop>
produces the wanted result:
<shop>
<name>super</name>
<sells>
squash
beer
bread
apples
peas</sells>
</shop>
Cheers,
Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,
http://fxsl.sourceforge.net/ -- the home of FXSL
Resume:
http://fxsl.sf.net/DNovatchev/Resume/Res.html