473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

XSLT: Making attribute to parent attribute

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?

Sep 17 '05 #1
1 1758
Hi,
Tempore 01:19:26, die Sunday 18 September 2005 AD, hinc in foro {comp.text.xml} scripsit Bostonasian <ax****@gmail.com>:
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:output 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
Sep 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sergio del Amo | last post by:
Hi, I use the xslt functions provided by php. I am running in my computer the package xampp(www.apachefriends.org) which includes php/apache/mysql .. In this package the php includes the sablotron...
2
by: poofpoof | last post by:
Dear Experts, I have some problems with XSLT recursivity... here what I want to do XML part : ---------- <struct>
4
by: titanandrews | last post by:
Hi, I have ran into a situation that I think should be possible, but I am fairly new to XSLT so maybe not. Suppose I have the following document <ROOT> <FOO name="A"> <CHILD name="B"/>...
2
by: clres | last post by:
Hi, I have an XSLT that needs to adjust height and width attributes of images. It sets the width to a particular value and should adjust the height correspondingly. I cannot seem to get it to...
12
by: Keith Chadwick | last post by:
I have a fairly hefty XSLT file that for the sake of debugging and clarity I wish to split into some separate sub-templates contained within the same file. The master template calls an...
2
by: Anthony | last post by:
Hi all, i have managed to create an asp.net menu using xml and xslt.. i am now trying to highlite the selected path by sending in the path info and hilite it using an xsl select choose function....
1
by: arnold | last post by:
Hi, I've been knocking my head against the wall trying to create an XSL transform to perform "normalizations" of a set of XML files that have a common structure. % XML file before transform
1
by: SWETA123 | last post by:
I will very helpful to you if you can help me out in my below problem. I am not getting idea what should be the right approach for this. Here is my problem. I have below (sample)xml. <Rules>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.