What is the valid XPATH to validate the node

霸气de小男生 提交于 2020-01-25 19:59:32

问题


Here is the xml

<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:processRequestResponse xmlns:ns="http://service.soap.oneflexi.com"><ns:return>
<?xml version="1.0" encoding="UTF-8"?> 
<ItemRs language="SG" currency="SGD"> 
  <Items>
 <Item>
   <CategoryCode />
    <CategoryDescription />
   <ItemCode>356</ItemCode>
    <ItemDescription>20% offer, Latest model</ItemDescription>
   <Quantity />
    <UnitPrice>24560</UnitPrice>
    <ItemBigImagesURL>http://goo.gl/klCGG4</ItemBigImagesURL>
    <ItemContent>Front Loading Washing Machine 6.5Kg Capacity 6 Motion Direct Drive</ItemContent>
   <ErrorCode />
    <ErrorMessage />
    <Beaconid>2499</Beaconid>
  </Item>
  </Items>
 <MID />
 </ItemRs>
</ns:return>
</ns:processRequestResponse>
</soapenv:Body>
</soapenv:Envelope>

I want to validate upto the path ItemRs tag

 <ItemRs language="SG" currency="SGD">

My java code is

Node result = (Node)xPath.evaluate(" Soapenv:Envelope/Soapenv:Body/ns:processRequestResponse/ns:return/ItemRs[language=\"SG\"]", doc, XPathConstants.NODE);

But the above code doesn't work because i think the path to get to the node is wrong. Please any help would be appreciated and Thanks in advance.

UPDATE

After running i get this error

Namespace with prefix 'ns' has not been declared.

What does it mean


回答1:


You need to define a mapping from namespace prefixes to URLs and add these to the context. You do this by implementing the NamespaceContext interface.

public class MyNamespaceContext implements NamespaceContext {

    private final Map<String, String> PREFIX_MAP = new HashMap<String, String>();

    public SimpleNamespaceContext(final Map<String, String> prefixMap) {
        PREFIX_MAP.putAll(prefixMap);       
    }

    public String getNamespaceURI(String prefix) {
        return PREFIX_MAP.get(prefix);
    }

    public String getPrefix(String uri) {
        throw new UnsupportedOperationException();
    }

    public Iterator getPrefixes(String uri) {
        throw new UnsupportedOperationException();
    }

}

And here is how to use this class.

HashMap<String, String> namespacesByPrefix = new HashMap<String, String>();
namespacesByPrefix.put("ns", "http://service.soap.oneflexi.com");
namespacesByPrefix.put("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
MyNamespaceContext namespaces = new MyNamespaceContext(namespacesByPrefix);
xPath.setNamespaceContext(namespaces);
//...
Node result = (Node)xPath.evaluate("Soapenv:Envelope/Soapenv:Body/ns:processRequestResponse/ns:return/ItemRs[language=\"SG\"]", doc, XPathConstants.NODE);    



回答2:


I got the solution. we should use xpath as "//ItemRs"

Node result = (Node)xPath.evaluate("//ItemRs[language=\"SG\"]", doc, XPathConstants.NODE);  


来源:https://stackoverflow.com/questions/25034829/what-is-the-valid-xpath-to-validate-the-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!