aspose file tools*
The moose likes XML and Related Technologies and the fly likes Determining whether attributes exist or not. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Determining whether attributes exist or not." Watch "Determining whether attributes exist or not." New topic
Author

Determining whether attributes exist or not.

Chris Bicnal
Ranch Hand

Joined: Aug 17, 2005
Posts: 78
    
    1
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

Joined: Apr 06, 2010
Posts: 4387
    
    8

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Aug 17, 2005
Posts: 78
    
    1
Thanks again Paul, using the not() function did exactly what I wanted!
Chris Bicnal
Ranch Hand

Joined: Aug 17, 2005
Posts: 78
    
    1
Thanks Matthew too! :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Determining whether attributes exist or not.