gpt4 book ai didi

java - XPath 选择 wsdl 前缀命名空间

转载 作者:行者123 更新时间:2023-12-01 11:59:52 25 4
gpt4 key购买 nike

我在 java 中使用 XPath 1.0,想要选择/检查 wsdl 文档 namespace ,但不幸的是没有成功。我要选择

  1. 命名空间 xmlns="http://schemas.xmlsoap.org/wsdl/"表示为“definitions”元素的属性
  2. 命名空间 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"表示为“definitions”元素的属性。

我尝试过 XPath 表达式:

  1. “//namespace::*”和“/*/namespace::*”,看起来很神秘,因为他们返回:

    "http: //www.w3.org/2001/XMLSchema", 
    "http: //www.w3.org/XML/1998/namespace" ????? (where does it come from?)
  2. “/definitions/@*”返回:

    HelloService
    "http ://www.examples.com/wsdl/HelloService.wsdl"

有没有办法捕获 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"和 xmlns="http://schemas.xmlsoap.org/wsdl/"属性/命名空间这个文档使用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 中启用 namespace 支持:

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);

关于java - XPath 选择 wsdl 前缀命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28053822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com