[XSLT] determining what node() finds?

M

Mike Kamermans

Is there a way to determine what a node() find returns? For instance, if
I have a snip of XSLT that looks like:

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

Is there a way to, for each node found, generate what kind of node it
found, like <some_element> or <#PCDATA> or something that indicates the
type of element it found?

(sort of like an <xsl:value-of select="node-type()"/>, except that
doesn't exist of course)


- Mike Kamermans
 
R

Richard Tobin

Mike Kamermans said:
Is there a way to, for each node found, generate what kind of node it
found, like <some_element> or <#PCDATA> or something that indicates the
type of element it found?

You could match it against some templates to find the type, and store
the result in a variable:

<xsl:template match="foo">
<xsl:for-each select="node()">
<xsl:variable name="type">
<xsl:apply-templates mode="type" select="."/>
</xsl:variable>
type is: <xsl:value-of select="$type"/>
</xsl:for-each>
</xsl:template>

<xsl:template match="*" mode="type">
<xsl:text>element</xsl:text>
</xsl:template>

<xsl:template match="text()" mode="type">
<xsl:text>text</xsl:text>
</xsl:template>

<xsl:template match="comment()" mode="type">
<xsl:text>comment</xsl:text>
</xsl:template>

<xsl:template match="processing-instruction()" mode="type">
<xsl:text>pi</xsl:text>
</xsl:template>

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top