Page 1 of 1

xml tree to flat xml

Posted: Tue Nov 14, 2006 6:12 pm
by whiteadi
Hi,

I have an "normal" xml,

a structure like

<x ...>
<y ...>
<z ...>
</z>
</y>
</x>

and I want to have a "flat" xml

<x .../>
<y .../>
<z .../>
so each node with its name and all attributes with original valus just not tree but with nodes not imbricated.(I have the unique ids already to know parent-child).

I try something like this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="x | y | z">
<xsl:text>< </xsl:text>
<xsl:value-of select="name(.)" />
<xsl:for-each select="@*">
<xsl:value-of select="name()" />=<xsl:value-of select="." />
</xsl:for-each>
<xsl:text disable-output-escaping="yes"> /></xsl:text>
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>

but have the error on <xsl:text>< </xsl:text>.

Best regards,
white

Posted: Tue Nov 14, 2006 6:42 pm
by jkmyoung
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="*">
<xsl:copy/>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

Posted: Tue Nov 14, 2006 8:18 pm
by whiteadi
Resolved,an root must be given.(James Durning did it)

Like this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/">
<ENHANCED>
<xsl:apply-templates />
</ENHANCED>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
</xsl:copy>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

I see now that also jkmyoung manage to get the answer, seems was not to complicated, 10x man!