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 17th, 2008, 03:39 AM
Registered User
 
Join Date: Jul 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting the position of a node in the original XML

Hi,

While writing an XSL, is there any way of getting the position of the node in the original document?

By position I'm referring to char offset from the beginning of the file or any similar information like line and column number. I would like to embed this information in the output if possible.

Thanks in advance.
 
Old July 17th, 2008, 04:08 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

In "straight" XSLT no; the XML is presented to the XSL processor as an infoset, a tree of nodes, and not as a text document. However some processors have extensions that allow this information to come through. I know Saxon has this ability but I can't remember the setting that allows it.

--

Joe (Microsoft MVP - XML)
 
Old July 17th, 2008, 04:48 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There's no standard way of doing this.

In Saxon, provided you use the -l switch or equivalent when parsing, you can access the line and column using the saxon:line-number() and saxon:column-number() extensions.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 17th, 2008, 06:05 AM
Registered User
 
Join Date: Jul 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the quick response, guys.

Sorry for not mentioning this earlier, but I'm using the transformation as part of a Java code and not executing it externally.

Let me try attacking this from a different angle: is there a way to uniquely identify the original XML element and use it in the output? In that case I can do some post-processing to get the location of the original node.


 
Old July 17th, 2008, 06:12 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Saxon is a Java based XSLT processor, so I don't see how that should present you with a problem.

/- Sam Judson : Wrox Technical Editor -/
 
Old July 17th, 2008, 06:18 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>Sorry for not mentioning this earlier, but I'm using the transformation as part of a Java code and not executing it externally.

There's an equivalent of the Saxon -l flag in the Java API: use setAttribute(LINE_NUMBERING) on the TransformerFactory. See http://www.saxonica.com/documentatio...formation.html
But again this is Saxon-specific.

>Let me try attacking this from a different angle: is there a way to uniquely identify the original XML element and use it in the output? In that case I can do some post-processing to get the location of the original node.

You might be better off preprocessing - put a SAX filter in the pipeline, and use it to add a lineNumber attribute to every element, which is then available to the XSLT code. This approach would work with any JAXP processor.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 17th, 2008, 07:31 AM
Registered User
 
Join Date: Jul 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again, great suggestions.


 
Old July 17th, 2008, 09:13 AM
Registered User
 
Join Date: Jul 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

BTW, any way this can be done in Xalan?

 
Old July 17th, 2008, 09:31 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

By the looks of the Xalan documentation they seem to provide a very similar function.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
xml tag position change rajesh_css XSLT 1 November 3rd, 2008 04:25 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
Position of a node outside current context QuickSilver002 XSLT 2 April 19th, 2007 02:07 PM
Original form SaveAs new doc w/o original VBA ChuckTremain Word VBA 0 February 8th, 2007 02:58 PM
xpath: position parent of current node orion83 XSLT 1 September 5th, 2005 07:20 AM





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