Hi all
I have this situation where I have an xml file similar to this:
<Root>
<MyElement year="2004"><Am ount>10</Amount></MyElement>
<MyElement year="2004"><Am ount>11</Amount></MyElement>
<MyElement year="2005"><Am ount>15</Amount></MyElement>
<MyElement year="2006"><Am ount>4</Amount></MyElement>
<MyElement year="2006"><Am ount>7</Amount></MyElement>
<MyElement year="2004"><Am ount>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 5 1431
hilz wrote: <Root> <MyElement year="2004"><Am ount>10</Amount></MyElement> <MyElement year="2004"><Am ount>11</Amount></MyElement> <MyElement year="2005"><Am ount>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:styleshe et
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:key name="elementBy Year" match="MyElemen t" use="@year" />
<xsl:template match="Root">
<xsl:copy>
<xsl:apply-templates select="MyEleme nt[generate-id() =
generate-id(key('element ByYear', @year)[1])]" />
</xsl:copy>
</xsl:template>
<xsl:template match="MyElemen t">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="key('el ementByYear', @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/
> <xsl:apply-templates select="MyEleme nt[generate-id() = generate-id(key('element ByYear', @year)[1])]" />
For those who haven't seen this trick before, it may be worth explaining...
The expression "generate-id(node1)=gener ate-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
Joe Kesselman wrote: <xsl:apply-templates select="MyEleme nt[generate-id() = generate-id(key('element ByYear', @year)[1])]" />
For those who haven't seen this trick before, it may be worth explaining...
The expression "generate-id(node1)=gener ate-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
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-)
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 fromt the
query, then run the XSLT on it and return the transformed data. But I
need to find out the original structure since the interface is poorly
documented.
Anyone have any suggestions as to how to do this? Or even better a
XSLT file...
|
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 "crumb-trail" of links. I'm
transforming the XML with XSLT through Saxon.
The nav menu and "crumb-trail" show the user where they are within the site
and is made by reflecting the XML tree structure.
My problem now is that when I want to generate...
|
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 (xml->html) doen't happen!
I have XSLT files for this, they work, I mean when I put the output of
the app as an XML file on some server, and make it use the XSLT files to
transform into HTML, it works, I get a HTML page.
|
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 all XML related code and replace
with HTML valid code, but with my commandline XSLT
transformer(http://xmlsoft.org/XSLT/) it outputs a xmlns on elements
I'v used <xsl:copy-of select="/some/node"> to produce. Is this how it
should work, or is this...
|
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" City="Dallas" StateID="2" State="TX"
Date="9/17/2005"/>
<Event ID="3" City="Chicago" StateID="3" State="IL"
Date="9/17/2005"/>
| |
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 and attributes from the
source XML into the output using simple "identity" like XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
|
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 the development
environment or recommend me some other that you know.
XML IDE's
- http://xslt-process.sourceforge.net
- http://www.mentattech.com/themes/mentat/alchemist/index.html
Regards,
|
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)
<Flights>
<Flight>
<FlightLeg >
<BookingClassCodeList>
<BookingCode Value="C" />
<BookingCode Value="Y" />
</BookingClassCodeList>
|
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.
phpinfo() returns:
PHP Version 5.2.1
'./configure' '--enable-pic' '--with-apxs=/usr/local/apache/bin/apxs'
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |