xslt: handle nested structure

Discussion in 'XML' started by thijs.kupers@gmail.com, Dec 6, 2004.

  1. Guest

    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
     
    , Dec 6, 2004
    #1
    1. Advertising

  2. Joris Gillis Guest

    > 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,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
     
    Joris Gillis, Dec 6, 2004
    #2
    1. Advertising

  3. Guest

    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 ...
     
    , Dec 7, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dietrich
    Replies:
    1
    Views:
    648
    Joe Smith
    Jul 22, 2004
  2. Replies:
    0
    Views:
    380
  3. Leon
    Replies:
    2
    Views:
    535
  4. =?ISO-8859-1?Q?KLEIN_St=E9phane?=
    Replies:
    3
    Views:
    454
    hanumizzle
    Oct 6, 2006
  5. Replies:
    1
    Views:
    212
Loading...

Share This Page