Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Determining whether attributes exist or not.

 
Chris Bicnal
Ranch Hand
Posts: 96
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm here with another newbie question - how can I easily tell whether attributes exist or not?

Given the following XML snippet....
I have certain elements with the Complexity, Impact etc... attributes and others without. For the ones without attributes I want to insert pre-defined default values into my output CSV file. For the ones with attributes I want to use the values from the XML document.

I have tried comparing the value of @Attribute to '' in an xsl:if which doesn't seem to work (it always returns false regardless of whether @Attribute exists or not) and I tried testing for the attribute itself, like so...

...but this always returns true if the Complexity attribute exists - I want to be able to negate that test to return true if it doesn't exist.

Any help with this would be much appreciated!

Chris
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Bicnal wrote:
...but this always returns true if the Complexity attribute exists - I want to be able to negate that test to return true if it doesn't exist.

Have you tried just using the not() function to invert the condition, then?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More precisely, "@Attribute" returns a node list. When coerced to boolean (as in your <xsl:if test=""> example) the node list becomes true if it contains any nodes and false if not. Passing it to the not() function, as Matthew suggests, also coerces it to boolean, so therefore applying not() results in false if the node list contains any nodes and true if it is empty.
 
Chris Bicnal
Ranch Hand
Posts: 96
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again Paul, using the not() function did exactly what I wanted!
 
Chris Bicnal
Ranch Hand
Posts: 96
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew too! :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic