Meaningless Drivel is fun!*
The moose likes XML and Related Technologies and the fly likes How to Add a Line Counter for Each XML Element in XSL? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to Add a Line Counter for Each XML Element in XSL?" Watch "How to Add a Line Counter for Each XML Element in XSL?" New topic
Author

How to Add a Line Counter for Each XML Element in XSL?

Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
I have converted an XML file to a text file using XSL. The output is the value of each XML element. And I put a line break after each output value:

How do I put a line counter for each XML element, so that the output looks like

My XSL file looks like:

Thanks in advance.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You pass the line numbers as another parameter to the "clip" template:


(By the way I hope that isn't medical information about a real person. Please edit your post as soon as possible if it is.)
Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
I made up the data for a medical record. There is no patient name shown in my post.

I have tried what you suggested. But nothing happens - a number does not appear in front of the value of each XML element. The output still looks like:

The format that is desired to be displyed is 1. element count number + 2. caret symbol + 3. the value of the XML element; for example:

The XSL code now looks like (a counter number is not inserted in the front of each line of the output):

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You do have to change the "clip" template to output the sequence number. Sorry, I thought that would be obvious so I didn't provide the full solution.
Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
It is not obvious to me. I am new to all XML related technologies. For adding a numbering, I am thinking along the line of:
but, I still have difficult time to figure it out.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

No, no, no. The "clip" template should simply copy the sequence number parameter to the output.

There aren't any XSLT constructs which can work on nodes in the output. And there isn't anything in your input document which can be examined to produce a numbering system (at least not from what I can see from your input document). So the simplest thing is to produce your own sequence numbers, which is anyway trivially simple to do in the sample of XSLT which you posted. You already did half of that by choosing the sequence numbers. The second half is to use them.
Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
I got it. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Add a Line Counter for Each XML Element in XSL?
 
Similar Threads
XSLT value-of parameter not works as xml value
XSLT: escaping " in output text
Newbie Question---Help?
xsl template question
An XSLT that's just a little more Specific