0

I have a huge xml document. When I test with xpath = //requestHeader returned result is below:

  <requestHeader> 
      <!--Optional:-->  
      <req:clientCode xmlns:req="http://www.ibb.com/ws/requestHeader">?</req:clientCode>  
      <!--Optional:-->  
      <req:clientUsername xmlns:req="http://www.ibb.com/ws/requestHeader">CC</req:clientUsername>  
      <!--Optional:-->  
      <req:channel xmlns:req="http://www.ibb.com/ws/requestHeader">?</req:channel>  
      <!--Optional:-->  
    </requestHeader>

And more, I need to get clientUsername value. Unfortunately //requestHeader//req:clientUsername returns empty string. what is wrong with it?

You can test with: http://www.xpathtester.com/test

3
  • Have you specified the namespace context for req in you parser? Commented Sep 24, 2013 at 18:57
  • No, how can specify it? Commented Sep 24, 2013 at 18:59
  • Depends on the API to the XPath engine you are using, which you haven't specified. Commented Sep 24, 2013 at 21:31

2 Answers 2

1

Try specifying req in the namespaceContext:

XPath.setNamespaceContext

Sign up to request clarification or add additional context in comments.

Comments

0

put your namespace at the top the xml file like this:

<?xml version="1.0" encoding="UTF-8"?>
<requestHeader  xmlns:req="http://www.ibb.com/ws/requestHeader"> 
  <!--Optional:-->  
  <req:clientCode>?</req:clientCode>  
  <!--Optional:-->  
  <req:clientUsername>CC</req:clientUsername>  
  <!--Optional:-->  
  <req:channel>?</req:channel>  
  <!--Optional:-->  
</requestHeader>

now you can use xpath like so

 //requestHeader/req:clientUsername

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.