473,396 Members | 1,866 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,396 software developers and data experts.

Grouping sets of tags with XSLT

I have a document like:

<all>
<phone>
<number>12345</number>
<r comp="1"/>
<r comp="2"/>
<r comp="3"/>
<r comp="1">
<r address="a"/>
</r>
<r comp="2">
<r address="b"/>
</r>
<r comp="3">
<r address="c"/>
</r>
<r comp="1">
<r country="a1"/>
</r>
<r comp="2">
<r country="b1"/>
</r>
<r comp="3">
<r country="c1"/>
</r>
</phone>
<phone>
<number>67890</phone>
<r comp="1"/>
<r comp="1">
<r address="a"/>
</r>
<r comp="1">
<r country="a1"/>
</r>
</phone>
</all>

That I would like to use XSLT to transform to:

<all>
<phone>
<number>12345</number>
<r comp="1">
<r address="a"/>
<r country="a1"/>
</r>
<r comp="2">
<r address="b"/>
<r country="b1"/>
</r>
<r comp="3">
<r address="c"/>
<r country="c1"/>
</r>
</phone>
<phone>
<number>67890</phone>
<r comp="1">
<r address="a"/>
<r country="a1"/>
</r>
</phone>
</all>

So I need to do grouping. I've used the key and generate-id trick to
do:

<xsl:key name="rs" match="r" use="@comp">

then:

<xsl:for-each select="r[generate-id(.)=generate-id(key('rs',
@comp)[1])]">

to select the r elements and then process them to get the correct
output. Using this method I can get the first phone element to output
correctly, but when it gets to the second one (or any other one that
has an r element with a comp attribute that has already been processed,
the select in the for-each statement doesn't select anything (comp="1"
was already processed in the previous phone element), so I get no
output. Does anyone have any ideas on how to get this to work or
another way to do it?

Thanks.

Sep 2 '05 #1
1 1050
SL
ma*******@yahoo.com a écrit :
So I need to do grouping. I've used the key and generate-id trick to
do:

<xsl:key name="rs" match="r" use="@comp">

then:

<xsl:for-each select="r[generate-id(.)=generate-id(key('rs',
@comp)[1])]">

to select the r elements and then process them to get the correct
output. Using this method I can get the first phone element to output
correctly, but when it gets to the second one (or any other one that
has an r element with a comp attribute that has already been processed,
the select in the for-each statement doesn't select anything (comp="1"
was already processed in the previous phone element), so I get no
output. Does anyone have any ideas on how to get this to work or
another way to do it?


I think you could avoid using xsl:key with something like:

<xsl:template match="phone">
<phone>
<xsl:copy-of select="number" />
<xsl:for-each select="r[not(*)]">
<xsl:variable name="nbr" select="@comp" />
<r comp="{$nbr}">
<r address="{ ../r[@comp="$nbr"]/r/@address }" />
<r country="{ ../r[@comp="$nbr"]/r/@country }" />
</r>
</xsl:for-each>
</phone>
</xsl:template

not tested at all !

HTH,
SL
Sep 2 '05 #2

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

Similar topics

3
by: Kevin Brown | last post by:
Is there anyway to generate this type of resulting HTML table from this XML using XSLT? Basically I need to be able to consult 2 trees of data to generate the HTML, but I have not been able to...
3
by: Graham | last post by:
Hi, I am having trouble getting XSL to count the members of a group. What I am trying to do is group by <objectid.Contactid> and count the number of <activityid>'s for each <objectid.contactid>....
5
by: Jody Greening | last post by:
Transforming with XSLT, Grouping elements until difference found. I am seeking some help with the following problem, I am fairly new at XSLT transformations, and my problem may lie in looking at...
2
by: Tristan Miller | last post by:
Greetings. I have a question involving sorting and grouping output using XSLT. I am trying to produce a publication list from a BibTeX-like XML file: <bibxml:file...
2
by: Andreas Håkansson | last post by:
Seeing how my previous post seem to have fallen between the cracks, I thought I would have a second, more direct, go at it. So my question is "Is it possible to group (Muenchian method) over...
2
by: viral612 | last post by:
Hi, someone pls help me on this. following is my xml... I need to take out papers for each type separately. and then this each type should have area seperately. which means: there are...
0
by: Hvid Hat | last post by:
Hi At first, I thought I could only solve my problem with a C# method inside my XSLT but I'm beginning to think it might be possible with XSLT only. So I'm trying, but I need help :-) How can I...
1
by: Sandeep Singh | last post by:
Hi, How to do group by in XSLT ? I tried on the following codes: <files> <file name="swablr.eps" size="4313" project="mars"/> <file name="batboy.wks" size="424" ...
4
by: MRe | last post by:
Hi, Is it possible using XSLT to transform this.. <test> <b>0</b> <a>1</a> <a>2</a> <b>3</b> <b>4</b>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
agi2029
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,...

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.