Re: [xsl] how to test if a child node have a specific name?

Subject: Re: [xsl] how to test if a child node have a specific name?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 28 May 2004 10:03:23 -0400
At 2004-05-28 21:30 +0800, Chen Yi wrote:
Thanks for you advise.
But I still have a question.
I think the following expresstion is correct.
<xsl:if test="name(child::node()[1])='NodeName'">
  ...
</xsl:if>

And as I am a new beginner of XSLT, I am confused what's the difference between the term Node and Element, Can you kindly explain it to me ? Thanks.

There are four different types of nodes found along the child:: axis - elements, comments, processing instructions, and text nodes. When you use "node()" you are checking *all* four kinds of node along the given axis. When you use "*" you are checking only elements along the given axis.


And, yes, it happens to be true that your expression works regarding the node name, but I did qualify my suggestion by saying that it works in this limited situation where your element types are not namespace-qualified ... you asked for the "correct" expression, and I maintain that the "correct" expression is to use the self:: axis and not to use the name() function.

When I teach XSLT I underscore that the name() function is suitable for exposition but not suitable for node checking.

I maintain that your expression above is not sufficient in the general case because of possible text nodes at the start of your child nodes and of possible future use of namespace-qualified element types, therefore I will stick to:

test="child::*[1][self::NodeName]"

as being the "correct" way to do what you have requested.

I hope this helps.

......................... Ken


-- Public courses: Spring 2004 world tour of hands-on XSL instruction Next: 3-day XSLT/XPath; 2-day XSL-FO - Birmingham, UK June 14,2004

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 Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread