Search code examples
xmlweb-servicesxsltsoapnintex-workflow

Transform SOAP Request with XSLT | For Each Loop Issue


I have a SOAP Request that looks like the following;

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetCountriesResponse xmlns="url">
      <GetCountriesResult>
        <ExtendedOptionItem>
          <Value>GB</Value>
          <Description>United Kingdom</Description>
          <Active>true</Active>
          <IsDefault>true</IsDefault>
          <Order>0</Order>
        </ExtendedOptionItem>
        <ExtendedOptionItem>
          <Value>GB</Value>
          <Description>United Kingdom</Description>
          <Active>true</Active>
          <IsDefault>true</IsDefault>
          <Order>0</Order>
        </ExtendedOptionItem>
      </GetCountriesResult>
    </GetCountriesResponse>
  </soap:Body>
</soap:Envelope>

My XSLT at the moment specifically targets the "Description" node. What I'd like to do is pull out the description for each "ExtendedOptionItem".

I have written some XSLT that will need to separate each description with a semi-colon.

This XSLT works absolutely fine when there is only one "ExtendedOptionItem". However when there are multiple items the XML is not transformed.

Please can you review my XSLT and evaluate where I'm going wrong;

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output method="xml" omit-xml-declaration="no" encoding="utf-8" indent="yes" />
<xsl:template match="/">

  <xsl:for-each xmlns:n="url" select="n:ExtendedOptionItem">
<xsl:value-of select="n:Description" />;</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

Many thanks

UPDATE;

The SOAP response is modified by the Workflow Engine to appear as follows;

<ExtendedOptionItem xmlns="url"><Value>GB</Value><Description>United Kingdom</Description><Active>true</Active><IsDefault>true</IsDefault><Order>0</Order></ExtendedOptionItem><ExtendedOptionItem xmlns="url"><Value>AF</Value><Description>Afghanistan</Description><Active>true</Active><IsDefault>false</IsDefault><Order>0</Order></ExtendedOptionItem><ExtendedOptionItem xmlns="url"><Value>AX</Value><Description>Åland Islands</Description><Active>true</Active><IsDefault>false</IsDefault><Order>0</Order></ExtendedOptionItem>

Solution

  • This XSLT works absolutely fine when there is only one "ExtendedOptionItem".

    No, it doesn't.

    The problem is that your template matches the / root node - and ExtendedOptionItem is not a child of the / root node. As a result, your:

    <xsl:for-each xmlns:n="url" select="n:ExtendedOptionItem">
    

    selects nothing. Try changing it to:

    <xsl:for-each xmlns:n="url" select="//n:ExtendedOptionItem">