[英]How to parse an SOAP response with internal references using XPath in Java
我想用XPath解析SOAP消息,但是在XML響應中是內部引用。 XPath不能解決這些問題。 是否可以在使用XPath之前自動解析引用,以便我可以使用xpath表達式,例如“ // get_referencesResponse / Result / source / item”,還是有更好的閱讀文檔的方法?
<SOAP-ENV:Body>
<get_referencesResponse SOAP-ENC:root="1" id="i1">
<Result href="#i2"/>
</get_referencesResponse>
<Result SOAP-ENC:root="0" id="i2">
<source href="#i3"/>
<malware href="#i4"/>
[...]
</Result>
<source SOAP-ENC:arrayType="xsd:string[2]" SOAP-ENC:root="0" id="i3" xsi:type="SOAP- ENC:Array">
<item href="#i8"/>
<item href="#i9"/>
</source>
<malware SOAP-ENC:arrayType="xsd:string[7]" SOAP-ENC:root="0" id="i4" xsi:type="SOAP-ENC:Array">
<item href="#i10"/>
<item href="#i11"/>
<item href="#i12"/>
[...]
</malware>
[...]
<item SOAP-ENC:root="0" id="i8" xsi:type="xsd:string">address</item>
<item SOAP-ENC:root="0" id="i9" xsi:type="xsd:string">network_prefix</item>
<item SOAP-ENC:root="0" id="i10" xsi:type="xsd:string">md5</item>
[...]
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
是否可以在使用XPath之前自動解析引用,以便我可以使用xpath表達式,例如“
//get_referencesResponse/Result/source/item
”,還是有更好的方法來閱讀文檔?
用途 :
/*/item[@id = substring(/*/Result/source/@href, 2)]
基於XSLT的驗證 :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/item[@id = substring(/*/Result/source/@href, 2)]"/>
</xsl:template>
</xsl:stylesheet>
當施加於下面的XML文檔該轉化 (你的,但制成良好和一個item
加有id=3
):
<SOAP-ENV:Body
xmlns:SOAP-ENV="some:SOAP-ENV"
xmlns:SOAP-ENC="some:SOAP-ENC"
xmlns:xsi="some:xsi">
<get_referencesResponse SOAP-ENC:root="1" id="i1">
<Result href="#i2"/>
</get_referencesResponse>
<Result SOAP-ENC:root="0" id="i2">
<source href="#i3"/>
<malware href="#i4"/> [...]
</Result>
<source SOAP-ENC:arrayType="xsd:string[2]"
SOAP-ENC:root="0" id="i3" xsi:type="SOAP-ENC:Array">
<item href="#i8"/>
<item href="#i9"/>
</source>
<malware SOAP-ENC:arrayType="xsd:string[7]"
SOAP-ENC:root="0" id="i4" xsi:type="SOAP-ENC:Array">
<item href="#i10"/>
<item href="#i11"/>
<item href="#i12"/> [...]
</malware> [...]
<item SOAP-ENC:root="0" id="i3"
xsi:type="xsd:string">name</item>
<item SOAP-ENC:root="0" id="i8"
xsi:type="xsd:string">address</item>
<item SOAP-ENC:root="0" id="i9"
xsi:type="xsd:string">network_prefix</item>
<item SOAP-ENC:root="0" id="i10" x
si:type="xsd:string">md5</item> [...]
</SOAP-ENV:Body>
計算Xpath表達式並輸出所選元素 :
<item xmlns:SOAP-ENV="some:SOAP-ENV"
xmlns:SOAP-ENC="some:SOAP-ENC"
xmlns:xsi="some:xsi"
SOAP-ENC:root="0" id="i3" xsi:type="xsd:string">name</item>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.