Announcement Announcement Module
Collapse
No announcement yet.
JaxenXPathTemplate is eating the tags Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JaxenXPathTemplate is eating the tags

    I'm trying to find out why by XPath expressions that work in other tools are not working with the JaxenXPathTemplate.
    I created a simple test case:
    Code:
      @Test public void testBlahBlahBlah()  {
        String str =
            "<soap-env:Envelope xmlns:soap-env=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                     "   <soap-env:Header/>\n" +
            "   <soap-env:Body xmlns:soap-env=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
            "      <soap-env:Fault>\n" +
            "         <faultcode>soap-env:Server</faultcode>\n" +
            "         <faultstring>Exception Code: 000. Executing Facade: Transferencias, Method: doSomething</faultstring>\n" +
            "         <faultactor>http://eric_estarda</faultactor>\n" +
            "         <detail>\n" +
            "            <prefixRigel0:Something xmlns:prefixRigel0=\"http://www.blah.com/stuff\">\n" +
            "               <kcMensaje>\n" +
            "                  <codigo>MD_0033</codigo>\n" +
            "                  <descripcion>THE CONTRACT MUST BE SPECIFIED</descripcion>\n" +
            "               </kcMensaje>\n" +
            "            </prefixRigel0:Something>\n" +
            "         </detail>\n" +
            "      </soap-env:Fault>\n" +
            "   </soap-env:Body>\n"+
            "</soap-env:Envelope>";
    
        JaxenXPathTemplate xpathTemplate = new JaxenXPathTemplate();
    
         System.out.println("["+xpathTemplate.evaluateAsString( ".", new StringSource( str ) )+"]");
      }
    What I get is this:
    Code:
    [   
       
          
             soap-env:Server
             Exception Code: 000. Executing Facade: Transferencias, Method: doSomething
             http://eric_estarda
             
                
                   
                      MD_0033
                      THE CONTRACT MUST BE SPECIFIED
                   
                
             
          
       
    ]
    It's no wonder that my XPath expressions are failing. All of the tags have been eaten. Why?

    I'm using
    revision.springframework = 3.1.2
    revision.spring-int = 2.1.3
    revision.spring-ws = 2.1.0

  • #2
    I found out the answer to my question.
    The root of my confusion was that I was not see in the same results when using the XPathTemplate rather than a XPath path builder to such as one at Toolz (http://xpath.online-toolz.com/tools/xpath-editor.php).
    I didn't realize that the '.' expression when evaluated by the tool was showing my the node, where as the XPathTemplate gave be exactly what I asked for which was a string.

    I stepped through the code with the JaxenXpathTemplate to see why the results look so odd, and it boils down to how the Jaxen DocumentNavigator builds the response string. I expect that Jaxp13XPathTemplate gives the same results for the same reason.
    Here's the Jaxen code that answered my question, and reminded me about the basics of XML handling:
    Code:
        /**
         * Construct a node's string value recursively.
         *
         * @param node the current node
         * @param buffer the buffer for building the text
         * @return the buffer passed as a parameter (for convenience)
         */
        private StringBuffer getStringValue (Node node, StringBuffer buffer)
        {
            if (isText(node)) {
                buffer.append(node.getNodeValue());
            } else {
                NodeList children = node.getChildNodes();
                int length = children.getLength();
                for (int i = 0; i < length; i++) {
                    getStringValue(children.item(i), buffer);
                }
            }
            return buffer;
        }

    Comment

    Working...
    X