Creating an XSLT data envelope using XML CDATA sections

February 10, 2010

Just noting this technique down for future reference.

Creating an envelope document using an XML CDATA section:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var userCSharp" version="1.0"  xmlns:ns0="http://tempuri.com/GenericEnvelope"  xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp">
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
  <xsl:template match="/">
    <ns0:Envelope>
      <xsl:attribute name="DocType">
      </xsl:attribute>
<xsl:element name="ns0:Payload">
		<xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text> 
			 <xsl:copy-of select="./@* | ./*"/> 
		<xsl:text disable-output-escaping="yes">]]&gt;</xsl:text> 
      </xsl:element>
    </ns0:Envelope>
  </xsl:template>
</xsl:stylesheet>

Extractng a message from an XML envelope CDATA section:

<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var userCSharp" version="1.0" xmlns:ns0="http://tempuri.com/GenericEnvelope" xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp">
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
  <xsl:template match="/">
		<xsl:copy> 
			<xsl:value-of select="//*[local-name()='Payload']" disable-output-escaping="yes"/> 
	  </xsl:copy> 
  </xsl:template>
</xsl:stylesheet>