choosing element with no attribute

A

anitawa

Hi,

Im in a dilemma and need some help.
I have an xml like this:

<animal species="dog">
<color>brown</color>
</animal>
<animal species="cat">
<color>black</color>
</animal>
<animal>
<color>no color</color>
</animal>

I want to be able to select the color with no attribute. Something
like this:
<xsl:choose>
<xsl:when test="./@species='dog'">
<value-of select=".[@species='dog']//color"/>
</xsl>
<xsl:when test="./@species='cat'">
<value-of select=".[@species='cat']//color"/>
</xsl>
....
....
</xsl:choose>

How would i get the color element under <animal> with no attribute??
 
R

Richard Tobin

anitawa said:
I want to be able to select the color with no attribute. Something
like this:
<xsl:choose>
<xsl:when test="./@species='dog'">

You can just write "@species='dog'"
<value-of select=".[@species='dog']//color"/>

You already know that . has @species = 'dog', so you can just write
".//color" here. Or if, as in your example, <color> is the immediate
</xsl>
<xsl:when test="./@species='cat'">
<value-of select=".[@species='cat']//color"/>
</xsl>
....
....
</xsl:choose>

How would i get the color element under <animal> with no attribute??

<xsl:when test="not(@species)">
<value-of select=".//color"/>

-- Richard
 
D

David Carlisle

anitawa said:
I want to be able to select the color with no attribute. Something
like this:
<xsl:choose>
<xsl:when test="./@species='dog'">
the ./ is doing nothing there: you could do
@species='dog'
<value-of select=".[@species='dog']//color"/>
..[ is a syntax error in XSLT1 (it's allowed in xpath2) you just want
select="color" (you must already be on the animal element for teh
attribute test to work so you know color is the child)
</xsl>
<xsl:when test="./@species='cat'">
<value-of select=".[@species='cat']//color"/>
as above this is a syntax error in xslt1
</xsl>
....
....
</xsl:choose>

How would i get the color element under <animal> with no attribute??
select="animal[not(@*)]/color"

If you are using xslt2 then what I think you want is to replace the
xsl:choose by

<xsl:value-of
select="(animal[@species='dog'],animal[@species='cat'],animal)[1]/color"/>

ie select the color of the dog if there is one else the cat else any
other animal.
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top