XSLT Dynamic Transformation

I have an incoming simple xml request below and need to convert to a lower SOAP message with the correct namespace. In the incoming XML request, the namespace will not appear this way, creating the SOAP message, we need to take care of the namespace as well. Is there any XSLT code snippet that will help me with this. Note. We need to do this XSLT transformation dynamically, like an incoming request, there can be any element, such as "GetImageRequest", therefore, based on this element, we need to create a namespace. (maybe we can save the entire namespace in one XML file and need to build a SOAP message)

Inbound XML Request:

<request>
<payload>
<GetImageRequest>
   <participantId>1191152220010</participantId>
   <participantCode>131029</participantCode>
   <groupCode>027198</groupCode>
   <userType>EE</userType>
   <clientName>Test</clientName>
   <shoeboxID>123444</shoeboxID>
   <imageID>45235</imageID>
</GetImageRequest>
</payload>
</request>

================== You need to create a SOAP message below with the correct namespace.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>

   </soapenv:Header>
   <soapenv:Body>
      <get:GetShoeboxItemRequest xmlns:get="urn:webservice/server/mobile/shoebox/types/v1/GetShoeboxItem">
         <get:participantId>1060687620010</get:participantId>
         <get:participantCode>1060687620010</get:participantCode>
         <get:groupCode>027198</get:groupCode>
         <get:userType>EE</get:userType>
         <get:clientName>Test</get:clientName>
         <get:shoeboxID>123444</get:shoeboxID>
      </get:GetShoeboxItemRequest>
   </soapenv:Body>
</soapenv:Envelope>

. XSLT .

+4
2

:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pRequestedItemName" select="'Shoebox'"/>

 <xsl:variable name="vUpper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
 <xsl:variable name="vLower" select="'abcdefghijklmnopqrstuvwxyz'"/>

 <xsl:variable name="vLcItemName" select=
                "translate($pRequestedItemName, $vUpper, $vLower)"/>

 <xsl:variable name="vDynNamespace" select=
   "concat('urn:webservice/server/mobile/', $vLcItemName, '/types/v1/Get', 'Item')"/>

  <xsl:template match="/">
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
            <soapenv:Header>
            </soapenv:Header>
            <soapenv:Body>
              <xsl:element name="get:Get{$pRequestedItemName}ItemRequest" 
                   namespace="{$vDynNamespace}">
                <xsl:apply-templates select="/*/*/GetImageRequest/node()"/>
              </xsl:element>
            </soapenv:Body>
        </soapenv:Envelope>
  </xsl:template>

  <xsl:template match="*">
    <xsl:element name="get:{name()}" namespace="{$vDynNamespace}">
      <xsl:copy-of select="namespace::*|@*"/>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

XML-:

<request>
    <payload>
        <GetImageRequest>
            <participantId>1191152220010</participantId>
            <participantCode>131029</participantCode>
            <groupCode>027198</groupCode>
            <userType>EE</userType>
            <clientName>Test</clientName>
            <shoeboxID>123444</shoeboxID>
            <imageID>45235</imageID>
        </GetImageRequest>
    </payload>
</request>

, :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <get:GetShoeboxItemRequest
         xmlns:get="urn:webservice/server/mobile/shoebox/types/v1/GetItem">
         <get:participantId>1191152220010</get:participantId>
         <get:participantCode>131029</get:participantCode>
         <get:groupCode>027198</get:groupCode>
         <get:userType>EE</get:userType>
         <get:clientName>Test</get:clientName>
         <get:shoeboxID>123444</get:shoeboxID>
         <get:imageID>45235</get:imageID>
      </get:GetShoeboxItemRequest>
   </soapenv:Body>
</soapenv:Envelope>

(, ), . .


UPDATE

OP , , , XML .

:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pRequestData">
   <r name="GetImageRequest" 
    ns="urn:webservice/server/mobile/shoebox/types/v1/GetShoeboxItem"/>
   <r name="SaveShoeBoxitemRequest" 
    ns="urn:webservice/server/mobile/shoebox/types/v1/SaveShoeboxItem"/>
   <r name="SaveClaimWithReceiptRequest" 
    ns="urn:webservice/server/mobile/shoebox/types/v1/SaveClaimAndReceipt"/>
   <r name="GetThumbNailImageRequest" 
    ns="urn:webservice/server/mobile/shoebox/types/v1/GetThumbnail"/>
   <r name="AttachReceiptwithExistingClaimRequest" 
    ns="urn:webservice/server/mobile/shoebox/types/v1/AttachClaimAndReceipt"/>
 </xsl:param>

 <xsl:variable name="vParams" select="document('')/*/xsl:param[@name='pRequestData']"/>

  <xsl:template match="/">
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
            <soapenv:Header>
            </soapenv:Header>
            <soapenv:Body>
                <xsl:apply-templates select="/*/*/*"/>
            </soapenv:Body>
        </soapenv:Envelope>
  </xsl:template>

  <xsl:template match="*">
    <xsl:param name="pKey" select="local-name()"/>
    <xsl:element name="get:{local-name()}" namespace="{$vParams/*[@name = $pKey]/@ns}">
      <xsl:copy-of select="namespace::*|@*"/>
      <xsl:apply-templates>
        <xsl:with-param name="pKey" select="$pKey"/>
      </xsl:apply-templates>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

XML- ( , payload):

<request>
    <payload>
        <GetImageRequest>
            <participantId>1191152220010</participantId>
            <participantCode>131029</participantCode>
            <groupCode>027198</groupCode>
            <userType>EE</userType>
            <clientName>Test</clientName>
            <shoeboxID>123444</shoeboxID>
            <imageID>45235</imageID>
        </GetImageRequest>
        <SaveShoeBoxitemRequest>
            <participantId>1191152220010</participantId>
            <participantCode>131029</participantCode>
            <groupCode>027198</groupCode>
            <userType>EE</userType>
            <clientName>Test</clientName>
            <shoeboxID>123444</shoeboxID>
            <imageID>45235</imageID>
        </SaveShoeBoxitemRequest>
    </payload>
</request>

, ( "" :

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
       <soapenv:Header/>
       <soapenv:Body>
          <get:GetImageRequest 
xmlns:get="urn:webservice/server/mobile/shoebox/types/v1/GetShoeboxItem">
             <get:participantId>1191152220010</get:participantId>
             <get:participantCode>131029</get:participantCode>
             <get:groupCode>027198</get:groupCode>
             <get:userType>EE</get:userType>
             <get:clientName>Test</get:clientName>
             <get:shoeboxID>123444</get:shoeboxID>
             <get:imageID>45235</get:imageID>
          </get:GetImageRequest>
          <get:SaveShoeBoxitemRequest 
xmlns:get="urn:webservice/server/mobile/shoebox/types/v1/SaveShoeboxItem">
             <get:participantId>1191152220010</get:participantId>
             <get:participantCode>131029</get:participantCode>
             <get:groupCode>027198</get:groupCode>
             <get:userType>EE</get:userType>
             <get:clientName>Test</get:clientName>
             <get:shoeboxID>123444</get:shoeboxID>
             <get:imageID>45235</get:imageID>
          </get:SaveShoeBoxitemRequest>
       </soapenv:Body>
    </soapenv:Envelope>
+3

, , 5 , xml XML. , , .

, "root", .

( , ):

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://www.example.com/my"
exclude-result-prefixes="my">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<my:ns-map>
    <ns root="GetImageRequest">urn:webservice/a/b/GetShoeboxItem</ns>
    <ns root="SaveShoeBoxitemRequest">urn:webservice/c/d/SaveShoeBoxitemRequest</ns>
    <ns root="SaveClaimWithReceiptRequest">urn:webservice/e/f/SaveClaimWithReceiptRequest</ns>
    <ns root="GetThumbNailImageRequest">urn:webservice/g/h/GetThumbNailImageRequest</ns>
    <ns root="AttachReceiptwithExistingClaimRequest">urn:webservice/i/k/AttachReceiptwithExistingClaimRequest</ns>
</my:ns-map>

<xsl:variable name="root" select="name(/request/payload/*)"/>
<xsl:variable name="ns" select="document('')/xsl:stylesheet/my:ns-map/ns[@root=$root]"/>

<xsl:template match="/">
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <soapenv:Header/>
        <soapenv:Body>
            <xsl:apply-templates select="request/payload/*" />
        </soapenv:Body>
    </soapenv:Envelope>
</xsl:template>     

<xsl:template match="*" >
    <xsl:element name="get:{local-name()}" namespace="{$ns}">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates />
    </xsl:element>
</xsl:template>

</xsl:stylesheet>
0

Source: https://habr.com/ru/post/1612646/


All Articles