Mayur Jadhav
Mayur Jadhav

Reputation: 125

XSLT & BizTalk: Issue with applying template and transgforming XML to specific format

I am construction a message using BizTalk Map and applying external XSLT to it. I want the transformed XML to be having '&lt' and '&gt' instead of "<" and ">" for example:

My Input XML is:

   <DEALER>
      <DEALER_DETAILS>
          <ID>DL00005</ID>
          <DEALER_NAME>Dealer Name</DEALER_NAME>
          <INN_NUMBER>7736530296</INN_NUMBER>
          <KPP_NUMBER>773601001</KPP_NUMBER>
          <OKPO_NUMBER>69076838</OKPO_NUMBER>
          <SAP_CODE>SAP101</SAP_CODE>
          <UPDATE_DATE>2010-08-02</UPDATE_DATE>
      </DEALER_DETAILS>
    </DEALER>

XSLT that I am applying is:

     <xsl:template match="/">
        <xsl:apply-templates select="/DEALER" />
     </xsl:template>
     <xsl:template match="/DEALER">
        <Result>
            <Dealer> 
                <xsl:copy-of select="/*" />
            </Dealer>
        </Result>
     </xsl:template>

Output is:

    <Result>
      <DEALER xmlns="http://tempuri.org/RSINT9_Output.xsd">
        <DEALER_DETAILS>
          <ID>DL00005</ID>
          <DEALER_NAME>Dealer Name Again</DEALER_NAME>
          <INN_NUMBER>7736530296</INN_NUMBER>
          <KPP_NUMBER>773601001</KPP_NUMBER>
          <OKPO_NUMBER>69076838</OKPO_NUMBER>
          <SAP_CODE>SAP101</SAP_CODE>
          <UPDATE_DATE>2010-08-02</UPDATE_DATE>
        </DEALER_DETAILS>
      </DEALER>
    </Result>

But I want the output to be like :

    &lt;Result&gt;
      &lt;DEALER xmlns="http://tempuri.org/RSINT9_Output.xsd"&gt;
        &lt;DEALER_DETAILS&gt;
          &lt;ID&gt;DL00005&lt;/ID&gt;
          &lt;DEALER_NAME&gt;Dealer Name Again&lt;/DEALER_NAME&gt;
          &lt;INN_NUMBER&gt;7736530296&lt;/INN_NUMBER&gt;
          &lt;KPP_NUMBER&gt;773601001&lt;/KPP_NUMBER&gt;
          &lt;OKPO_NUMBER&gt;69076838&lt;/OKPO_NUMBER&gt;
          &lt;SAP_CODE&gt;SAP101&lt;/SAP_CODE&gt;
          &lt;UPDATE_DATE&gt;2010-08-02&lt;/UPDATE_DATE&gt;
        &lt;/DEALER_DETAILS&gt;
      &lt;/DEALER&gt;
    &lt;/Result&gt;

Please guide me.

Thanks, Mayur Jadhav

Upvotes: 2

Views: 1228

Answers (1)

StuartLC
StuartLC

Reputation: 107237

This should do the trick - it works by capturing the result of your XSLT transform into a variable, and then using C# script to grab the XmlEncoded OuterXml.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp"             
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="userCSharp msxsl"
                >
    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/">
        <!--Capture the result of the XSLT Transform into a variable-->
        <xsl:variable name="xmlTree">
            <xsl:call-template name="CreateXml">
            </xsl:call-template>
        </xsl:variable>
        <!--Use a C# function to 'escape' the XML-->
        <xsl:value-of select="userCSharp:EscapeXml($xmlTree)"/>
    </xsl:template>

    <!--Do your transform here, using XSLT, Functoids, etc--> 
    <xsl:template name="CreateXml">
        <Result>
            <xsl:copy-of select="/*" />
        </Result>
    </xsl:template>

    <msxsl:script language="C#" implements-prefix="userCSharp">
    <![CDATA[
      // Escape the outer Xml
      public System.String EscapeXml(System.Xml.XPath.XPathNavigator x)
      {
        return x.OuterXml;
      } 
     ]]>
    </msxsl:script>

</xsl:stylesheet>

Upvotes: 3

Related Questions