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 June 27th, 2007, 11:44 AM
Registered User
 
Join Date: Aug 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Counting leaf nodes by transforming their value

I've been googling for help on this one and found the dawson resource (http://www.dpawson.co.uk/xsl/sect2/sect21.html) but there's a lot there and I'm not even really sure how to properly word my problem.

I want to take an arbitrary XML file (so immediately I'm thinking 'start with identity transform') and for every leaf node, set its text value equal to an ever increasing number. Ideally they should represent an iterative count of which leaf it is, but as long as they are unique, I could live without it.

I'm a bit stuck on two aspects. First, how to modify the identity transform to say "Copy everything other than the text, which I would like to change to X" and second, how to maintain that X throughout my transform so that it is unique/increasing.

(For the curious I've got a rather unwieldy piece of legacy code (not XSL) that reads in my XML and processes it twelve ways to Sunday. There's a bug somewhere that's causing wrong data to be processed. Since a large portion of my incoming data is identical to other parts, I'm trying to come up with some sort of way to map my data through the system so that when I look at the output I can say "Aha, leaf 12 ended up here when it should have been leaf 97.")

Thanks!


 
Old June 27th, 2007, 12:24 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Use the standard identity template and add

<xsl:template match="*[not(*)]">
  <xsl:copy>
  <xsl:number level="any" match="*"/>
  </xsl:copy>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 27th, 2007, 07:08 PM
Registered User
 
Join Date: Aug 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael! Unfortunately I'm not sure that's giving me the results I need. For instance, from this sample file:

<A>
 <B>b1</B>
 <C>
  <D>d1</D>
    <D>d2</D>
  <D>
   <E>e1</E>
   <E/>
  </D>
 </C>
</A>

I'm hoping to generate something like this:
<A>
 <B>1</B>
 <C>
  <D>2</D>
    <D>3</D>
  <D>
   <E>4</E>
   <E>5</E>
  </D>
 </C>
</A>

So that each value that had no children ends up assigned a unique number (even the <E/> that originally started with no textual value).

When I run your transform on the above file I get:

<?xml version="1.0"?>
<A>
 <B>1</B>
 <C>
  <D>1</D>
  <D>2</D>
  <D>
   <E>1</E>
   <E>2</E>
  </D>
 </C>
</A>

which is properly identified all the leaves (even the empty one), but it's repeating numbers for each level (so D is the #1 child of C, E is the #1 child of D, etc...)



 
Old June 29th, 2007, 04:58 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Actually the code as given should give a compile error - I wrote "match=" when I meant "count=". (If your processor doesn't report this error, it has a bug). And to reproduce your desired output more closely, you need count="*[not(*)]"

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting Nodes That Are Not Empty tommyready XSLT 2 September 4th, 2007 09:03 AM
Count empty leaf JLacerte XSLT 0 July 2nd, 2007 10:09 AM
Counting nodes on output bonekrusher XSLT 6 February 26th, 2007 08:19 PM
Using axis instead of count? Counting child nodes. dep XSLT 1 January 17th, 2007 11:27 AM
counting child nodes Tomi XSLT 1 September 6th, 2006 03:26 AM





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