Results 1 to 4 of 4

Thread: testEvaluateAsBooleanNamespaces

  1. #1

    Default 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. #2
    Join Date
    Jul 2005
    Location
    Rotterdam, the Netherlands
    Posts
    1,562

    Default

    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
    Arjen Poutsma

    Spring Web Services Dev Lead
    Please read the FAQ

  3. #3

    Default

    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:


    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.

  4. #4

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •