Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Recursive processing of an XML document >Thread Next - Re: Recursive processing of an XML document Re: Recursive processing of an XML documentTo: NULL Date: 7/9/2005 6:25:00 PM
"kloppie" <kloppnr1@h...> wrote in message
news:1120895633.749194.296410@o......
> Hi there,
>
> i have a basic understanding of xsl, but now i need some recursive
> processing of a xml-document.
> The xml-document consists of a series of nodes which together form a
> tree - this is done by a parent/child relationship between the rows.
>
> But how do i come from my flat xml-document to a nested document?
> Look below for the source document and desired output document:
>
> My XML source:
>
> <Table>
> <Node ID="1" ParentID="0" Text="1" />
> <Node ID="2" ParentID="1" Text="1.1" />
> <Node ID="3" ParentID="2" Text="1.1.1" />
> <Node ID="4" ParentID="2" Text="1.1.2" />
> <Node ID="5" ParentID="2" Text="1.1.3" />
> <Node ID="6" ParentID="1" Text="1.2" />
> <Node ID="7" ParentID="6" Text="1.2.1" />
> </Table>
>
> My desired XML transformed output:
>
> <?xml version="1.0" encoding="utf-8" ?>
> <TREENODES>
> <treenode text="1">
> <treenode text="1.1">
> <treenode text="1.1.1" />
> <treenode text="1.1.2" />
> <treenode text="1.1.3" />
> </treenode>
> <treenode text="1.2">
> <treenode text="1.2.1"/>
> </treenode>
> </treenode>
>>/TREENODES>
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:template match="/">
<xsl:apply-templates
select="*/Node[not(@ParentID = /*/Node/@ID)]"/>
</xsl:template>
<xsl:template match="Node">
<treenode text="{@Text}">
<xsl:apply-templates
select="/*/Node[@ParentID = current()/@ID]">
</xsl:apply-templates>
</treenode>
</xsl:template>
</xsl:stylesheet>
when applied on your xml document:
<Table>
<Node ID="1" ParentID="0" Text="1" />
<Node ID="2" ParentID="1" Text="1.1" />
<Node ID="3" ParentID="2" Text="1.1.1" />
<Node ID="4" ParentID="2" Text="1.1.2" />
<Node ID="5" ParentID="2" Text="1.1.3" />
<Node ID="6" ParentID="1" Text="1.2" />
<Node ID="7" ParentID="6" Text="1.2.1" />
</Table>
produces the wanted result:
<treenode text="1">
<treenode text="1.1">
<treenode text="1.1.1">
</treenode>
<treenode text="1.1.2">
</treenode>
<treenode text="1.1.3">
</treenode>
</treenode>
<treenode text="1.2">
<treenode text="1.2.1">
</treenode>
</treenode>
</treenode>
>
> I want the xsl to run down the tree and create treenode start-tags as
> long as there is children - and on the way back up the tree the
> end-tags have to be inserted.
XSLT operates on nodes, not on tags.
Cheers,
Dimitre Novatchev
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
