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

  • testEvaluateAsBooleanNamespaces

    Hi,

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

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

    Cheers,
    -Ralph.

  • #2
    Yes . From the XPath spec: http://www.w3.org/TR/xpath#section-Boolean-Functions:

    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

    Comment


    • #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..

      Cheers,
      -Ralph.

      Comment


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

        Code:
        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.

        Cheers,
        -Ralph.

        Comment

        Working...
        X