Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 26th, 2005, 09:24 AM
Authorized User
 
Join Date: Jun 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding existing and non-existing attributes

OK, here is my dilemma. I need to calculate the sum of an attribute of a child node. That's easy enough, however, the reason i call this a dilemma rests in the fact that it required to add a default value if the child node is not present. Basically, Is there a way sum all of these values and if the node/attribute is not present for a given node then add a default value?

p.s. To add to the challenge, it must be compatable with a XSLT 1.0 processor. I do wish to apologize for this limitation. It is not my choice to use such an obsolete processor.

p.s.s. To anyone that can provide me any help, your time and help is invaluable.

 
Old July 26th, 2005, 09:46 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

perhaps you can do something like this:
sum of child attributes
+ default * (count of not present child nodes)
?

not easy to see what the xslt might be unless you can show your xml structure...
 
Old July 26th, 2005, 09:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This is the same as any other problem summing over a computed value (the classical example is summing over price*quantity; another example is summing the prices after removing a currency symbol).

The solutions break down into three categories:

(a) recursive solutions: walk the sequence of nodes in a step-by-step recursive scan, keeping track of the running total as you go

(b) two-pass solutions: compute the values to be totalled in the first pass over the data (e.g. in an extra attribute), total them using the sum() function in the second pass

(c) reuse an existing solution that's already been implemented: this problem is very easy do solve using Dimitre Novatchev's FXSL library, once you've worked out how to install this library.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 26th, 2005, 09:54 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Actually, for this example there's a fourth solution which might be simpler: sum the values for those nodes that have the attribute, then add N times the number of nodes that don't have the attribute, where N is the default value:

sum(item/@a) + count(item[not(@a)])*$default-value

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 27th, 2005, 01:46 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

A whimsical fifth solution. Use a validating parser with a schema where the default attribute value is specified. The sum all of the attributes.

Should that work Michael?

--

Joe (Microsoft MVP - XML)
 
Old July 27th, 2005, 04:02 PM
Authorized User
 
Join Date: Jun 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

All of your suggestion have helped me gain the greater picture! So thank you! For my purposes Micheal Kay's fourth suggestion will fit all of my neededs! Thanks again!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding existing Console/project ? chiefarchon BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 0 September 19th, 2008 07:51 AM
Problem with Adding a new row to an existing datag cesemj ASP.NET 2.0 Basics 5 December 6th, 2007 02:37 AM
Page 501/2 -Adding an existing file Nick Y BOOK: Ivor Horton's Beginning Visual C++ 2005 0 July 6th, 2006 03:28 AM
Adding rows, AND column(s) to existing datatable cliffd64 VB.NET 2002/2003 Basics 1 August 18th, 2005 06:50 AM
XML DOM :modifying the existing nodes attributes i sharmasourabh54 XML 1 February 14th, 2005 05:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.