0

Already searched for an answer here, but couldn't find any suitable solution...!

I'm trying to write a XSLT tranformation, starting from a SOAP response message, to be transformed to a XML message (the XML tags will not be the same).

Here is my input SOAP message :

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
    <CatalogPriceResponseParams xmlns="http://theurl.com">
        <ArrayOfOutputCatalogItems>
            <ItemID>003332</ItemID>
            <ShipFromPartyWarehouseLocationID>901</ShipFromPartyWarehouseLocationID>
            <Quantity>20.0</Quantity>
            <UnitCode>PCE</UnitCode>
            <UnitPriceAmount>104.9</UnitPriceAmount>
            <UnitPricePerQuantity>1.0</UnitPricePerQuantity>
            <UnitPricePerQuantityUOM>EA</UnitPricePerQuantityUOM>
            <PricingAmountUnitPretaxAmount>104.9</PricingAmountUnitPretaxAmount>
        </ArrayOfOutputCatalogItems>
        <ArrayOfOutputCatalogItems>
            <ItemID>003333</ItemID>
            <ShipFromPartyWarehouseLocationID>901</ShipFromPartyWarehouseLocationID>
            <Quantity>1.0</Quantity>
            <UnitCode>PCE</UnitCode>
            <UnitPriceAmount>100.9</UnitPriceAmount>
            <UnitPricePerQuantity>1.0</UnitPricePerQuantity>
            <UnitPricePerQuantityUOM>EA</UnitPricePerQuantityUOM>
            <PricingAmountUnitPretaxAmount>100.9</PricingAmountUnitPretaxAmount>
        </ArrayOfOutputCatalogItems>
    </CatalogPriceResponseParams>
    </soap:Body>
</soap:Envelope>

the desired output is :

<TradeResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
               xsi:noNamespaceSchemaLocation="genericResponse.xsd">
   <getPrices>
      <errorCode>0</errorCode>
      <currency>EUR</currency>
      <articleList>
         <article>
            <articleId>003332</articleId>
            <icon>2</icon>
            <priceList>
               <price>
                  <price>104.9</price>
               </price>
            </priceList>
         </article>
         <article>
            <articleId>003333</articleId>
            <icon>2</icon>
            <priceList>
               <price>
                  <price>100.9</price>
               </price>
            </priceList>
         </article>
      </articleList>
   </getPrices>
</TradeResponse>

I already started to write a XSLT transformation but this doesn"t give the expected result:

<xsl:stylesheet version="1.0" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="soap:*">
        <xsl:apply-templates select="*"/>
    </xsl:template>
    <xsl:template match="/">
        <TradeResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="genericResponse.xsd">
            <getPrices>
                <errorCode>
                    <xsl:value-of select="'0'"/>
                </errorCode>
                <currency>
                    <xsl:value-of select="'EUR'"/>
                </currency>
                <articleList>
                    <xsl:for-each select=".*/CatalogPriceResponseParams/ArrayOfOutputCatalogItems">
                        <article>
                            <articleId>
                                <xsl:value-of select="./ItemID"/>
                            </articleId>
                            <icon>
                                <xsl:value-of select="'2'"/>
                            </icon>
                            <priceList>
                                <price>
                                    <price>
                                        <xsl:value-of select="./UnitPriceAmount"/>
                                    </price>
                                </price>
                            </priceList>
                        </article>
                    </xsl:for-each>
                </articleList>
            </getPrices>
        </TradeResponse>
    </xsl:template>
</xsl:stylesheet>

I hacve issue with the for-each instructions and I'm not able to address the XPathes I need.

Any help would be appreciated !

Many Thanks, David.

  • The main issue you have is that `CatalogPriceResponseParams` and all its descendants are in a namespace of their own. See here how to handle this: https://stackoverflow.com/a/34762628/3016153. -- P.S. Do you really need the `xmlns:soap` namespace declaration in the output? – michael.hor257k Sep 13 '22 at 07:22
  • @michael.hor257k Thanks for your reply. I'll have a look on your suggestion. And, No I don't need the `xmlns:soap` in the output ! that's another issue I have ! – David TOLLET Sep 13 '22 at 07:25
  • Well, then put `exclude-result-prefixes="soap ns0"` in your `xsl:stylesheet` start-tag (where `ns0` would be the prefix used for declaring the `http://theurl.com` namespace). – michael.hor257k Sep 13 '22 at 07:31
  • @michael.hor257k OK ! work with : `` But still, I'm unable to access/address the ItemID in the for-each loop ... – David TOLLET Sep 13 '22 at 07:37
  • Are you using ``? – michael.hor257k Sep 13 '22 at 08:01
  • Yes I do now, but nothing change... Do you want the updated XSLT ? – David TOLLET Sep 13 '22 at 08:27

1 Answers1

0

As I mentioned in the comments, the main issue here is the default namespace declared in your XSLT. Once you fix that, and remove the unnecessary verbosity, you should be able to do with:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns0="http://theurl.com"
exclude-result-prefixes="soap ns0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/soap:Envelope">
    <TradeResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="genericResponse.xsd">
        <getPrices>
            <errorCode>0</errorCode>
            <currency>EUR</currency>
            <articleList>
                <xsl:for-each select="soap:Body/ns0:CatalogPriceResponseParams/ns0:ArrayOfOutputCatalogItems">
                    <article>
                        <articleId>
                            <xsl:value-of select="ns0:ItemID"/>
                        </articleId>
                        <icon>2</icon>
                        <priceList>
                            <price>
                                <price>
                                    <xsl:value-of select="ns0:UnitPriceAmount"/>
                                </price>
                            </price>
                        </priceList>
                    </article>
                </xsl:for-each>
            </articleList>
        </getPrices>
    </TradeResponse>
</xsl:template>

</xsl:stylesheet>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51