Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select boolean-valued nodes in XPath?

Tags:

xpath

xsd

I have an XML document and associated schema that defines several attributes as having the xs:boolean type. The lexical values for xs:boolean are true, false, 1, and 0, so it seems that to correctly select attributes with a particular boolean value I'd have to write something like:

@attribute='true' or @attribute='1'

or

@attribute='false' or @attribute='0'

This seems verbose.

You might expect something like boolean(@attribute) to work, but the boolean function has different semantics.

Is there a better way? Is this something that a schema-aware processor would help with?

like image 683
John Avatar asked Jul 13 '09 00:07

John


1 Answers

In addition to the solutions proposed by Phil and Tomalak, I discovered that XPath 2.0 provides a few alternatives:

@attribute=('true','1')
string(@attribute) cast as xs:boolean

And finally, XPath 2.0 does provide schema-aware processing, which means that if everything is in alignment, you should be able to write:

data(@attribute)

But schema-aware processors seem hard to come by. The most popular seems to be the non-free commercial variant of saxon, which costs £300. So for now I'm using @attribute=('true','1').

like image 138
John Avatar answered Sep 26 '22 14:09

John