Subject: Re: [xsl] how to test if a child node have a specific name?|
From: "Chen Yi" <chen_yi36@xxxxxxxxxxx>
Date: Fri, 28 May 2004 21:30:17 +0800
Thanks for you advise. But I still have a question. I think the following expresstion is correct.
<xsl:if test="name(child::node())='NodeName'"> ... </xsl:if>
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: Re: [xsl] how to test if a child node have a specific name? Date: Fri, 28 May 2004 09:14:35 -0400
At 2004-05-28 20:52 +0800, Chen Yi wrote:I want to test if the first child node is a specific name "NodeName" node.I use the following expression.
<xsl:if test="child::node()=NodeName"> ... </xsl:if>
But it doesn't work.
(1) the first of the child nodes might not be an element (when people type XML they often put newline sequences at the end of start tags creating text nodes as the first child node)
(2) your second operand is evaluated as the text content of the first child name "NodeName", which isn't what you want.
Then I changed the expression to <xsl:if test="child::node()='NodeName'"> ... </xsl:if>
or <xsl:if test="name(child::node())='NodeName'"> ... </xsl:if>
They all seem work.
But they are not namespace-safe (even though you aren't yet using namespaces), and it seems you are lucky that the first child node is not a text node. And I'm surprised the first of those actually works since you are not testing the name of the node but rather you are testing the text content of the node.
I want to know which one is the correct one?
I suggest "none of the above" ... I would suggest the "correct" answer would be:
as this first looks at all children elements (ignoring child text nodes), filters out only the first one, and then filters that one as being an element with the name "NodeName". The resulting node set is empty if the element is not as desired, and non-empty if the element is as desired, resulting in the associated false/true boolean test. Using this style of check will allow you to migrate safely to namespace-qualified element types, as in:
I hope this helps.
-- 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