RE: [xsl] Finding the current node type

Subject: RE: [xsl] Finding the current node type
From: "Naschke, Pete" <NaschkePS@xxxxxxxxx>
Date: Mon, 30 Jul 2007 17:50:04 -0500
I think I'm doing this exact same thing...actually, I'm in the process
of "hacking" my way through a 100+ page XML file...loaded to the gills
with attributes...

I'll post what I come up with...check out the following link in the mean
time...I think it deals with exactly what we're facing...I hope...

.NET and XML: XPath Queries

I'm going home...


-----Original Message-----
From: Mark Anderson [mailto:mark.anderson@xxxxxxxxxxxxxxxxxxx]
Sent: Monday, July 30, 2007 5:35 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Finding the current node type

Hi All

I'm trying to figure out a way to determine the "type" of the current
node in an nodeset.

I have a union of two dissimilar nodesets and I need to number the
combined set sequentially in my output. I also need to output details
from each node, but as they are completely different I need to process
each node type differently based on it's contents.

Here's a silly example

		<woofer>12 inch</woofer>
		<rms_power>70 watts</rms_power>
		<peak_power>95 watts</peak_power>
		<height>40 inches</height>
		<width>18 inches</width>
		<depth>18 inches</depth>
		<screen>15 inch</screen>
		<cpu>Intel Core Duo 2.3ghz</cpu>

So, I need to do something like:

<xsl:for-each select="speaker | laptop">
	<!-- Assign a sequential Number -->
	Item No: <xsl:value-of select="position()"/> <br />
	<!-- Now output item-specific details -->
		<xsl:when test="node = speaker">
			RMS Power: <xsl:value-of select="rms_power"/>
<br />
			Peak Power: <xsl:value-of select="peak_power"/>
<br />
		<xsl:when test="node = laptop">
			Disk Drive: <xsl:value-of select="disk"/> <br />
			CPU: <xsl:value-of select="cpu"/> <br />


So, what I need to figure out is the test in the when clause to see
which type of node I'm processing.

I could hack it with something like:

<xsl:when test="count(rms_power) &gt; 0">
	<!-- Must be a speaker -->
	RMS Power: <xsl:value-of select="rms_power"/>
	Peak Power: <xsl:value-of select="peak_power"/>

However, it's messy and I have to find unique child nodes for every

I'm sure there's a more elegant way

Can anyone help?



Current Thread