简体   繁体   English

如何在Java中使用XPath解析具有内部引用的SOAP响应

[英]How to parse an SOAP response with internal references using XPath in Java

I'll want to parse an SOAP Message with XPath but in the XML Response are internal references. 我想用XPath解析SOAP消息,但是在XML响应中是内部引用。 XPath does not resolve these. XPath不能解决这些问题。 Is it possible to automatical resolve references before using XPath so that i could use xpath expressions like "//get_referencesResponse/Result/source/item" or is there an better way to read the document? 是否可以在使用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>

Is it possible to automatical resolve references before using XPath so that i could use xpath expressions like " //get_referencesResponse/Result/source/item " or is there an better way to read the document? 是否可以在使用XPath之前自动解析引用,以便我可以使用xpath表达式,例如“ //get_referencesResponse/Result/source/item ”,还是有更好的方法来阅读文档?

Use : 用途

/*/item[@id = substring(/*/Result/source/@href, 2)]

XSLT - based verification : 基于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>

when this transformation is applied on the following XML document (yours, but made well-formed and an item added with id=3 ): 当施加于下面的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>

the Xpath expression is evaluated and the selected element is output : 计算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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM