Grouping sets of tags with XSLT

Discussion in 'XML' started by masonmarc@yahoo.com, Sep 2, 2005.

  1. Guest

    I have a document like:

    <all>
    <phone>
    <number>12345</number>
    <r comp="1"/>
    <r comp="2"/>
    <r comp="3"/>
    <r comp="1">
    <r address="a"/>
    </r>
    <r comp="2">
    <r address="b"/>
    </r>
    <r comp="3">
    <r address="c"/>
    </r>
    <r comp="1">
    <r country="a1"/>
    </r>
    <r comp="2">
    <r country="b1"/>
    </r>
    <r comp="3">
    <r country="c1"/>
    </r>
    </phone>
    <phone>
    <number>67890</phone>
    <r comp="1"/>
    <r comp="1">
    <r address="a"/>
    </r>
    <r comp="1">
    <r country="a1"/>
    </r>
    </phone>
    </all>

    That I would like to use XSLT to transform to:

    <all>
    <phone>
    <number>12345</number>
    <r comp="1">
    <r address="a"/>
    <r country="a1"/>
    </r>
    <r comp="2">
    <r address="b"/>
    <r country="b1"/>
    </r>
    <r comp="3">
    <r address="c"/>
    <r country="c1"/>
    </r>
    </phone>
    <phone>
    <number>67890</phone>
    <r comp="1">
    <r address="a"/>
    <r country="a1"/>
    </r>
    </phone>
    </all>

    So I need to do grouping. I've used the key and generate-id trick to
    do:

    <xsl:key name="rs" match="r" use="@comp">

    then:

    <xsl:for-each select="r[generate-id(.)=generate-id(key('rs',
    @comp)[1])]">

    to select the r elements and then process them to get the correct
    output. Using this method I can get the first phone element to output
    correctly, but when it gets to the second one (or any other one that
    has an r element with a comp attribute that has already been processed,
    the select in the for-each statement doesn't select anything (comp="1"
    was already processed in the previous phone element), so I get no
    output. Does anyone have any ideas on how to get this to work or
    another way to do it?

    Thanks.
     
    , Sep 2, 2005
    #1
    1. Advertising

  2. SL Guest

    a écrit :

    > So I need to do grouping. I've used the key and generate-id trick to
    > do:
    >
    > <xsl:key name="rs" match="r" use="@comp">
    >
    > then:
    >
    > <xsl:for-each select="r[generate-id(.)=generate-id(key('rs',
    > @comp)[1])]">
    >
    > to select the r elements and then process them to get the correct
    > output. Using this method I can get the first phone element to output
    > correctly, but when it gets to the second one (or any other one that
    > has an r element with a comp attribute that has already been processed,
    > the select in the for-each statement doesn't select anything (comp="1"
    > was already processed in the previous phone element), so I get no
    > output. Does anyone have any ideas on how to get this to work or
    > another way to do it?


    I think you could avoid using xsl:key with something like:

    <xsl:template match="phone">
    <phone>
    <xsl:copy-of select="number" />
    <xsl:for-each select="r[not(*)]">
    <xsl:variable name="nbr" select="@comp" />
    <r comp="{$nbr}">
    <r address="{ ../r[@comp="$nbr"]/r/@address }" />
    <r country="{ ../r[@comp="$nbr"]/r/@country }" />
    </r>
    </xsl:for-each>
    </phone>
    </xsl:template

    not tested at all !

    HTH,
    SL
     
    SL, Sep 2, 2005
    #2
    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. Christian Ludwig

    XSLT: sorting and grouping

    Christian Ludwig, Nov 24, 2003, in forum: XML
    Replies:
    2
    Views:
    626
    Christian Ludwig
    Nov 26, 2003
  2. Kevin Brown
    Replies:
    3
    Views:
    579
    Kevin Brown
    Aug 28, 2004
  3. Graham

    Help Grouping/Counting XSLT

    Graham, Sep 17, 2004, in forum: XML
    Replies:
    3
    Views:
    587
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=
    Sep 17, 2004
  4. Jody Greening
    Replies:
    5
    Views:
    688
    Jody Greening
    Jan 6, 2005
  5. Steven Bethard

    grouping subsequences with BIO tags

    Steven Bethard, Apr 21, 2005, in forum: Python
    Replies:
    6
    Views:
    343
    Bengt Richter
    Apr 23, 2005
Loading...

Share This Page