XSLT: Making attribute to parent attribute

Discussion in 'XML' started by Bostonasian, Sep 18, 2005.

  1. Bostonasian

    Bostonasian Guest

    I find this challenging task(maybe because I am fairly new to XSLT).
    I've got followoing xml.

    <Events>
    <Event ID="1" City="New York" StateID="1" State="NY"
    Date="9/17/2005"/>
    <Event ID="2" City="Dallas" StateID="2" State="TX"
    Date="9/17/2005"/>
    <Event ID="3" City="Chicago" StateID="3" State="IL"
    Date="9/17/2005"/>
    <Event ID="4" City="San Diego" StateID="4" State="CA"
    Date="9/17/2005"/>
    <Event ID="5" City="Albany" StateID="1" State="NY"
    Date="9/17/2005"/>
    <Event ID="6" City="Los Angeles" StateID="4" State="CA"
    Date="9/17/2005"/>
    </Events>

    And I have to change into following

    <States>
    <State ID="1" Name="NY">
    <Events>
    <Event ID="1" City="New York" StateID="1" State="NY"
    Date="9/17/2005"/>
    <Event ID="5" City="Albany" StateID="1" State="NY"
    Date="9/17/2005"/>
    </Events>
    </State>
    <State ID="2" Name="TX">
    <Events>
    <Event ID="2" City="Dallas" StateID="2" State="TX"
    Date="9/17/2005"/>
    </Events>
    </State>
    <State ID="3" Name="IL">
    <Events>
    <Event ID="3" City="Chicago" StateID="3" State="IL"
    Date="9/17/2005"/>
    </Events>
    </State>
    <State ID="4" Name="CA">
    <Events>
    <Event ID="4" City="San Diego" StateID="4" State="CA"
    Date="9/17/2005"/>
    <Event ID="6" City="Los Angeles" StateID="4" State="CA"
    Date="9/17/2005"/>
    </Events>
    </State>
    </States>

    And don't ask me why. I am not responsible and both schemas are those
    two diffenrent clients have been using and I have no control of
    changing them. Only control I have is XSLT.
    I was able to sort by state.
    Then In use foreach to loop, then I did something like
    if StateID != PrevStateID then create new instance of "State"&"Events"
    element. But I haven't been successful.
    Can anyone help?
    Bostonasian, Sep 18, 2005
    #1
    1. Advertising

  2. Bostonasian

    Joris Gillis Guest

    Hi,
    Tempore 01:19:26, die Sunday 18 September 2005 AD, hinc in foro {comp.text.xml} scripsit Bostonasian <>:

    > I was able to sort by state.
    > Then In use foreach to loop, then I did something like
    > if StateID != PrevStateID then create new instance of "State"&"Events"
    > element. But I haven't been successful.


    This problem can be approached as a common grouping problem. When you're using XSLT 1.0, the most common method for grouping would be the muenchian one:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="xml" indent="yes"/>

    <xsl:key name="EventByState" match="Event" use="@State"/>

    <xsl:template match="Events">
    <States>
    <xsl:for-each select="Event[generate-id()=
    generate-id(key('EventByState',@State)[1])]">
    <xsl:sort select="@StateID"/>
    <State ID="{@StateID}" Name="{@State}">
    <Events>
    <xsl:copy-of select="key('EventByState',@State)"/>
    </Events>
    </State>
    </xsl:for-each>
    </States>
    </xsl:template>

    </xsl:stylesheet>

    regards,
    --
    Joris Gillis (http://users.telenet.be/root-jg/me.html)
    Vincit omnia simplicitas
    Keep it simple
    Joris Gillis, Sep 18, 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. Replies:
    5
    Views:
    1,852
    Andrew Thompson
    Jan 12, 2005
  2. bprimuslevy
    Replies:
    1
    Views:
    429
    Soren Kuula
    Aug 23, 2006
  3. Stefan_W
    Replies:
    1
    Views:
    8,162
    Stefan_W
    Nov 5, 2008
  4. Ulrich Eckhardt
    Replies:
    3
    Views:
    207
    Ulrich Eckhardt
    Nov 23, 2010
  5. Bitswapper
    Replies:
    5
    Views:
    119
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page