Commons JXPath
  1. Commons JXPath
  2. JXPATH-14

JXPathContext.getValue(String xpath) does not through JXPathException when it should

    Details

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

      Operating System: Linux
      Platform: All

      Description

      It seems that for certain XPaths, JXPathContext.getValue(String xpath) does not
      through a JXPathException when the expression does not match any nodes, even if
      lenient is set to false, and returns null instead.

      An example: if the XPath is
      /root/foo[@name='attrName']/*

      for XML document
      <root><foo name="attrName"><value>blahblah</value></foo></root>

      Then JXPathContext.newContext( dom ).getValue() returns "blahblah".

      If the node <foo name="attrName">...</foo> does not exist, then getValue()
      returns null, instead of throwin a JXPathException.

      However, if the expression is
      /root/foo[@name='attrName'] ( lacking the ending /* )
      then getValue() does throw a JXPathException if the node does not exist; and
      the correct value if the node does exist.

      The fact that expression "/root/foo[@name='attrName']/*" works when the node
      exists leads me to believe it is a valid xpath expression?

      Tracing through the JXPath code a bit, in
      JXPathContextReferenceImpl.getValue(xpath, expr), line 287,
      expr.computeValue(getEvalContext()) is returning null for the case above where
      the node doesn't exist. Since null is not an instanceof EvalContext or
      NodePointer, it is just returned as-is.

      The DOM parser being used is Apache Xerces.

      Thanks,
      Matias

        Activity

        Hide
        Dmitri Plotnikov added a comment -

        I added the check for null. Since an expression can sometimes return null
        legitimately, I also added a check for the type of expression (Path).

        Show
        Dmitri Plotnikov added a comment - I added the check for null. Since an expression can sometimes return null legitimately, I also added a check for the type of expression (Path).

          People

          • Assignee:
            Unassigned
            Reporter:
            Matias Pelenur
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development