[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] RE: determining position of a document included using
Bryan:
At 11:28 AM 2/13/02, you wrote: I run stylesheet1 against document1, document1 has a structure sort of like in all the classic "xinclude with xslt" examples: i.e <doc> <file href="document2.xml"/> <file href="document3.xml"/> </doc> so when I get to the file node I use the document function as above to load the document referred to by the href attribute, then I apply-templates against that document, however I found in the document I loaded at one point I would really have liked to know something about the position of the file node which had the attribute href from which I knew what document to load! :) You still haven't said what you mean by "position". This is murky because the position() function does not return the position of a node in any absolute sense (that is, its position relative to other nodes in the document), but rather its position in the processing flow (as I said, its position in the current node list). So <xsl:template match="file"> <xsl:value-of select="position()"/> </xsl:template> will get you different things depending on how the nodes were selected. For example, looking at your source, the element <file href="document3.xml"/>, when selected by <xsl:apply-templates/> <= position() returns 4 unless you're stripping space inside 'doc' in which case it's 2 <xsl:apply-templates select="file"/> <= position() returns 2 This misunderstanding of position() is a common source of errors for new users of the language. I can actually see where that might get very useful to know, or the knowledge of what file's parent is, in cases where I load a document into the middle of another one, which I do often, and I suppose others do as well. Well, name(..) will tell you the element type name of the parent, generate-id(..) will return a string unique to that parent for the run of the stylesheet, <xsl:number level="single"/> will tell you whether the file is the first, fourth or fifteenth file inside its parent, <xsl:number level="any" from="doc"/> will tell you whether it's first, fifteenth etc. within its 'doc' ancestor (wherever that is), etc. etc. Any of this information could be useful depending on what you need it for. Obviously that could be done by passing a parameter but what if, as in this case, I need to apply-templates five or six times before I get to the actual template I need the info in. Yes, there are undoubtedly better ways to figure out what you want, since XPath and XSLT provide a multitude of approaches to locating nodes in the tree. But as it is, your question doesn't have a single answer since you haven't defined your terms. It must be the coffee: maybe some chocolate milk is in order. Cheers, Wendell
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|