xsl-list
[Top] [All Lists]

Re: [xsl] Determining the context node

2006-04-12 22:59:21
Hi Allison,

<xsl:value-of select="name()"/> will print the name of the current node (if it has a name) to the output.

Also, the local-name() function returns just the local part, and namespace-uri() returns the namespace URI part.

Each of these functions take a node-set as an argument (and operate on just the first node in the set if there's more than one), but the default argument is the context node, so that writing name(.) is equivalent to name(), for example.

For debugging, you may also want to look at <xsl:message>, the content of which most processors print to stderr output.

As far as keeping track of the context node goes, I try to write my code in such a way as to make this as transparent as possible. Bite-sized template rules, for example, let you look up just a few lines to see what the match attribute says, which often gives you a good hint as to the context, as long as the pattern is not completely generic like *. But even then you at least know it's an element.

This is also one of the reasons why I try to avoid using xsl:call-template when context will be relevant. Instead, I use xsl:apply-templates with a particular mode name. Named templates don't give you any hints about the context. All you know is that the context is the same as the context of the corresponding xsl:call-template. But if you use a template rule instead (i.e. with a "match" attribute and invoked with xsl:apply-templates), then you can build in some documentation about your context without having to write any comments (which have the potential of getting out of sync).

The key things to remember about the current node:

1. Only two instructions change it: <xsl:for-each> and <xsl:apply-templates>
2. <xsl:call-template> does *not* change the context

I hope this helps. Once you've fully grasped context in XSLT, you'll be well on your way to mastery :-)

I'm afraid I can't address your question about xsl:copy, because I can't make sense of it, given the way you've stated it. Maybe an example would help.

Evan


Allison Bloodworth wrote:
Hi,
I sometimes get confused about what the context node is when I'm writing a
complicated XSL stylesheet. I'm wondering if someone can suggest an easy way
to determine what node I'm on (e.g. by using some code to print this info
out to my output)?
I've tried xsl:copy, xsl:copy-of, and xsl:value-of, but I don't want to know
the value of the element, I want to know the name of the element (e.g. for
<EventWebsite>www.berkeley.edu</EventWebsite> I want to know that I'm on
EventWebsite, not that the value is www.berkeley.edu). Also, because most of
my elements contain other elements instead of text, often doing xsl:copy
(which is more rational than using copy-of) doesn't get me anything. I
assume there's probably an easy XPath expression for this, but even after
doing quite a bit of research I can't find it.

Thanks for any suggestions on this!

Allison Bloodworth
Principal Administrative Analyst
Technology Program Office
University of California, Berkeley
(415) 377-8243
abloodworth(_at_)berkeley(_dot_)edu




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--