xml - 使用 XPath 从 WSDL 文件中导入的 ("xsd:import") 架构获取 XML 元素声明

标签 xml xpath wsdl xsd

有一个问题困扰我一周了。

我使用 Netbeans 7.2.1 将虚拟 Web 服务编程为无状态 EJB:

@WebService(serviceName = "WSPersonLookup")
@Stateless()
public class WSPersonData {

  @PersistenceContext(unitName="PUWSPersonData")
  private EntityManager em;

 /**
  * Web service operation
  */
  @WebMethod(operationName = "getPersonData")
  public Person getPersonData(@WebParam(name = "personId") final String personId) {
    return em.find(Person.class, personId);
  }
}

“Person”类只是映射到该表的 JPA 实体(我使用 PostgreSQL 作为 DBMS):

CREATE TABLE person
(
  id character varying(10) NOT NULL DEFAULT ''::character varying,
  nit text NOT NULL DEFAULT ''::text,
  name text NOT NULL DEFAULT ''::text,
  address text NOT NULL DEFAULT ''::text,
  phone integer NOT NULL DEFAULT 0,
  creation_date timestamp with time zone NOT NULL DEFAULT now(),
  CONSTRAINT pk_person PRIMARY KEY (id)
)

生成的 WSDL 是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.i2b.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.i2b.com/" name="WSPersonLookup">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://ws.i2b.com/" schemaLocation="http://pc-i2b:8080/WSPersonLookup/WSPersonData?xsd=1" />
    </xsd:schema>
  </types>
  <message name="getPersonData">
    <part name="parameters" element="tns:getPersonData" />
  </message>
  <message name="getPersonDataResponse">
    <part name="parameters" element="tns:getPersonDataResponse" />
  </message>
  <portType name="WSPersonData">
    <operation name="getPersonData">
      <input wsam:Action="http://ws.i2b.com/WSPersonData/getPersonDataRequest" message="tns:getPersonData" />
      <output wsam:Action="http://ws.i2b.com/WSPersonData/getPersonDataResponse" message="tns:getPersonDataResponse" />
    </operation>
  </portType>
  <binding name="WSPersonDataPortBinding" type="tns:WSPersonData">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
    <operation name="getPersonData">
      <soap:operation soapAction="" />
      <input>
        <soap:body use="literal" />
      </input>
      <output>
        <soap:body use="literal" />
      </output>
    </operation>
  </binding>
  <service name="WSPersonLookup">
    <port name="WSPersonDataPort" binding="tns:WSPersonDataPortBinding">
      <soap:address location="http://pc-i2b:8080/WSPersonLookup/WSPersonData" />
    </port>
  </service>
</definitions>

XSD 声明:

<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:tns="http://ws.i2b.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
           version="1.0" targetNamespace="http://ws.i2b.com/">
  <xs:element name="getPersonData" type="tns:getPersonData"/>
  <xs:element name="getPersonDataResponse" type="tns:getPersonDataResponse"/>
  <xs:element name="person" type="tns:person"/>
  <xs:complexType name="getPersonData">
    <xs:sequence>
      <xs:element name="personId" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="getPersonDataResponse">
    <xs:sequence>
      <xs:element name="return" type="tns:person" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="person">
    <xs:sequence>
      <xs:element name="address" type="xs:string" minOccurs="0"/>
      <xs:element name="creationDate" type="xs:dateTime" minOccurs="0"/>
      <xs:element name="id" type="xs:string" minOccurs="0"/>
      <xs:element name="name" type="xs:string" minOccurs="0"/>
      <xs:element name="nit" type="xs:string" minOccurs="0"/>
      <xs:element name="phone" type="xs:int"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

问题是:我需要访问上面的模式声明,在 WSDL 中导入:

 <types>
    <xsd:schema>
      <xsd:import namespace="http://ws.i2b.com/" schemaLocation="http://pc-i2b:8080/WSPersonLookup/WSPersonData?xsd=1" />
    </xsd:schema>
  </types>

通过使用 XPath。我不允许直接访问架构。

有办法做到这一点吗?如果没有,有人知道解决方法吗?

亲切的问候。

最佳答案

您无法仅使用 XPath 获取主 XML 文档 (WSDL) 引用的辅助 XML 文档(本例中的架构)。

使用 XSLT,您可以通过 document() 函数来完成此操作:

<xsl:variable name="schema" select="document(/soap:definitions/soap:types/xsd:schema/xsd:import/@schemaLocation)"/>

或者,不尝试获取导入的模式,只需使用 /soap:definitions/soap:types/xsd:schema 等 XPath 获取 WSDL 内的模式片段。这是一个有效的架构,在 http://pc-i2b:8080/WSPersonLookup/WSPersonData?xsd=1 导入该架构 - 您应该能够直接使用它。

关于xml - 使用 XPath 从 WSDL 文件中导入的 ("xsd:import") 架构获取 XML 元素声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14224777/

相关文章:

python - 查找标题包含文本的 anchor

c# - 将 LINQ XML 与命名空间一起使用

xml - XSLT 将元素名称转换为 Pascal 大小写

xslt - 使用 Xpath 选择每个节点和兄弟节点(直到该节点下一次出现)组合

python - 如何使用 Scrapy 从网站获取所有纯文本?

wcf 使用扁平化 WSDL 从 WCF 服务中提取 wsdl

java - 具有相同操作名称和输入类型的 WSDL 会生成不正确的客户端 stub 吗?

java - web.xml 中的 spring web 服务配置?

xml - 冒号是 XML 标记名称中合法的第一个字符吗?

c# - 用于使用反射编写脚本的 XML SAX 解析器