This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Need to write XSL for this XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Need to write XSL for this XML" Watch "Need to write XSL for this XML" New topic
Author

Need to write XSL for this XML

Raja Mario
Greenhorn

Joined: Sep 24, 2008
Posts: 7
Hi,

I m new to XSL and XSLT. I am using XSLT 1.0 and trying to write the XSLT for the following XML. The output should be, disticnt "PaymentMethod" and the maximum "Amount" in that Payment method.

Required Output:

MasterCard 100
Visa 10
Diners Club 89

XML:

<?xml version="1.0" encoding="utf-8"?>
<Report Name="Payment Detail" Version="1.0" MerchantID="gbp">
<Batches>
<Batch BatchID="1234" BatchDate="2007-06-03">
<Requests>
<Request RequestID="555" MerchantReferenceNumber="1234">
<PaymentMethod>Visa</PaymentMethod>
<CurrencyCode>GBP</CurrencyCode>
<Amount>10.00</Amount>
<Application>bill</Application>

</Request>
<Request RequestID="3456" MerchantReferenceNumber="555">
<PaymentMethod>Visa</PaymentMethod>
<CurrencyCode>GBP</CurrencyCode>
<Amount>10.00</Amount>
<Application>bill</Application>

</Request>
<Request RequestID="44" MerchantReferenceNumber="7655">
<PaymentMethod>MasterCard</PaymentMethod>
<CurrencyCode>GBP</CurrencyCode>
<Amount>83.00</Amount>
<Application>bill</Application>

</Request>
<Request RequestID="55" MerchantReferenceNumber="5678">
<PaymentMethod>MasterCard</PaymentMethod>
<CurrencyCode>GBP</CurrencyCode>
<Amount>82.00</Amount>
<Application>bill</Application>

</Request>
<Request RequestID="1234" MerchantReferenceNumber="67">
<PaymentMethod>Visa</PaymentMethod>
<CurrencyCode>USD</CurrencyCode>
<Amount>81.00</Amount>
<Application>bill</Application>

</Request>
<Request RequestID="2439599466520008401927"
MerchantReferenceNumber="45">
<PaymentMethod>Diners Club</PaymentMethod>
<CurrencyCode>USD</CurrencyCode>
<Amount>89.00</Amount>
<Application>bill</Application>

</Request>
<Request RequestID="2439600359220008402433"
MerchantReferenceNumber="3364613CJY22222CV4BY">
<PaymentMethod>Visa</PaymentMethod>
<CurrencyCode>GBP</CurrencyCode>
<Amount>10.00</Amount>
<Application>bill</Application>
</Request>
<Request RequestID="44" MerchantReferenceNumber="7655">
<PaymentMethod>MasterCard</PaymentMethod>
<CurrencyCode>GBP</CurrencyCode>
<Amount>100.00</Amount>
<Application>bill</Application>
</Request>
</Requests>
</Batch>
</Batches>
</Report>



I tried to use the grouping concept and it is not working. Could anyone help me on this?

Thanks
Raja
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

Well, you know we don't write code for people here. But it seems to me that grouping wouldn't be a bad approach. Sorting based on the "Amount" element and choosing the last value out of the sort would also work.
Raja Mario
Greenhorn

Joined: Sep 24, 2008
Posts: 7
Hi,

Thanks for the reply. Actually your reply induced me to do some research on this and finally I got my output. :)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="lb" select="' '" />
<xsl:param name="sep" select="' '" />
<xsl:output method="text" />
<xsl:key name="by-payment" match="Request" use="PaymentMethod" />

<xsl:template match="/">
<xsl:apply-templates select="Report/Batches/Batch/Requests/Request[generate-id() = generate-id(key('by-payment', PaymentMethod)[1])]" mode="card">
<xsl:sort select="PaymentMethod" data-type='text' order='descending'/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="Request" mode="card">
<xsl:variable name="group" select="key('by-payment', PaymentMethod)" />
<xsl:for-each select="$group">
<xsl:sort select="sum(Amount)" order="descending" data-type='number'/>
<xsl:if test="position() = 1">
<xsl:value-of select="concat(PaymentMethod, $sep, sum(Amount), $sep, CurrencyCode, $sep, $lb)" />
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

I would like to study more on XSLT transfermation. Could you tell me the good site which has tutorials with samples? [Samples will give the best undestanding]

Thanks
Raja
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

That's not bad at all. Grouping isn't an easy thing to do in XSLT 1.0, but it looks like you have it working.

I learned XSLT by reading Michael Kay's book. But there are tutorials on the web, some good, some bad. I don't recommend the ones at w3schools because they just rehash the syntax. That means they don't mention important concepts which aren't described by syntax, like the built-in template rule for example. And they choose examples designed to explain the specific elements of syntax, which isn't the right way to choose examples.

(It's like help documents which answer questions like "What does the F7 key do" when your question is more like "How do I get to the next customer record".)

Anyway have a look at Jeni Tennison's tutorials.
Raja Mario
Greenhorn

Joined: Sep 24, 2008
Posts: 7
Hi Paul, Thank you very much
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to write XSL for this XML