Ryan Ternier
Ryan Ternier

Reputation: 8804

Wrap SOAP tags/headers around XML with XSLT

I'm trying to find XSLT that can do the following.

I'm trying to wrap an XML message in a quick soap envelope (xml). The source XML is not set in stone, so the XSLT should not care what the XML is. It does need to strip the XML Declaration off the top of the XML.

Example:

<?xml version="1.0"?>
<foo>
    <bar />
</foo>

transformed to:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:hl7-org:v3">
    <soapenv:Header/>
    <soapenv:Body>
          <foo>
            <bar />
          </foo>
    </soapenv:Body>
</soapenv:Envelope>  

Example 2:

<lady>
  <and>
   <the>
    <tramp />
   </the>
  </and>
</lady>



<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:hl7-org:v3">
    <soapenv:Header/>
    <soapenv:Body>
         <lady>
           <and>
              <the>
                  <tramp />
             </the>
           </and>
         </lady>
    </soapenv:Body>
</soapenv:Envelope>  

Upvotes: 3

Views: 4450

Answers (2)

Codeman
Codeman

Reputation: 12375

This XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="*">
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns="urn:hl7-org:v3">
            <soapenv:Header/>
            <soapenv:Body>
                <xsl:copy-of select="/*"/>
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>
</xsl:stylesheet>

outputs this XML:

<soapenv:Envelope xmlns="urn:hl7-org:v3" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <lady xmlns="">
         <and>
            <the>
               <tramp/>
            </the>
         </and>
      </lady>
   </soapenv:Body>
</soapenv:Envelope>

Upvotes: 2

Daniel Haley
Daniel Haley

Reputation: 52858

You could use this (which is similar to Phoenixblade9's answer, but doesn't use xsl:element)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:hl7-org:v3">
            <soapenv:Header/>
            <soapenv:Body>
                <xsl:copy-of select="*"/>
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 2

Related Questions