473,763 Members | 9,275 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Transforming with XSLT, Grouping elements until difference found.

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 it from a
traditional programming point of view.

I have did quite a bit of searching but have found no answers to my
particular problem... please read below XML for the problem I am
having.

PS: I have no control over the XML I am trying to transform.

--- I have only spaced the lines for clarity ---

<Amounts>
<Amount date="2004-05-30">5290</Amount>
<Amount date="2004-06-30">5290</Amount>

<Amount date="2004-07-30">5300</Amount>
<Amount date="2004-08-30">5300</Amount>

<Amount date="2004-09-30">5290</Amount>
<Amount date="2004-10-30">5290</Amount>
<Amount date="2004-11-30">5290</Amount>

<Amount date="2004-12-30">1000</Amount>

<Amount date="2005-01-30">5290</Amount>

<Amount date="2005-02-28">5300</Amount>

<Amount date="2005-03-30">5290</Amount>
<Amount date="2005-04-30">5290</Amount>

<Amount date="2004-07-30">5300</Amount>
<Amount date="2004-08-30">5300</Amount>
</Amounts>

I am looking to transform the above XML into the below structure
grouping payments with alike ones util there is a different payment
encountered. I also need to inculde a count of the like payments.

Output should look like this: (in the correct order found in the input
stream) I have had limited success with for-each but the problem lies
in the counting of the LIKE elements.

<Payments>
<Payment amount="5290" numpayments="2" />
<Payment amount="5300" numpayments="2" />
<Payment amount="5290" numpayments="3" />
<Payment amount="1000" numpayments="1" />
<Payment amount="5290" numpayments="1" />
<Payment amount="5300" numpayments="1" />
<Payment amount="5290" numpayments="2" />
<Payment amount="5300" numpayments="2" />
</Payments>

Any help would be greatly appreciated.
Jody Greening
jgreening#AT#cy ence#DOT#com

Jul 20 '05 #1
5 2175


Jody Greening wrote:
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 it from a
traditional programming point of view.

I have did quite a bit of searching but have found no answers to my
particular problem


Grouping is described here:
<http://www.jenitenniso n.com/xslt/grouping/index.html>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #2
I have already looked through those examples, but I did not find
anything that can answer my above question. Maybe I am not seeing it,
but there is no example there of what I am trying to accomplish.

I have figured out a way to lop and see if the current value is the
same as the previous, but I am still having the problem of counting
them.

Keep in mind that the outputs here are strictly for testing purposes.

<xsl:template match="Results/Stream[@name='Rents']/Amounts/Amount">
<xsl:for-each select="." >
<xsl:variable name="PreviousA mount"
select="precedi ng-sibling::Amount[position()=1]" />
<xsl:variable name="CurrentAm ount" select="." />
<xsl:choose>
<xsl:when test="scripts:C heckIfNull($Cur rentAmount, '0') =
scripts:CheckIf Null($PreviousA mount, '0')">
<TransPaymentIn fo>
<xsl:attribut e name="TransSubS cheduleId">
<xsl:text>Sam e as last one </xsl:text>
| Current Payment - <xsl:value-of
select="scripts :CheckIfNull($C urrentAmount, 'BLANK')" />
| Previous Payment - <xsl:value-of
select="scripts :CheckIfNull($P reviousAmount, 'BLANK')" />
</xsl:attribute>
</TransPaymentInf o>
</xsl:when>
<xsl:otherwis e>
<TransPaymentIn fo>
<xsl:attribut e name="TransSubS cheduleId">
<xsl:text>Diffe rent than last one </xsl:text>
| Current Payment - <xsl:value-of
select="scripts :CheckIfNull($C urrentAmount, 'BLANK')" />
| Previous Payment - <xsl:value-of
select="scripts :CheckIfNull($P reviousAmount, 'BLANK')" />
</xsl:attribute>
</TransPaymentInf o>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>

Jul 20 '05 #3
Hi,
I am looking to transform the above XML into the below structure
grouping payments with alike ones util there is a different payment
encountered. I also need to inculde a count of the like payments.


The following sample stylesheet produces the output you want.
<?xml version='1.0' encoding="ISO-8859-1"?>
<xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>
<xsl:template match="Amounts" >
<Payments>
<xsl:apply-templates select="Amount[position()= 1 or . !=preceding::Am ount[1]]"/>
</Payments>
</xsl:template>

<xsl:template match="Amount">
<xsl:variable name="count" select="count(f ollowing::Amoun t[. !=current()][1]/preceding::Amou nt) "/>
<xsl:variable name="cnt">
<xsl:if test="$count =0"><xsl:valu e-of select="count(. ./Amount)"/></xsl:if>
<xsl:if test="$count !=0"><xsl:valu e-of select="$count"/></xsl:if>
</xsl:variable>
<Payment amount="{.}" numpayments="{$ cnt - count(preceding ::Amount) }" />
</xsl:template>

</xsl:stylesheet>

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #4


Jody Greening wrote:
I have already looked through those examples, but I did not find
anything that can answer my above question. Maybe I am not seeing it,
but there is no example there of what I am trying to accomplish.


To group you set up a key, then you can run through the elements, all
explained here
<http://www.jenitenniso n.com/xslt/grouping/muenchian.html>
To count nodes you use the count function of XPath:
<?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" encoding="UTF-8" indent="yes" />

<xsl:key name="same-amount" match="Amount" use="." />

<xsl:template match="/">
<Payments>
<xsl:for-each select="Amounts/Amount[generate-id() =
generate-id(key('same-amount', .))]">
<Payment amount="{.}" numpayments="{c ount(key('same-amount', .))}" />
</xsl:for-each>
</Payments>
</xsl:template>

</xsl:stylesheet>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #5
This code (Joris's) works perfectly to organize and count elements in
the way that I explained above.

Thanks :-)

Jul 20 '05 #6

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

Similar topics

2
3193
by: Peter Gerstbach | last post by:
Hello, I need help on the new xsl:document element! I'm using XSLT version 1.1 to be able to use the <xsl:document> element, because I need more than 1 output files. I'm using Saxon 6.5.3 My problem is the following. My XML document is a OpenOffice-document. There are headers and text underneath. But unfortunately the text elements are not nested in the header elements. See this simple example:
3
1814
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>. My XSL keeps returning zero for the count. The results for the XML/XSLT files below should look like:- Contact # of Visits Account ----------------------------------------------------------
5
9553
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 where node ordering matters, and also the case where it doesn't, but perhaps that is an advanced topic). Ideally the method should be available to xpath expressions, so I think that creating new templates which compare nodes will not work (well,...
0
471
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 it from a traditional programming point of view. I have did quite a bit of searching but have found no answers to my particular problem... please read below XML for the problem I am having.
4
1832
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]<--
4
2028
by: Cathie | last post by:
Hi All, I am trying to get my style sheet to work. It works fine in IE but I can't get it to work in .net. Below is the function I use for transforming, where advancedOptionsFile is the path to the file containing an XML document required by the transformation, and xmlSimplified is the XML document to trasform. ---------------------------------------------------------------------------- -------------------------------
3
3323
by: shaun roe | last post by:
mild rant follows Working now for a couple of years with xslt and now xslt 2.0, does anyone else get the impression that xslt 2.0 somehow missed the point? Yes its got a fancy new data model (and thank goodness for the new grouping functions), but where are the functions which would really have made it useful without taxing the implementers? e.g trigonometry functions for SVG; square root /log function to do simple statistics? hex/ dec...
6
1257
by: navneet0909 | last post by:
hello, i m new to the enthralling world of xml,xslt so guys pls help. What will be the xsl file of the following.I have an xml input <root> <element name="sam" number="1"/> <element name="sam" number="2"/> <element name="sam" number="3"/> <element name="bob" number="5"/> <element name="bob" number="7"/>
2
22778
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 in the attribute of an element. Instead of using the <xsl:attribute> element, you can simply place the xpath in the attribute itself. The most common usage of this is in creating hyperlinks.
0
9387
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,...
0
10002
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
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,...
0
8822
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, 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...
1
7368
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 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...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
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...
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.