[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Determining the context node

Subject: Re: Determining the context node
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 12 Apr 2006 20:46:21 -0400
allison holman
At 2006-04-12 17:15 -0700, Allison Bloodworth wrote:
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)?

My answer depends on why you are asking for the name of the current node.


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).

Fine, but do you need to know the name in order to expose it, or in order to selective logic?


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.

<xsl:copy> will add the current node to the result tree.


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.

I tell my students that the proper way to do a "what am I?" test for the purposes of logic is to address the node you are looking for on the self:: axis ... if the node is there, then you are that kind of node. For your example above, you would use:


  <xsl:if test="self::EventWebsite">
    ...true when the current node is EventWebsite in no namespace...

This is a namespace-safe test, such that if you needed to test "figure" in the namespace "urn:x-allison", you would use:

  <xsl:if test="self::b:figure" xmlns:b="urn:x-allison">
    ...true when the current node is {urn:x-allison}:figure...

That test will work even if the source tree node is:

<book:figure xmlns:book="urn:x-allison">

The self:: axis is not appropriate for attributes, namespaces or the root node, so I use the union operator to test for those.

Now, if you just need to expose the name of the current node, use node() in XPath 1.0 and node-name() in XPath 2.0.

But it is a commonly used but incorrect way when people test name()='b:figure' because this is not namespace-safe and would not test true for the element above.

I hope this helps.

. . . . . . . . . . Ken

--
Registration open for XSLT/XSL-FO training: Wash.,DC 2006-06-12/16
Also for XML/XSLT/XSL-FO training:Birmingham,England 2006-05-22/25
Also for XSLT/XSL-FO training:    Copenhagen,Denmark 2006-05-08/11
World-wide on-site corporate, govt. & user group XML/XSL training.
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Aug'05  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.