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

Help on xslt - grouping

I have a XML that I need to map.
The XML goes like:

<Children>
<Child>
<References>
<External>
<Reference name="filename" value="1.dat"/>
<Reference name="invoicenr" value="1111111"/>
<Reference name="invoicer_name" value="Bill"/>
<Reference name="invoiceref" value="bbbbbb"/>
</External>
</References>
</Child>
<Child>
<References>
<External>
<Reference name="filename" value="2.dat"/>
<Reference name="invoicenr" value="222222"/>
<Reference name="invoicer_name" value="Bill"/>
<Reference name="invoiceref" value="bbbbbb"/>
</External>
</References>
</Child>
<Child>
<References>
<External>
<Reference name="filename" value="3.dat"/>
<Reference name="invoicenr" value="33333"/>
<Reference name="invoicer_name" value="Clinton"/>
<Reference name="invoiceref" value="ccccc"/>
</External>
</References>
</Child>
</Children>

I want the structure to map to:

<Senders>
<Sender>
<invoicer_name>Bill</invoicer_name>
<invoiceref>bbbbbb</invoiceref>
<Items TotalItems="2"/>
</Sender>
<Sender>
<invoicer_name>Clinton</invoicer_name>
<invoiceref>ccccc</invoiceref>
<Items TotalItems="1"/>
</Sender>
</Senders>

I have tried to use the "Muenchian Grouping" method, but am not able to
obtain both
<invoicer_name> and <invoiceref> under the <Sender> tag

Here is my xsl :

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:output encoding="ISO-8859-1"/>
<xsl:key name="kDistinctSender"
match="Children/Child/References/External/Reference[@name='invoicer_name']/@
value" use="."/>
<xsl:template match="/">
<Senders>
<!-- go through distinct InvoicerName -->
<xsl:for-each
select="/Children/Child/References/External/Reference[@name='invoicer_name']
/@value[generate-id()=generate-id(key('kDistinctSender',.))]">
<!-- sort by InvoicerName -->
<xsl:sort select="."/>
<Sender>
<xsl:variable name="InvoicerName">
<xsl:value-of select="."/>
</xsl:variable>
<InvoicerName>
<xsl:value-of select="$InvoicerName"/>
</InvoicerName>
<Items TotalItems="{count(key('kDistinctSender',.))}">
</Items>
</Sender>
</xsl:for-each>
</Senders>
</xsl:template>
</xsl:stylesheet>
Jul 20 '05 #1
6 1562
Specify for the "use" attribute of xsl:key the concatenation of the values
of "invoicer_name" and "invoiceref".
Cheers,
Dimitre Novatchev
"Per Jørgen Vigdal" <pe***************@ergo.no> wrote in message
news:11***************@makrell.interpost.no...
I have a XML that I need to map.
The XML goes like:

<Children>
<Child>
<References>
<External>
<Reference name="filename" value="1.dat"/>
<Reference name="invoicenr" value="1111111"/>
<Reference name="invoicer_name" value="Bill"/>
<Reference name="invoiceref" value="bbbbbb"/>
</External>
</References>
</Child>
<Child>
<References>
<External>
<Reference name="filename" value="2.dat"/>
<Reference name="invoicenr" value="222222"/>
<Reference name="invoicer_name" value="Bill"/>
<Reference name="invoiceref" value="bbbbbb"/>
</External>
</References>
</Child>
<Child>
<References>
<External>
<Reference name="filename" value="3.dat"/>
<Reference name="invoicenr" value="33333"/>
<Reference name="invoicer_name" value="Clinton"/>
<Reference name="invoiceref" value="ccccc"/>
</External>
</References>
</Child>
</Children>

I want the structure to map to:

<Senders>
<Sender>
<invoicer_name>Bill</invoicer_name>
<invoiceref>bbbbbb</invoiceref>
<Items TotalItems="2"/>
</Sender>
<Sender>
<invoicer_name>Clinton</invoicer_name>
<invoiceref>ccccc</invoiceref>
<Items TotalItems="1"/>
</Sender>
</Senders>

I have tried to use the "Muenchian Grouping" method, but am not able to
obtain both
<invoicer_name> and <invoiceref> under the <Sender> tag

Here is my xsl :

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:output encoding="ISO-8859-1"/>
<xsl:key name="kDistinctSender"
match="Children/Child/References/External/Reference[@name='invoicer_name']/@
value" use="."/>
<xsl:template match="/">
<Senders>
<!-- go through distinct InvoicerName -->
<xsl:for-each
select="/Children/Child/References/External/Reference[@name='invoicer_name']
/@value[generate-id()=generate-id(key('kDistinctSender',.))]">
<!-- sort by InvoicerName -->
<xsl:sort select="."/>
<Sender>
<xsl:variable name="InvoicerName">
<xsl:value-of select="."/>
</xsl:variable>
<InvoicerName>
<xsl:value-of select="$InvoicerName"/>
</InvoicerName>
<Items TotalItems="{count(key('kDistinctSender',.))}">
</Items>
</Sender>
</xsl:for-each>
</Senders>
</xsl:template>
</xsl:stylesheet>

Jul 20 '05 #2
Thanks
I have tried to play around with concatenation and cant get it right, her is
the result :

<Senders>
<Sender>
<InvoicerName>Bill</InvoicerName>
<Items TotalItems="3"/>
</Sender>
</Senders>
Using xsl :

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:output encoding="ISO-8859-1"/>
<xsl:key name="kDistinctSender"
match="Children/Child/References/External/Reference[@name='invoicer_name']/@
value" use="concat(@name,'||',@value)"/>
<xsl:template match="/">
<Senders>

<xsl:for-each
select="/Children/Child/References/External/Reference[@name='invoicer_name']
/@value[generate-id()=generate-id(key('kDistinctSender',concat(@name,'||',@v
alue)))]">

<xsl:sort select="."/>
<Sender>
<xsl:variable name="InvoicerName">
<xsl:value-of select="."/>
</xsl:variable>
<InvoicerName>
<xsl:value-of select="$InvoicerName"/>
</InvoicerName>
<Items
TotalItems="{count(key('kDistinctSender',concat(@n ame,'||',@value)))}">
</Items>
</Sender>
</xsl:for-each>
</Senders>
</xsl:template>
</xsl:stylesheet>
"Dimitre Novatchev" <di******@tpg.com.au> wrote in message
news:42***********************@authen.white.readfr eenews.net...
Specify for the "use" attribute of xsl:key the concatenation of the values
of "invoicer_name" and "invoiceref".
Cheers,
Dimitre Novatchev
"Per Jørgen Vigdal" <pe***************@ergo.no> wrote in message
news:11***************@makrell.interpost.no...
I have a XML that I need to map.
The XML goes like:

<Children>
<Child>
<References>
<External>
<Reference name="filename" value="1.dat"/>
<Reference name="invoicenr" value="1111111"/>
<Reference name="invoicer_name" value="Bill"/>
<Reference name="invoiceref" value="bbbbbb"/>
</External>
</References>
</Child>
<Child>
<References>
<External>
<Reference name="filename" value="2.dat"/>
<Reference name="invoicenr" value="222222"/>
<Reference name="invoicer_name" value="Bill"/>
<Reference name="invoiceref" value="bbbbbb"/>
</External>
</References>
</Child>
<Child>
<References>
<External>
<Reference name="filename" value="3.dat"/>
<Reference name="invoicenr" value="33333"/>
<Reference name="invoicer_name" value="Clinton"/>
<Reference name="invoiceref" value="ccccc"/>
</External>
</References>
</Child>
</Children>

I want the structure to map to:

<Senders>
<Sender>
<invoicer_name>Bill</invoicer_name>
<invoiceref>bbbbbb</invoiceref>
<Items TotalItems="2"/>
</Sender>
<Sender>
<invoicer_name>Clinton</invoicer_name>
<invoiceref>ccccc</invoiceref>
<Items TotalItems="1"/>
</Sender>
</Senders>

I have tried to use the "Muenchian Grouping" method, but am not able to
obtain both
<invoicer_name> and <invoiceref> under the <Sender> tag

Here is my xsl :

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:output encoding="ISO-8859-1"/>
<xsl:key name="kDistinctSender"
match="Children/Child/References/External/Reference[@name='invoicer_name']/@ value" use="."/>
<xsl:template match="/">
<Senders>
<!-- go through distinct InvoicerName -->
<xsl:for-each
select="/Children/Child/References/External/Reference[@name='invoicer_name'] /@value[generate-id()=generate-id(key('kDistinctSender',.))]">
<!-- sort by InvoicerName -->
<xsl:sort select="."/>
<Sender>
<xsl:variable name="InvoicerName">
<xsl:value-of select="."/>
</xsl:variable>
<InvoicerName>
<xsl:value-of select="$InvoicerName"/>
</InvoicerName>
<Items TotalItems="{count(key('kDistinctSender',.))}">
</Items>
</Sender>
</xsl:for-each>
</Senders>
</xsl:template>
</xsl:stylesheet>


Jul 20 '05 #3
Per Jørgen Vigdal wrote:
I have a XML that I need to map.
The XML goes like:

<Children>
<Child>
<References>
<External>
<Reference name="filename" value="1.dat"/>
<Reference name="invoicenr" value="1111111"/>
<Reference name="invoicer_name" value="Bill"/>
<Reference name="invoiceref" value="bbbbbb"/>
</External>
</References>
</Child>
<Child>
<References>
<External>
<Reference name="filename" value="2.dat"/>
<Reference name="invoicenr" value="222222"/>
<Reference name="invoicer_name" value="Bill"/>
<Reference name="invoiceref" value="bbbbbb"/>
</External>
</References>
</Child>
<Child>
<References>
<External>
<Reference name="filename" value="3.dat"/>
<Reference name="invoicenr" value="33333"/>
<Reference name="invoicer_name" value="Clinton"/>
<Reference name="invoiceref" value="ccccc"/>
</External>
</References>
</Child>
</Children>

I want the structure to map to:

<Senders>
<Sender>
<invoicer_name>Bill</invoicer_name>
<invoiceref>bbbbbb</invoiceref>
<Items TotalItems="2"/>
</Sender>
<Sender>
<invoicer_name>Clinton</invoicer_name>
<invoiceref>ccccc</invoiceref>
<Items TotalItems="1"/>
</Sender>
</Senders>

I have tried to use the "Muenchian Grouping" method, but am not able to
obtain both
<invoicer_name> and <invoiceref> under the <Sender> tag

Here is my xsl :

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:output encoding="ISO-8859-1"/>
<xsl:key name="kDistinctSender"
match="Children/Child/References/External/Reference[@name='invoicer_name']/@
value" use="."/>
<xsl:template match="/">
<Senders>
<!-- go through distinct InvoicerName -->
<xsl:for-each
select="/Children/Child/References/External/Reference[@name='invoicer_name']
/@value[generate-id()=generate-id(key('kDistinctSender',.))]">
<!-- sort by InvoicerName -->
<xsl:sort select="."/>
<Sender>
<xsl:variable name="InvoicerName">
<xsl:value-of select="."/>
</xsl:variable>
<InvoicerName>
<xsl:value-of select="$InvoicerName"/>
</InvoicerName>
<Items TotalItems="{count(key('kDistinctSender',.))}">
</Items>
</Sender>
</xsl:for-each>
</Senders>
</xsl:template>
</xsl:stylesheet>


Did you try to simply put them in the right order?
<xsl:template match="/">
<Senders>
<!-- go through distinct InvoicerName -->
<xsl:apply-templates select="-XPath expression-">
</Senders>
</xsl:template>

<xsl:template match="-expression from above-">
<Sender>
<xsl:apply-templates select=".[@name='invoicer_name']"/>
<xsl:apply-templates select=".[@name='invoiceref']"/>
<xsl:apply-templates select=".[@name='otherArtributeName']"/>
<xsl:apply-templates select=".[@name='............']"/>
<xsl:apply-templates select=".[@name='-one more line-']"/>
</Sender>
</xsl:apply-templates>
</xsl:template>

<xsl:........... - And Some More Templates - ........./>
Jul 20 '05 #4

"Per Jørgen Vigdal" <pe***************@ergo.no> wrote in message
news:11***************@makrell.interpost.no...
Thanks
I have tried to play around with concatenation and cant get it right, her
is
the result :

<Senders>
<Sender>
<InvoicerName>Bill</InvoicerName>
<Items TotalItems="3"/>
</Sender>
</Senders>

This transformation:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:strip-space elements="*"/>

<xsl:key name="kExtNameRef" match="External"
use="concat(Reference[@name='invoicer_name']/@value,
'+',
Reference[@name='invoiceref']/@value
)"/>
<xsl:template match="/">
<Senders>
<xsl:for-each select=
"/*/*/*/External
[generate-id()
=
generate-id(
key('kExtNameRef',
concat(Reference[@name='invoicer_name']/@value,
'+',
Reference[@name='invoiceref']/@value
)
)[1]
)
]">
<Sender>
<invoicer_name>
<xsl:value-of select=
"Reference[@name='invoicer_name']/@value"/>
</invoicer_name>
<invoiceref>
<xsl:value-of select=
"Reference[@name='invoiceref']/@value"/>
</invoiceref>
<Items TotalItems="{
count(
key('kExtNameRef',
concat(Reference[@name='invoicer_name']/@value,
'+',
Reference[@name='invoiceref']/@value
)
)
)
}"/>
</Sender>
</xsl:for-each>
</Senders>

</xsl:template>
</xsl:stylesheet>

when applied on your source.xml:

<Children>
<Child>
<References>
<External>
<Reference name="filename" value="1.dat"/>
<Reference name="invoicenr" value="1111111"/>
<Reference name="invoicer_name" value="Bill"/>
<Reference name="invoiceref" value="bbbbbb"/>
</External>
</References>
</Child>
<Child>
<References>
<External>
<Reference name="filename" value="2.dat"/>
<Reference name="invoicenr" value="222222"/>
<Reference name="invoicer_name" value="Bill"/>
<Reference name="invoiceref" value="bbbbbb"/>
</External>
</References>
</Child>
<Child>
<References>
<External>
<Reference name="filename" value="3.dat"/>
<Reference name="invoicenr" value="33333"/>
<Reference name="invoicer_name" value="Clinton"/>
<Reference name="invoiceref" value="ccccc"/>
</External>
</References>
</Child>
</Children>

produces the wanted result:

<Senders>
<Sender>
<invoicer_name>Bill</invoicer_name>
<invoiceref>bbbbbb</invoiceref>
<Items TotalItems="2" />
</Sender>
<Sender>
<invoicer_name>Clinton</invoicer_name>
<invoiceref>ccccc</invoiceref>
<Items TotalItems="1" />
</Sender>
</Senders>
Hope this helped.

Cheers,
Dimitre Novatchev
Jul 20 '05 #5
This is great, exactly what I want. Thank you.
This is goanna be tested in QA 22. Mai and if successful, put into
production on 25. Mai where the
xsl will do transformation on files that are as big as 100MB with thousands
of items


"Dimitre Novatchev" <di******@tpg.com.au> wrote in message
news:42***********************@authen.white.readfr eenews.net...

"Per Jørgen Vigdal" <pe***************@ergo.no> wrote in message
news:11***************@makrell.interpost.no...
Thanks
I have tried to play around with concatenation and cant get it right, her is
the result :

<Senders>
<Sender>
<InvoicerName>Bill</InvoicerName>
<Items TotalItems="3"/>
</Sender>
</Senders>

This transformation:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:strip-space elements="*"/>

<xsl:key name="kExtNameRef" match="External"
use="concat(Reference[@name='invoicer_name']/@value,
'+',
Reference[@name='invoiceref']/@value
)"/>
<xsl:template match="/">
<Senders>
<xsl:for-each select=
"/*/*/*/External
[generate-id()
=
generate-id(
key('kExtNameRef',
concat(Reference[@name='invoicer_name']/@value,
'+',
Reference[@name='invoiceref']/@value
)
)[1]
)
]">
<Sender>
<invoicer_name>
<xsl:value-of select=
"Reference[@name='invoicer_name']/@value"/>
</invoicer_name>
<invoiceref>
<xsl:value-of select=
"Reference[@name='invoiceref']/@value"/>
</invoiceref>
<Items TotalItems="{
count(
key('kExtNameRef',
concat(Reference[@name='invoicer_name']/@value,
'+',
Reference[@name='invoiceref']/@value
)
)
)
}"/>
</Sender>
</xsl:for-each>
</Senders>

</xsl:template>
</xsl:stylesheet>

when applied on your source.xml:

<Children>
<Child>
<References>
<External>
<Reference name="filename" value="1.dat"/>
<Reference name="invoicenr" value="1111111"/>
<Reference name="invoicer_name" value="Bill"/>
<Reference name="invoiceref" value="bbbbbb"/>
</External>
</References>
</Child>
<Child>
<References>
<External>
<Reference name="filename" value="2.dat"/>
<Reference name="invoicenr" value="222222"/>
<Reference name="invoicer_name" value="Bill"/>
<Reference name="invoiceref" value="bbbbbb"/>
</External>
</References>
</Child>
<Child>
<References>
<External>
<Reference name="filename" value="3.dat"/>
<Reference name="invoicenr" value="33333"/>
<Reference name="invoicer_name" value="Clinton"/>
<Reference name="invoiceref" value="ccccc"/>
</External>
</References>
</Child>
</Children>

produces the wanted result:

<Senders>
<Sender>
<invoicer_name>Bill</invoicer_name>
<invoiceref>bbbbbb</invoiceref>
<Items TotalItems="2" />
</Sender>
<Sender>
<invoicer_name>Clinton</invoicer_name>
<invoiceref>ccccc</invoiceref>
<Items TotalItems="1" />
</Sender>
</Senders>
Hope this helped.

Cheers,
Dimitre Novatchev

Jul 20 '05 #6

"Per Jørgen Vigdal" <pe***************@ergo.no> wrote in message
news:11***************@makrell.interpost.no...
This is great, exactly what I want. Thank you.
This is goanna be tested in QA 22. Mai and if successful, put into
production on 25. Mai where the
xsl will do transformation on files that are as big as 100MB with
thousands
of items


Good luck, and it would be interesting if you share your experience (or if
you have any problems -- just signal) to the newsgroups (and the xsl-list).
Cheers,
Dimitre Novatchev
Jul 20 '05 #7

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

Similar topics

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: inquirydog | last post by:
Hi- Does anyone know a way to compare whether two nodes contain the same information in xslt (the name, attributes, and all content recursivly should be the same. I am interested in the case...
1
by: Omega375 | last post by:
Hello. I'll first start off by showing the structure of my xml-file. <root> <node path="X"> More nodes containing data </node> </root>
5
by: Chris Kettenbach | last post by:
Good Morning, Sorry for xposting. Just need a liitle help. I have an xml file that's generated from a database. How do I select distinct values from a field in xslt and then loop through the...
4
by: Chris Kettenbach | last post by:
Hi Peter, I get error when processing the stylesheet. It errors here. <xsl:for-each select="registration)=1]"> specifically: Expression does not return a DOM node. registration)=1]<--
2
by: parth | last post by:
I have the following XML file - <root> <book> <section>art</section> <title>abc</title> <author>mark</author> </book> <book> <section>science</section>
3
by: silver_sabrina | last post by:
Hey everyone, I'm having some trouble with this. I need to convert one xml doc into another and I think that XSLT may be the answer, so I'd like some help from the gurus out here :) If XSLT cannot...
1
by: AndreasAndersson | last post by:
Hi! I'm trying to design an XSLT in order to transform an XML document into another XML document. The source XML looks something like this: <?xml version="1.0" encoding="UTF-8"?> <Orders>...
2
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
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" ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.