Announcement Announcement Module
No announcement yet.
testEvaluateAsBooleanNamespaces Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • testEvaluateAsBooleanNamespaces


    AbstractXPathTemplateTestCase. testEvaluateAsBooleanNamespaces() from SWS-1.0.3 evaluates the following expression against the xml in namespaces.xml and asserts the result is true:

    <root xmlns="namespace1">
        <prefix:child xmlns:prefix="namespace2">
    This testcase does not fail if I replace the 1 with a 0, or any other non-empty string. Is this the expected behaviour?


  • #2
    Yes . From the XPath spec:

    The boolean function converts its argument to a boolean as follows:
    • a number is true if and only if it is neither positive or negative zero nor NaN
    • a node-set is true if and only if it is non-empty
    • a string is true if and only if its length is non-zero
    • an object of a type other than the four basic types is converted to a boolean in a way that is dependent on that type


    • #3
      Thanks Arjen, I just noticed your reply while writing down my own findings..

      Turns out this is the expected behaviour, it's just not very intuitive:
      While XPath expressions select nodes in the XML document, the XPath API allows the selected nodes to be coalesced into [...] other data types. [...] When a Boolean return type is requested, Boolean.TRUE is returned if one or more nodes were selected; otherwise, Boolean.FALSE is returned.
      I falsely assumed XPath 1.0 would recognize schema datatypes, which it doesn't (XPath 2.0 does, but there seems to be no open source implementation).

      Therefore, the literals "true" and "false" in a xs:boolean represent the boolean values True and False according to xml schema, but XPath 1.0 will evaluate both of them to True. The same goes for other datatypes as well: "12.78e-2" is a valid value for xs:double, but evaluates to Double.NaN.

      javax.xml.datatype provides converters between schema and java types, but only for date and time related stuff. The package summary mentions the mappings for schema built-in datatypes in JAXB, but I haven't yet figured out how to use those as lightweight converters ("evaluate this string as xs:boolean") without full un/marshalling.

      Maybe that test case really should contain "0" or "false", so people like me won't draw the wrong conclusions..



      • #4
        For the archive: XMLBeans contains easy to use converters between java and schema's built-in data types:

        Node n = jaxp13XPathTemplate.evaluateAsNode(expression, context);
        boolean b = XmlBoolean.Factory.parse(n).getBooleanValue();
        This results in b being True for values of "true" or "1", False for values of "false" or "0", and throws an exception otherwise.