假设我有任何标准的 WSDL 文件(以 XML 表示):
<wsdl:definitions>
<wsdl:types>
...
</wsdl:types>
<wsdl:message>
...
</wsdl:message>
<wsdl:portType name="countrySoap”>
<wsdl:operation name="GetCountryByCountryCode">
<wsdl:documentation>Get country name by country code</wsdl:documentation>
<wsdl:input message="tns:GetCountryByCountryCodeSoapIn" />
<wsdl:output message="tns:GetCountryByCountryCodeSoapOut" />
</wsdl:operation>
<wsdl:operation name="GetISD">
<wsdl:documentation>Get International Dialing Code </wsdl:documentation>
<wsdl:input message="tns:GetISDSoapIn" />
<wsdl:output message="tns:GetISDSoapOut" />
</wsdl:operation>
...
<wsdl:portType name="countrySoap”>
....
</wsdl:definitions>
我想做的是有一种简单/有效的方法来交换每条消息的输入和输出消息。
例如,我想要:
<wsdl:operation name="GetCountryByCountryCode">
<wsdl:documentation>Get country name by country code</wsdl:documentation>
<wsdl:input message="tns:GetCountryByCountryCodeSoapOut" />
<wsdl:output message="tns:GetCountryByCountryCodeSoapIn" />
</wsdl:operation>
我一直在使用的示例 wsdl 文件可以在这里找到: http://www.webservicex.net/country.asmx?WSDL
补充说明:
- 我可能正在寻找基于 XSLT 的解决方案,因为在我看来,这种解决方案效率更高。例如,我目前的解决方案是基于 Java 的,但它似乎没有我想要的那么高效。
- 如果解决方案能够忽略任何 namespace ,那将是很好的,例如:一些 wsdl 文件显示为:
wsdl:definitions
、wsdl:portType
、wsdl:operation
- 而其他的可能只是definitions
、portType
、operation
等。
最佳答案
此 XSLT(1.0 和 2.0)转换:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="some:wsdl">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="x:input/@message">
<xsl:attribute name="message">
<xsl:value-of select="../../x:output/@message"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="x:output/@message">
<xsl:attribute name="message">
<xsl:value-of select="../../x:input/@message"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
应用于以下 XML 文档时(从提供的严重畸形文档中获取):
<wsdl:definitions xmlns:wsdl="some:wsdl">
<wsdl:types>
...
</wsdl:types>
<wsdl:message>
...
</wsdl:message>
<wsdl:portType name="countrySoap">
<wsdl:operation name="GetCountryByCountryCode">
<wsdl:documentation>Get country name by country code</wsdl:documentation>
<wsdl:input message="tns:GetCountryByCountryCodeSoapIn" />
<wsdl:output message="tns:GetCountryByCountryCodeSoapOut" />
</wsdl:operation>
<wsdl:operation name="GetISD">
<wsdl:documentation>Get International Dialing Code </wsdl:documentation>
<wsdl:input message="tns:GetISDSoapIn" />
<wsdl:output message="tns:GetISDSoapOut" />
</wsdl:operation>
...
</wsdl:portType>
....
</wsdl:definitions>
产生想要的、正确的结果:
<wsdl:definitions xmlns:wsdl="some:wsdl">
<wsdl:types>
...
</wsdl:types>
<wsdl:message>
...
</wsdl:message>
<wsdl:portType name="countrySoap">
<wsdl:operation name="GetCountryByCountryCode">
<wsdl:documentation>Get country name by country code</wsdl:documentation>
<wsdl:input message="tns:GetCountryByCountryCodeSoapOut"/>
<wsdl:output message="tns:GetCountryByCountryCodeSoapIn"/>
</wsdl:operation>
<wsdl:operation name="GetISD">
<wsdl:documentation>Get International Dialing Code </wsdl:documentation>
<wsdl:input message="tns:GetISDSoapOut"/>
<wsdl:output message="tns:GetISDSoapIn"/>
</wsdl:operation>
...
</wsdl:portType>
....
</wsdl:definitions>
关于java - 交换 XML (WSDL) 文件中的元素属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11526334/