473,403 Members | 2,366 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,403 software developers and data experts.

xslt challenging situation related to iterations

Hi all
I have this situation where I have an xml file similar to this:

<Root>
<MyElement year="2004"><Amount>10</Amount></MyElement>
<MyElement year="2004"><Amount>11</Amount></MyElement>
<MyElement year="2005"><Amount>15</Amount></MyElement>
<MyElement year="2006"><Amount>4</Amount></MyElement>
<MyElement year="2006"><Amount>7</Amount></MyElement>
<MyElement year="2004"><Amount>20</Amount></MyElement>
...
...
</Root>
Now I want to summarize this by transforming it into something like this

<Root>
<MyElement year="2004">
<Amounnt>10</Amount>
<Amounnt>11</Amount>
<Amounnt>20</Amount>
</MyElement>
<MyElement year="2005">
<Amounnt>15</Amount>
</MyElement>
<MyElement year="2006">
<Amounnt>4</Amount>
<Amounnt>7</Amount>
</MyElement>
</Root>
The problem I am having is how to loop through the MyElement elements
and get all amounts for a given unique year, then go to the next year.
In other words, I want to be able to use the year as a unique key for my
iterations.

Any help is greatly appreciated.
Thanks
Feb 13 '06 #1
5 1411


hilz wrote:

<Root>
<MyElement year="2004"><Amount>10</Amount></MyElement>
<MyElement year="2004"><Amount>11</Amount></MyElement>
<MyElement year="2005"><Amount>15</Amount></MyElement> <Root>
<MyElement year="2004">
<Amounnt>10</Amount>
<Amounnt>11</Amount>
<Amounnt>20</Amount>
The problem I am having is how to loop through the MyElement elements
and get all amounts for a given unique year, then go to the next year.
In other words, I want to be able to use the year as a unique key for my
iterations.


Then define a key and use if for grouping:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:key name="elementByYear" match="MyElement" use="@year" />

<xsl:template match="Root">
<xsl:copy>
<xsl:apply-templates select="MyElement[generate-id() =
generate-id(key('elementByYear', @year)[1])]" />
</xsl:copy>
</xsl:template>

<xsl:template match="MyElement">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="key('elementByYear', @year)/Amount" />
</xsl:copy>
</xsl:template>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 13 '06 #2
> <xsl:apply-templates select="MyElement[generate-id() =
generate-id(key('elementByYear', @year)[1])]" />


For those who haven't seen this trick before, it may be worth explaining...

The expression "generate-id(node1)=generate-id(node2): is a way of
testing whether node1 and node2 are the same node... often a useful
test, but one that accidentally got let out of XSLT 1.0.

In this case, we're checking it against the list of nodes keyed by the
same @year value -- specifically, against the first node in that list.
The result is that we process all the possible key values, but only the
first node for each key. This gives us a way to enumerate the keys,
which also got left out of XSLT 1.0.

Then, in the template for that node, we can retrieve and process
everything associated with that key.

Clever solution, and it has become a standard XSLT idiom... but it's
definitely not obvious to a beginner!
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Feb 14 '06 #3
Joe Kesselman wrote:
<xsl:apply-templates select="MyElement[generate-id() =
generate-id(key('elementByYear', @year)[1])]" />


For those who haven't seen this trick before, it may be worth explaining...

The expression "generate-id(node1)=generate-id(node2): is a way of
testing whether node1 and node2 are the same node... often a useful
test, but one that accidentally got let out of XSLT 1.0.

In this case, we're checking it against the list of nodes keyed by the
same @year value -- specifically, against the first node in that list.
The result is that we process all the possible key values, but only the
first node for each key. This gives us a way to enumerate the keys,
which also got left out of XSLT 1.0.

Then, in the template for that node, we can retrieve and process
everything associated with that key.

Clever solution, and it has become a standard XSLT idiom... but it's
definitely not obvious to a beginner!

Thank you Joe and Martin.
You've given me what is necessary for me to proceed.
I also found this example while searching. I guess it is the same as
what Martin suggested:

http://www.jenitennison.com/xslt/gro...muenchian.html
thanks
Feb 14 '06 #4
On Mon, 13 Feb 2006 21:45:53 -0500, Joe Kesselman
<ke************@comcast.net> wrote:
Clever solution, and it has become a standard XSLT idiom... but it's
definitely not obvious to a beginner!


Is _anything_ in XSLT obvious to a beginner? I've never known a
language like it for needing a "cookbook" approach.

OTOH, I'm just starting to learn Scheme.... 8-)
Feb 16 '06 #5
Andy Dingley wrote:
Is _anything_ in XSLT obvious to a beginner? I've never known a
language like it for needing a "cookbook" approach.


Depends on what languages the beginner has been exposed to in the past.
Most folks have only been taught procedural programming, and thinking in
terms of pattern-matching and recursion comes hard until you get used to it.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Feb 16 '06 #6

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

Similar topics

3
by: Matt Bradbury | last post by:
Hi everyone. I'm in a situation where I need to work with XML data that I don't have the format for. It's a web service situation where I hand them a query and an XSLT file. They produce XML...
2
by: ted | last post by:
Was wondering if XSLT alone is appropriate for the following situation. From XML, I'm creating a small website (around 50 pages) with pages that link to each other through a nav menu and a...
12
by: gipsy boy | last post by:
Hello, I have sort of a big problem. I would really appreciate any help you could give me. I made a web service in C++ that throws XML to the client (browser). But, the XSLT transormation...
2
by: Taare | last post by:
Hi, I got <xsl:output method="html" encoding="iso-8859-1" doctype-public="-//W3C//DTD HTML 4.01//EN" doctype-system=" http://www.w3.org/TR/html4/strict.dtd"/> in my XSLT file. This should remove...
1
by: Bostonasian | last post by:
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"...
3
by: Alex | last post by:
I stumbled upon this while developing a custom XPathNavigator. It appears that copy action for attributes is broken in the .net framework XSLT processor. The intent was to just copy the entities...
3
by: Teksure | last post by:
Hi group, searching in the Internet I found two products for XML which incorporate a very robust debugger for XSL/XSLT, I would like you to see these products and then, give me your opinion about...
1
by: byquestion | last post by:
Hi there xslt gurus, i am kinda new to xslt and having difficulty to implement my some iterations. i need to recreate an xml file by using xslt. here is the sample xml file(input for xslt) ...
2
by: Piotr | last post by:
Hello, i got a problem with my hosting provider and the system admin seems not to be a top class specialist... I'm not one myself though. The problem is related to XSLT support in php....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.