RE: [xsl] how to test for the element type of the current node

Subject: RE: [xsl] how to test for the element type of the current node
From: "Jim Fuller" <jim.fuller@xxxxxxxxxxxxxxxxxx>
Date: Wed, 2 Jul 2003 09:15:04 +0100


> -----Original Message-----
> From: Godmar Back [mailto:gback@xxxxxxxxxxxx]
> Sent: 02 July 2003 08:51
> To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] how to test for the element type of the current node
> 
> 
> 
> Hi,
> 
> what expression do I use if I need to test the type of the 
> current node
> in a for-each loop?
> 
> Say I have a DTD that has	
> 
> 	<!ELEMENT A (B|C)*>
> 
> and I want to write
> 
> <xsl:template match="A">
>     <xsl:for-each select="*">
> 	<xsl:choose>
> 	    <xsl:when test="if this node is a B"> <--- this is 
> what I can't figure out
> 		apply-templates for B
> 
> 		...
> 	    <xsl:otherwise>
> 		do something else for C
>     ...
> 
> 
> I tried ".=B" inside the test, which doesn't work, I tried just "B", 
> I tried "boolean(B)", nothing works.
> 

I have rejigged the test a little so we are not talking in terms of yukky dtd's

xslt file
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
>

<xsl:template match="test">

<xsl:for-each select="node()">

<xsl:choose>
	<xsl:when test="name()='a'">
	I have matched an a element
	</xsl:when>
  
	<xsl:otherwise>
	 must be something else
	</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


xml file
<?xml version="1.0" ?>
<test>
<a>1</a>
<b>2</b>
<c>3</c>
</test>

basically you are testing on the name of the element, so you must use the name() function...remember that if you are using namespaces ( xmlns ) that u may have to use local-name(). Also this may not be what you want to do, you may want to test the node identity, not the name of the node.

well, gl jim fuller


> Note that I believe that using <xsl:template match="B"> won't 
> solve my problem
> because I need to do something specific that I only need to if the 
> B element is nested inside  the A element.
> 
> This seems such an elementary thing to do (!?), but maybe I 
> still don't
> understand XSL well enough to see an alternative.
> 
> Thanks!
> 
> 	- Godmar
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 
> 
This e-mail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you are not the intended recipient please contact the sender immediately. Any disclosure, copying, distribution or any other use of this communication is strictly prohibitedand may be unlawful. Stuart Lawrence Marketing Communications Limited reserves the right to monitor and intercept communications for unlawful business purposes.

This also confirms that this message has been swept for viruses, although Stuart Lawrence Marketing Communications Limited accepts no responsibility for any loss or damage resulting directly or indirectly from the use of this email or contents.

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


Current Thread