This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: Flattening a tree
- To: "Pierre-Yves Saumont" <pys at volga dot fr>
- Subject: RE: Flattening a tree
- From: Jeni Tennison <Jeni dot Tennison at epistemics dot co dot uk>
- Date: Fri, 14 Jul 2000 16:09:45 +0100
- Cc: <xsl-list at mulberrytech dot com>
- References: <3.0.6.32.20000714114514.012d7460@NTServer>
- Reply-To: xsl-list at mulberrytech dot com
Pierre,
>However, the first solution is not flat enough and the second one is too
>flat !
Erm - it was all supposed to be one solution. Sorry I didn't make that
clearer. Using your input:
---- test.xml ----
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<document>
<para>
content-1
<List>
<Item>content-2</Item>
<Item>content-3</Item>
</List>
content-4
<graphic/>
content-5
</para>
</document>
----
With the following stylesheet:
---- test.xsl ----
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="*" mode="flatten">
<xsl:choose>
<xsl:when test="*"><xsl:apply-templates mode="flatten" /></xsl:when>
<xsl:otherwise>
<xsl:copy-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="text()" mode="flatten">
<xsl:if test="normalize-space(.) != ''">
<xsl:element name="{name(..)}">
<xsl:value-of select="normalize-space(.)" />
</xsl:element>
</xsl:if>
</xsl:template>
<xsl:template match="document">
<document>
<xsl:apply-templates mode="flatten" />
</document>
</xsl:template>
</xsl:stylesheet>
----
Gives the output:
---- out.xsl ----
<?xml version="1.0"?>
<document>
<para>content-1</para>
<Item>content-2</Item>
<Item>content-3</Item>
<para>content-4</para>
<graphic/>
<para>content-5</para>
</document>
----
Which is what you said you wanted. This was using SAXON.
I hope that works for you now,
Jeni
Dr Jeni Tennison
Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE
Telephone 0115 9061301 • Fax 0115 9061304 • Email
jeni.tennison@epistemics.co.uk
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list