Re: Testing the type of the current node

Subject: Re: Testing the type of the current node
From: Steve Tinney <stinney@xxxxxxxxxxxxx>
Date: Fri, 24 Mar 2000 00:12:42 -0500
> I'm trying to determine the type of the current node and print
> out an appropriate message. 

I think (someone please prove me wrong) it is impossible to do this
directly.

What you can do, however, is this:

nodetype.xsl:
=============
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:template name="node-type">
  <xsl:param name="node" select="."/>
  <xsl:apply-templates mode="nodetype" select="$node"/>
</xsl:template>
<xsl:template mode="nodetype" match="*">element</xsl:template>
<xsl:template mode="nodetype" match="@*">attribute</xsl:template>
<xsl:template mode="nodetype" match="text()">text</xsl:template>
</xsl:stylesheet>

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

<xsl:import href="nodetype.xsl"/>

<xsl:template match="/"> 
  <xsl:for-each select="//node()|//@*">
    <xsl:variable name="node-type">
      <xsl:call-template name="node-type"/>
    </xsl:variable>
    <xsl:message>Node is of type: <xsl:value-of select="$node-type"
                 /></xsl:message>
  </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

test.xml:
=========
<test>
  <elem type="attr">hello</elem>
  world
</test>

output:
=======
$do-saxon2 test.xml test.xsl
Node is of type: element
Node is of type: text
Node is of type: element
Node is of type: attribute
Node is of type: text
Node is of type: text

In Real Life(TM), you can just test the string value in your xsl:choose
instead of testing the node type directly.

 Steve


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


Current Thread