Commons JXPath
  1. Commons JXPath
  2. JXPATH-83

JXpath automatically trims string values

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.2 Final
    • Fix Version/s: 1.3
    • Labels:
      None

      Description

      When an xml contains a value with leading or trailing spaces, JXPath trims this value.
      example: <value> 12324 56</value> is retrieved by JXPath as : '1234 56' while I expect ' 1234 56'.

        Activity

        Wim Biesemans created issue -
        Hide
        Matt Benson added a comment -

        For reasons of backward compatibility it is inadvisable to change the default behavior here. We'll call trim-by-default an application implementation feature of JXPath. However, the XML spec refers to the special xml:space attribute, which can be set to "preserve" to hint that whitespace should be preserved. svn HEAD (rev. 538698) contains support for this attribute, about which you can read more at http://www.w3.org/TR/REC-xml/#sec-white-space .

        Show
        Matt Benson added a comment - For reasons of backward compatibility it is inadvisable to change the default behavior here. We'll call trim-by-default an application implementation feature of JXPath. However, the XML spec refers to the special xml:space attribute, which can be set to "preserve" to hint that whitespace should be preserved. svn HEAD (rev. 538698) contains support for this attribute, about which you can read more at http://www.w3.org/TR/REC-xml/#sec-white-space .
        Matt Benson made changes -
        Field Original Value New Value
        Resolution Fixed [ 1 ]
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 1.3 [ 12312253 ]
        Hide
        Michele Vivoda added a comment -

        I think this is a quite important issue, in my opinion there should be a way to force the JXPath processor to return whitespace, it doesn't matter so much if is not the default behavior.

        I believe the special xml:space attribute applies only to validating processors, and in any case its default value is already 'preserve'.

        I think also that this and maybe other issues could be solved adding a generic configuration mechanism with properties, like for a SAX parser.

        Show
        Michele Vivoda added a comment - I think this is a quite important issue, in my opinion there should be a way to force the JXPath processor to return whitespace, it doesn't matter so much if is not the default behavior. I believe the special xml:space attribute applies only to validating processors, and in any case its default value is already 'preserve'. I think also that this and maybe other issues could be solved adding a generic configuration mechanism with properties, like for a SAX parser.
        Hide
        Michele Vivoda added a comment -

        A workaround for DOM based models is to use selectSingleNode and
        retrieve the string value using something like getStringValueOfNode(node);
        When using selectNodes instead return an empty string when iterator is empty otherwise
        return getStringValueOfNode(node) where

        {node}

        is the first node of the list.

        static String getStringValueOfNode(final Node n)

        { final StringBuffer buf = new StringBuffer(); toStringAppend(n, buf); return buf.toString(); }

        private static void toStringAppend(final Node n, final StringBuffer buf)
        {
        final String value = n.getNodeValue();
        if (value == null)
        {
        Node current= n.getFirstChild();
        while(current!=null)

        { toStringAppend(current, buf); current = current.getNextSibling(); }

        }
        else if (!(n instanceof Comment)) buf.append(value);
        }

        Show
        Michele Vivoda added a comment - A workaround for DOM based models is to use selectSingleNode and retrieve the string value using something like getStringValueOfNode(node); When using selectNodes instead return an empty string when iterator is empty otherwise return getStringValueOfNode(node) where {node} is the first node of the list. static String getStringValueOfNode(final Node n) { final StringBuffer buf = new StringBuffer(); toStringAppend(n, buf); return buf.toString(); } private static void toStringAppend(final Node n, final StringBuffer buf) { final String value = n.getNodeValue(); if (value == null) { Node current= n.getFirstChild(); while(current!=null) { toStringAppend(current, buf); current = current.getNextSibling(); } } else if (!(n instanceof Comment)) buf.append(value); }

          People

          • Assignee:
            Unassigned
            Reporter:
            Wim Biesemans
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development