xslt: handle nested structure

T

thijs.kupers

hy,

I don't have so much experience with xslt, so i've got a question about
it.

I've got a xml-document with the following structure:
<Block>
<Block>
<State name="1">...</State>
<State name="2">...</State>
<Block>
<State name="3">...</State>
</Block>
<Trans name="a"/>
<Trans name="b"/>
</Block>
<Trans name="c"/>
</Block>

A 'state' (and 'Trans') can be a child of a 'block' and a 'block' can
be a child of another 'block'. What I as output want is the same as the
input, with the difference that every 'State' has all the 'Trans' items
from the parents (so State 3 gets trans a, b and c. State 2 gets only
state c)

for example:

<Block name="b1">
<Block name="b2">
<State name="1">...
<Trans name="c" blockFrom="b1"/>
</State>
<State name="2">...
<Trans name="c" blockFrom="b1"/>
</State>
<Block name="b3">
<State name="3">...
<Trans name="a" blockFrom="b2"/>
<Trans name="b" blockFrom="b2"/>
<Trans name="c" blockFrom="b1"/>
</State>
</Block>
<Trans name="a"/>
<Trans name="b"/>
</Block>
<Trans name="c"/>
</Block>


I hope, someone can help me

Regards,

Thijs
 
J

Joris Gillis

hy,
I don't have so much experience with xslt, so i've got a question about
it.

I've got a xml-document with the following structure:
<Block>
<Block>
<State name="1">...</State>
<State name="2">...</State>
<Block>
<State name="3">...</State>
</Block>
<Trans name="a"/>
<Trans name="b"/>
</Block>
<Trans name="c"/>
</Block>

A 'state' (and 'Trans') can be a child of a 'block' and a 'block' can
be a child of another 'block'. What I as output want is the same as the
input, with the difference that every 'State' has all the 'Trans' items
from the parents (so State 3 gets trans a, b and c. State 2 gets only
state c)

Hi,

nice problem;-)

here's a solution:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:eek:utput indent="yes" method="xml"/>

<xsl:template match="Block">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="State">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
<xsl:apply-templates select="../ancestor::Block/Trans" mode="insert"/>
</xsl:copy>
</xsl:template>

<xsl:template match="Trans">
<xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="Trans" mode="insert">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="BlockFrom"><xsl:value-of select="../@name"/></xsl:attribute>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>



regards,
 
T

thijs.kupers

Thanks,

it works :)

the stuff with the ancestor and sibbeling is still a little bit strange
to me. You can do nice things with it. Maybe its a good idea that I
take a closer look on it ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top