[Home] [By Thread] [By Date] [Recent Entries]
My question is: What is the best approach to transform the "flat" structure back to the original logical structure, with XSLT2 (Saxon), assuming that only the 'flat' version is received by the application? Something as simple as this transformation (XSLT 1.0): <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:key name="kChildren" match="section"
use="generate-id(preceding::section
[@level = current()/@level - 1]
[last()]
)"
/>
<xsl:template match="/">
<section id="logical">
<xsl:apply-templates select="*/section[@level = 1]"/>
</section>
</xsl:template>
<xsl:template match="section[@level]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="key('kChildren', generate-id())"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>When applied on the provided source xml document: <section id="flat"> <section id="1" level="1"/> <section id="1-1" level="2"/> <section id="1-2" level="2"/> <section id="1-2-1" level="3"/> <section id="1-2-2" level="3"/> <section id="2" level="1"/> </section> the wanted result is produced: <section id="logical">
<section id="1" level="1">
<section id="1-1" level="2">
<section id="1-2-1" level="3" />
<section id="1-2-2" level="3" />
</section>
<section id="1-2" level="2" />
</section>
<section id="2" level="1" />
</section>-- Cheers, Dimitre Novatchev --------------------------------------- Truly great madness cannot be achieved without significant intelligence. --------------------------------------- To invent, you need a good imagination and a pile of junk ------------------------------------- You've achieved success in your field when you don't know whether what you're doing is work or play On 3/7/07, ac <ac@xxxxxxxxxxxxx> wrote: Hi,
|

Cart



