java 自动从 WSDL 中提取内联 XSD 到 XSD 文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2521008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatically extracting inline XSD from WSDL into XSD file(s)
提问by Steven Geens
I am using a third party Web Service whose definition and implementation are beyond my control. This web service will change in the future.
我正在使用第三方 Web 服务,其定义和实现超出了我的控制。此 Web 服务将来会发生变化。
The Web Service should be used to generate an XML file which contains some of the same data (represented by the same XSD types) as the Web Service plus some extra information generated by the program.
应使用 Web 服务生成一个 XML 文件,该文件包含一些与 Web 服务相同的数据(由相同的 XSD 类型表示)以及由程序生成的一些额外信息。
My approach:
我的做法:
- create my own XSD referring to the XSD definitions of the WSDL of the called web service (This XSD also includes XSD types for the extra information obviously.)
- use a Java XML databinding framework (like ADB or JiXB) to generate the databinding classes from my own XSD file from step 1
- use a Java SOAP framework (like Axis2 or CXF) with the same databinding framework to generate the databinding classes from the WSDL (This would enable me to use the objects retrieved by the web service directly in the generation of the XML.)
- 创建我自己的 XSD 参考被调用的 web 服务的 WSDL 的 XSD 定义(这个 XSD 显然还包括 XSD 类型的额外信息。)
- 使用 Java XML 数据绑定框架(如 ADB 或 JiXB)从步骤 1 中我自己的 XSD 文件生成数据绑定类
- 使用具有相同数据绑定框架的 Java SOAP 框架(如 Axis2 或 CXF)从 WSDL 生成数据绑定类(这将使我能够在生成 XML 时直接使用 Web 服务检索的对象。)
The XSD types I am going to use in my own XSD file, but are defined in the WSDL, are subject to change. Whenever they change, I would like to automatically process the XSD and WSDL databinding again. (If the change is significant enough, this might trigger some development effort.(But usually not.))
我将在我自己的 XSD 文件中使用但在 WSDL 中定义的 XSD 类型可能会发生变化。每当它们发生变化时,我想再次自动处理 XSD 和 WSDL 数据绑定。(如果变化足够大,这可能会触发一些开发工作。(但通常不会。))
My problem:
我的问题:
In step 1 I need an XSD referring to the same types as used by the Web Service.
在第 1 步中,我需要一个 XSD 引用与 Web 服务使用的类型相同的类型。
The WSDL is referring to another WSDL, which is referring to another WSDL etc. Eventually there is an WSDL with the needed inline XSD types. As far as I know there is no way to directly reference the inline XSD types of a WSDL from an XSD.
WSDL 指的是另一个 WSDL,后者指的是另一个 WSDL 等。最终有一个具有所需内联 XSD 类型的 WSDL。据我所知,无法从 XSD 直接引用 WSDL 的内联 XSD 类型。
The approach I would think most viable, is to include an extra step in the automatic processing (before the databinding) that extracts the inline XSD from the WSDL into other XSD file(s). These other XSD file(s) can then be referred to by my own XSD file.
我认为最可行的方法是在自动处理中(在数据绑定之前)包括一个额外的步骤,该步骤将内联 XSD 从 WSDL 提取到其他 XSD 文件中。然后,我自己的 XSD 文件可以引用这些其他 XSD 文件。
Things I'd like to avoid:
我想避免的事情:
- Manually copy pasting the inline XSD into an XSD file (I am looking for an automatic process.)
- Any manual steps.(Like the determining the WSDL that contains the inline types manually.(The location of that WSDL does change as well.))
- Using xsd:any in my own XSD. I would like my own XSD file to be correct.
- Using a non-Java technology(like .NET)
- Huge amounts of implementation (but hints on how you would implement such an extraction are welcome anyway)
- 手动将内联 XSD 复制粘贴到 XSD 文件中(我正在寻找一个自动过程。)
- 任何手动步骤。(例如手动确定包含内联类型的 WSDL。(该 WSDL 的位置也会改变。))
- 在我自己的 XSD 中使用 xsd:any。我希望我自己的 XSD 文件是正确的。
- 使用非 Java 技术(如 .NET)
- 大量的实现(但无论如何都欢迎提供有关如何实现此类提取的提示)
PS: I found some similar questions, but they all had responses like: WTH would you want to do that? That is the reason for my rather large background story.
PS:我发现了一些类似的问题,但他们都有这样的回答:你想这样做吗?这就是我相当大的背景故事的原因。
采纳答案by Miklos Csuka
I don't know any libraries that would do this for you, but it is definitly possible to implement with a bit of effort (~200 lines). A rough meta-program to generate all inline and included XSDs:
我不知道有任何库可以为您执行此操作,但是绝对可以通过一些努力(约 200 行)来实现。生成所有内联和包含 XSD 的粗略元程序:
method processWSDL(Document wsdl) {
for each ("/wsdl:definitions/wsdl:types/xsd:schema" in wsdl) {
call processXSD("inline_[i].xsd",".")
}
for each ("/wsdl:definitions/wsdl:import" in wsdl) {
Document x = read and parse ("@location")
if (x is WSDL) call processWSDL(x)
else if (x is XSD) call processXSD("@location", x)
}
}
method processXSD(String filename, Document xsd) {
write "xsd" to a new file "filename" // if 'filename' is a URL, take only the part after the last '/'
for each ("/xsd:schema/xsd:import" or "/xsd:schema/xsd:include" in xsd) {
if ("@schemaLocation" is local reference) { // no 'http://' prefix
Document x = read and parse ("@schemaLocation")
call processXSD("@schemaLocation", x)
}
}
}
It is not a full solution, e.g. does not handle namespace prefixes defined outside of the inline schema, but hopefully gives a good starting point.
这不是一个完整的解决方案,例如不处理定义在内联模式之外的命名空间前缀,但希望提供一个好的起点。
回答by Petru Gardea
Just to keep this post relevant, things have changed since an answer was accepted. It is now possible in Java to generate what you want starting from WSDL; JAX-WS provides the wsimporttool that does exactly what's requested: take the WSDL, create a client proxy along with a bunch of JAXB-annotated classes for un/marshalling of requests.
为了保持这篇文章的相关性,自从接受答案以来,情况已经发生了变化。现在可以在 Java 中从 WSDL 开始生成您想要的内容;JAX-WS 提供了wsimport工具,它完全按照要求执行:采用 WSDL,创建一个客户端代理以及一堆 JAXB 注释的类,用于取消/编组请求。
I would say though, to @MoizTankiwala 's point, that the need to export XSD content from WSDL (or to include all external XSD content inside a WSDL's types section) is alive and well.
不过,对于@MoizTankiwala 的观点,我想说的是,从 WSDL 导出 XSD 内容(或将所有外部 XSD 内容包含在 WSDL 的类型部分)的需求仍然存在。
The former is something that makes sense when someone has a large body of XSDs, and there's a general concern regarding effective managing, analysing and evolving of that model in XSD.
当某人拥有大量 XSD 时,前者是有意义的,并且普遍关注 XSD 中该模型的有效管理、分析和发展。
The latter is also sought after since some (mainly) dynamic languages still lack full blown support from tooling when it comes to WSDL to client side proxy generation.
后者也受到追捧,因为当涉及到 WSDL 到客户端代理生成时,一些(主要是)动态语言仍然缺乏工具的全面支持。
My other answer on SOtalks about a similar need, that should help at least with Moiz's request...
我在 SO 上的另一个回答谈到了类似的需求,这至少应该对 Moiz 的要求有所帮助......

