XalanJ2
  1. XalanJ2
  2. XALANJ-2549

XPath with default namespace - No result

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Not a Problem
    • Affects Version/s: 2.7.1
    • Fix Version/s: None
    • Component/s: Xalan
    • Security Level: No security risk; visible to anyone (Ordinary problems in Xalan projects. Anybody can view the issue.)
    • Labels:

      Description

      Hi,

      I am having difficulties to use XPath query on a XML dataset having its root element with an xmlns attribute with no prefix.

      In that specific case, the XPath query does not return the value. Since I have no prefix for the namespace I simply can get the value.
      It this a known issue or am I simply doing something wrong ?

      Here is my XML :

      default-ns.xml
      <?xml version="1.0" encoding="UTF-8"?>
      
      <report xmlns="http://www.mydomain.com/report">
          <account_number>123</account_number>
      </report>
      
          @Test
          public void xPathDefaultNS() throws XPathExpressionException {
              javax.xml.xpath.XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
              javax.xml.xpath.XPath xpath = factory.newXPath();
              javax.xml.xpath.XPathExpression expression = xpath.compile("/report/account_number");
              String result = expression.evaluate(new org.xml.sax.InputSource(this.getClass().getResourceAsStream("default-ns.xml")));
              Assert.assertTrue("123".equals(result));
          }
      

        Issue Links

          Activity

          Hide
          gareth reakes added a comment -

          Query rather than bug

          Show
          gareth reakes added a comment - Query rather than bug
          Hide
          gareth reakes added a comment -

          Hi Olivier,

          Its best if you ask questions on the mailing list rather than in Jira. As Joe said in the comment on 1030, you do still need to resolve namespaces. An element has a namespace even if there is no prefix. You will need to bind one yourself. Take a look in the mail archives and here:

          http://xml.apache.org/xalan-j/xpath_apis.html#namespacecontext

          For more information. If its still not clear then ask the list.

          Gareth

          Show
          gareth reakes added a comment - Hi Olivier, Its best if you ask questions on the mailing list rather than in Jira. As Joe said in the comment on 1030, you do still need to resolve namespaces. An element has a namespace even if there is no prefix. You will need to bind one yourself. Take a look in the mail archives and here: http://xml.apache.org/xalan-j/xpath_apis.html#namespacecontext For more information. If its still not clear then ask the list. Gareth
          Hide
          Olivier Roger added a comment -

          Link to XALANJ-1030 which describes the same issue

          Show
          Olivier Roger added a comment - Link to XALANJ-1030 which describes the same issue
          Hide
          Olivier Roger added a comment -

          I also tried other queries without success, like:

          1. javax.xml.xpath.XPathExpression expression = xpath.compile("/*[local-name()='report']/account_number");
          2. javax.xml.xpath.XPathExpression expression = xpath.compile("/*[local-name()='report' and namespace-uri()='http://www.mydomain.com/report']/account_number");

          Removing the xmlns attribute from the report tag allows me to find the correct result but is not my use case.
          Adding a prefix to the namespace and using it also allows me to find the correct result but is not my use case either.

          Show
          Olivier Roger added a comment - I also tried other queries without success, like: javax.xml.xpath.XPathExpression expression = xpath.compile("/* [local-name()='report'] /account_number"); javax.xml.xpath.XPathExpression expression = xpath.compile("/* [local-name()='report' and namespace-uri()='http://www.mydomain.com/report'] /account_number"); Removing the xmlns attribute from the report tag allows me to find the correct result but is not my use case. Adding a prefix to the namespace and using it also allows me to find the correct result but is not my use case either.

            People

            • Assignee:
              Unassigned
              Reporter:
              Olivier Roger
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development