Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 26th, 2005, 09:24 AM
Authorized User
 
Join Date: Jun 2005
Location: , , .
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.

Reply With Quote
  #2 (permalink)  
Old July 26th, 2005, 09:46 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
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...
Reply With Quote
  #3 (permalink)  
Old July 26th, 2005, 09:51 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
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
Reply With Quote
  #4 (permalink)  
Old July 26th, 2005, 09:54 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
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
Reply With Quote
  #5 (permalink)  
Old July 27th, 2005, 01:46 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #6 (permalink)  
Old July 27th, 2005, 04:02 PM
Authorized User
 
Join Date: Jun 2005
Location: , , .
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!

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:16 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.