XPath选择wsdl前缀名称空间
问题描述:
我在java中使用XPath 1.0,并想选择/检查wsdl文档名称空间,但不幸的是没有设法做到这一点。 我想选择表示为 “定义” 元件 XPath选择wsdl前缀名称空间
- 命名空间的xmlns = “http://schemas.xmlsoap.org/wsdl/”:肥皂=“HTTP:/ /schemas.xmlsoap.org/wsdl/soap/“表示为”definitions“元素的属性。
我试图XPath表达式:
-
“//命名空间:: *” 和 “/ * /命名空间:: *”,这似乎是神秘的, ,因为它们返回:
"http: //www.w3.org/2001/XMLSchema", "http: //www.w3.org/XML/1998/namespace" ????? (where does it come from?)
-
“/定义/ @ *” 的回报:
HelloService "http ://www.examples.com/wsdl/HelloService.wsdl"
有什么办法可以捕获xmlns:soap =“http://schemas.xmlsoap.org/wsdl/soap/”和xmlns =“http://schemas.xmlsoap.org/wsdl/”attributes/namespaces在这个文档中使用XPath?或者其他一些工具?以检查是否节点(属性)或命名空间值等于schemas.xmlsoap.org/wsdl/soap(种类正确的命名空间控制的)
Wsdl document:
<definitions name="HelloService"
targetNamespace="http://www.examples.com/wsdl/HelloService.wsdl"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<message name="SayHelloRequest">
<part name="firstName" type="xsd:string"/>
</message>
<message name="SayHelloResponse">
<part name="greeting" type="xsd:string"/>
</message>
<portType name="Hello_PortType">
<operation name="sayHello">
<input message="tns:SayHelloRequest"/>
<output message="tns:SayHelloResponse"/>
</operation>
</portType>
<binding name="Hello_Binding" type="tns:Hello_PortType">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="sayHello">
<soap:operation soapAction="sayHello"/>
<input>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:examples:helloservice"
use="encoded"/>
</input>
<output>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="urn:examples:helloservice"
use="encoded"/>
</output>
</operation>
</binding>
<service name="Hello_Service">
<documentation>WSDL File for HelloService</documentation>
<port binding="tns:Hello_Binding" name="Hello_Port">
<soap:address location="http://www.examples.com/SayHello/"/>
</port>
</service>
</definitions>
答
对于第一个:
/*/namespace::*[name()='']
结果: http://schemas.xmlsoap.org/wsdl/
对于第二之一:
/*/namespace::*[name()='soap']
结果:http://schemas.xmlsoap.org/wsdl/soap/
你应该记住,使命名空间支持java中:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); //This is really important, without it that XPath does not work
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(inputSource); //inputSource, inputStream or file which contains your XML.
XPath xpath = XPathFactory.newInstance().newXPath();
String nameSpace = xpath.evaluate("/*/namespace::*[name()='']", document);
String soapNameSpace = xpath.evaluate("/*/namespace::*[name()='soap']", document);
不工作。你确定它应该适用于Xpath 1.0吗? NodeList result =(NodeList)xpath.evaluate(“/ */namespace :: * [name()='soap']”,doc,XPathConstants.NODESET);和/ */namespace :: * [name()='']不返回任何内容。 – user34618 2015-01-20 20:07:57
我已经在http://www.xpathtester.com/xpath中测试过它。它不返回一个NodeList。我更新了答案,以显示每个人的结果。 – 2015-01-20 20:21:35
它写在问题“我正在使用Java中的XPath 1.0 ......” – user34618 2015-01-20 20:22:46