简体   繁体   中英

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 does not resolve these. 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?

<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?

Use :

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

XSLT - based verification :

<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 ):

<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 :

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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