choosing element with no attribute

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anitawa

    #1

    choosing element with no attribute

    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 <animalwith no attribute??

  • Richard Tobin

    #2
    Re: choosing element with no attribute

    In article <1181084249.030 382.176870@q66g 2000hsg.googleg roups.com>,
    anitawa <anitawa@gmail. comwrote:
    >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, <coloris the immediate
    child of <animal>, you can just write "color".
    </xsl>
    <xsl:when test="./@species='cat'" >
    <value-of select=".[@species='cat']//color"/>
    </xsl>
    ....
    ....
    ></xsl:choose>
    >
    >How would i get the color element under <animalwith no attribute??
    <xsl:when test="not(@spec ies)">
    <value-of select=".//color"/>

    -- Richard
    --
    "Considerat ion shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.

    Comment

    • David Carlisle

      #3
      Re: choosing element with no attribute

      anitawa wrote:
      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 <animalwith 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.


      --

      Comment

      Working...