|
Subject: Re: [xsl] Help to unflatten xml file From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Tue, 30 Nov 2010 17:16:06 -0500 |
I want to nest sections from a flat XML file based on the value of a section attribute. I have done this before with a similar but slightly different situation. I'm missing something with the behavior of xsl:for-each-group.
<xsl:template match="/">
<xsl:variable name="unflattenedDoc">
<book>
<xsl:copy>
<xsl:for-each-group select="*" group-starting-with="section[@label=1]">
T:\ftemp>type steve.xml
<?xml version="1.0" encoding="UTF-8"?>
<book type="Tech_Manual">
<bookinfo>
<title>client manual</title>
<subtitle>performance data</subtitle>
</bookinfo>
<section id="a--21" label="1">
<title lang="en">Preface</title>
<para>some interesting stuff</para>
</section>
<section id="a--22" label="2">
<title lang="en">Preface</title>
<para>some more interesting stuff</para>
</section>
<section id="a--23" label="2">
<title lang="en">Preface</title>
<para>some more interesting stuff</para>
</section>
<section id="a--24" label="3">
<title lang="en">Preface</title>
<para>some more interesting stuff</para>
</section>
<section id="a--25" label="1">
<title lang="en">Cautions</title>
<para>some interesting stuff</para>
</section>
</book>T:\ftemp>call xslt2 steve.xml steve.xsl
<?xml version="1.0" encoding="utf-8"?>
<book>
<bookinfo>
<title>client manual</title>
<subtitle>performance data</subtitle>
</bookinfo>
<section id="a--21" label="1">
<title lang="en">Preface</title>
<para>some interesting stuff</para>
<section id="a--22" label="2">
<title lang="en">Preface</title>
<para>some more interesting stuff</para>
</section>
<section id="a--23" label="2">
<title lang="en">Preface</title>
<para>some more interesting stuff</para>
<section id="a--24" label="3">
<title lang="en">Preface</title>
<para>some more interesting stuff</para>
</section>
</section>
</section>
<section id="a--25" label="1">
<title lang="en">Cautions</title>
<para>some interesting stuff</para>
</section>
</book>
T:\ftemp>type steve.xsl
<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" omit-xml-declaration="no" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/>
<xsl:template match="section[@label=1]" mode="group">
<section>
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="group"/>
<xsl:for-each-group select="current-group()[position()>1]"
group-starting-with="section[@label=2]">
<xsl:apply-templates select="." mode="group"/>
</xsl:for-each-group>
</section>
</xsl:template> <xsl:template match="section[@label=2]" mode="group">
<section>
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="group"/>
<xsl:for-each-group select="current-group()[position()>1]"
group-starting-with="section[@label=3]">
<xsl:apply-templates select="." mode="group"/>
</xsl:for-each-group>
</section>
</xsl:template> <xsl:template match="section[@label=3]" mode="group">
<section>
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="group"/>
</section>
</xsl:template>
<xsl:template match="node()" mode="group"> <xsl:copy-of select="."/> </xsl:template>
<!-- 2nd pass --> <xsl:template match="book" mode="pass2"> <xsl:copy-of select="."/> </xsl:template>
-- Contact us for world-wide XML consulting & instructor-led training Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Legal business disclaimers: http://www.CraneSoftwrights.com/legal
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| [xsl] Help to unflatten xml file, Steve Ylvisaker | Thread | [xsl] XPATH Issue, Rajesh Jain |
| Re: [xsl] Applying a Default Templa, Neil Beddoe | Date | Re: [xsl] Count elements A up to an, Christian Roth |
| Month |