XSLT: Making attribute to parent attribute

B

Bostonasian

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?
 
J

Joris Gillis

Hi,
Tempore 01:19:26 said:
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,
 

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
474,000
Messages
2,570,252
Members
46,848
Latest member
CristineKo

Latest Threads

Top