RE: [xsl] determining position of a document included using document function

Subject: RE: [xsl] determining position of a document included using document function
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 13 Feb 2002 13:03:49 -0500
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


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread