J
Juho Jussila
Hi
I have a problem while trying to deepen a flat XML document.
The problem is that there are no links between elements. The only
way to determine which element should be a child of another
is to check its position.
Input document is:
<?xml version="1.0" encoding="iso-8859-1"?>
<Root>
<A1/>
<B1/>
<B1/>
<C1/>
<C1/>
<D1/>
<B2/>
<C2/>
<Root>
Element can have 0-n sub-elements:
A1: B1,B2
B1: C1
C1: D1
B2: C2
And I'd like to have this kind of result document:
<A1>
<B1/>
<B1>
<C1/>
<C1>
<D1/>
</C1>
</B1>
<B2>
<C2/>
</B2>
</A1>
After reading FAQ
(http://www.dpawson.co.uk/xsl/sect2/N4486.html#d252e1102)
I managed to write this:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Root">
<xsl:apply-templates select="A1"/>
</xsl:template
<xsl:template match="A1">
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:apply-templates
select="following-sibling::*[1][name()='B1' or name()='B2']" />
</xsl:copy>
</xsl:template>
<xsl:template match="B1|B2">
<xsl:copy-of select="."/>
<xsl:apply-templates
select="following-sibling::*[1][name()='B1' or name()='B2']"/>
</xsl:template>
</xsl:stylesheet>
But now I don't have any idea how to travel next levels.
If I add apply-templates select="C1" inside the last template,
"iterator" position is lost. For example in sequence A1 B1 C1 B1 C1
the last B1 is not matched.
Is there any way to do this with xslt?
Thanks in advance.
I have a problem while trying to deepen a flat XML document.
The problem is that there are no links between elements. The only
way to determine which element should be a child of another
is to check its position.
Input document is:
<?xml version="1.0" encoding="iso-8859-1"?>
<Root>
<A1/>
<B1/>
<B1/>
<C1/>
<C1/>
<D1/>
<B2/>
<C2/>
<Root>
Element can have 0-n sub-elements:
A1: B1,B2
B1: C1
C1: D1
B2: C2
And I'd like to have this kind of result document:
<A1>
<B1/>
<B1>
<C1/>
<C1>
<D1/>
</C1>
</B1>
<B2>
<C2/>
</B2>
</A1>
After reading FAQ
(http://www.dpawson.co.uk/xsl/sect2/N4486.html#d252e1102)
I managed to write this:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Root">
<xsl:apply-templates select="A1"/>
</xsl:template
<xsl:template match="A1">
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:apply-templates
select="following-sibling::*[1][name()='B1' or name()='B2']" />
</xsl:copy>
</xsl:template>
<xsl:template match="B1|B2">
<xsl:copy-of select="."/>
<xsl:apply-templates
select="following-sibling::*[1][name()='B1' or name()='B2']"/>
</xsl:template>
</xsl:stylesheet>
But now I don't have any idea how to travel next levels.
If I add apply-templates select="C1" inside the last template,
"iterator" position is lost. For example in sequence A1 B1 C1 B1 C1
the last B1 is not matched.
Is there any way to do this with xslt?
Thanks in advance.