SXI Forum

A place to collect usefull tips, tricks and implementation strategies.

You are not logged in.

#1 22-11-2018 07:40:54

SeanR
Administrator
Registered: 20-11-2018
Posts: 148

How to collect an Attribute from an XML (esp. Web Service Response)

OK ... so there is no simple way to do this currently.

I had to get the "status" attribute from the following xml:

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
    <ns2:processTransactionResponse xmlns:ns2="http://soap.sxi.co.za/">
      <SXIReturn status="FAILED">
        <message>Failed to create XML file.</message>
        <transID>1513606805687</transID>
      </SXIReturn>
    </ns2:processTransactionResponse>
  </S:Body>
</S:Envelope></code></pre>

however when I used the following XPath:

//*[local-name()='SXIReturn']/@status

only the word status was returned .. however it worked in Oxygen (although it did return the full attribute >status="FAILED"<

The only way I have found so far to fetch an attribute is to address it via a stylesheet using an ApplyStyleSheetToMemoryContents mapping in XLayer using a simple stylesheet that looks as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
    <xsl:variable name="status" select="//*[local-name()='SXIReturn'][last()]/@status"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates />
        </xsl:copy>
        <xsl:if test="@status">
           <xsl:apply-templates select="@status" />
       </xsl:if>
    </xsl:template>    
    <xsl:template match="@status">
        <SXIReturnStatus><xsl:value-of select="$status" /></SXIReturnStatus>
    </xsl:template>    
</xsl:stylesheet></code></pre>

This copies all the xml but ALSO create a new xml element called "SXIReturnStatus" and adds the value of the status attribute to it.

This way I can run a "MemoryDataManipulation" mapping to see if the status was SUCCESS or FAILURE using a condition as follows:

<MemoryDataManipulation dataDefinition="GetSOAPStatus">
     <Conditions>
         <Condition>
             <When field="//*[local-name()='MySoapStatus']" operator="Matches" value="FAILED">
                 <LogComment Comment="==== FAILED FAILED FAILED ====="/>
             </When>
         </Condition>
     </Conditions>
 </MemoryDataManipulation>
 

Offline

Board footer

Powered by FluxBB