XSL Tricks

 Uncategorized  Comments Off on XSL Tricks
Feb 112015
 

I have mentioned this a couple of times before:

I HATE FUNCTIODS

I have reverted to using XSL for nearly all mapping requirements (except straight across mapping)

I have created a simple map that maps pretty much straight across using XSL:

Here is one of the functiods:

This has always worked, the issue is that if I change the context, ALL of my Inline XSLT scripts are broken:

What I have done before is go into each scripting functiod and change the XSLT. – What a pain.

Here I am going to show you a ‘trick’ to save yourself the headache.

We are going to use Inline XSLT Call Template

I have always had an issue with a Call Template, “I am creating a template to use, but nothing is ‘reusable’ in my mind” For instance: my output to FName isn’t reusable in any other scenario.

We are going to show how it can be reusable!

I am going to create a mapping for the FName, but to make it reusable, I am going to ‘pass’ into the call template the source node and output name:

Now for the fun, I want to create this template so I can reuse it, so here is what I have written:

The code:

 <xsl:element name="($OutputNode)">

is essentially saying: I want the real value of this variable as an output

All I have to do is copy the contents of this script and create new functiods and just put the output node name as an argument in the scripting functiod

Here is my map

Here is the underlying XSL

<?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" version="1.0" xmlns:ns0="http://StottCreations.com/XSL.DataDefinition">
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
  <xsl:template match="/">
    <xsl:apply-templates select="/ns0:Input" />
  </xsl:template>
  <xsl:template match="/ns0:Input">
    <ns0:Output>
      <Line>
        <xsl:call-template name="MakeOutput">
          <xsl:with-param name="InputValue" select="string(Record/@FirstName)" />
          <xsl:with-param name="OutputNode" select="&quot;FName&quot;" />
        </xsl:call-template>
        <xsl:call-template name="MakeOutput">
          <xsl:with-param name="InputValue" select="string(Record/@LastName)" />
          <xsl:with-param name="OutputNode" select="&quot;LName&quot;" />
        </xsl:call-template>
        <xsl:call-template name="MakeOutput">
          <xsl:with-param name="InputValue" select="string(Record/@Address)" />
          <xsl:with-param name="OutputNode" select="&quot;AddressLine&quot;" />
        </xsl:call-template>
        <xsl:call-template name="MakeOutput">
          <xsl:with-param name="InputValue" select="string(Record/@City)" />
          <xsl:with-param name="OutputNode" select="&quot;City&quot;" />
        </xsl:call-template>
        <xsl:call-template name="MakeOutput">
          <xsl:with-param name="InputValue" select="string(Record/@State)" />
          <xsl:with-param name="OutputNode" select="&quot;State&quot;" />
        </xsl:call-template>
        <xsl:call-template name="MakeOutput">
          <xsl:with-param name="InputValue" select="string(Record/@Zip)" />
          <xsl:with-param name="OutputNode" select="&quot;ZipCode&quot;" />
        </xsl:call-template>
      </Line>
    </ns0:Output>
  </xsl:template>
  <xsl:template name="MakeOutput">
 <xsl:param name="InputValue" />
 <xsl:param name="OutputNode" />
 <xsl:element name="($OutputNode)">
   <xsl:value-of select="$InputValue" />
 </xsl:element>
</xsl:template>
</xsl:stylesheet>

Now if I change the context, I don’t have to remap anything!

And here is the underlying XSL (without me having to go in an change anything in the scripting functiods)

<?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" version="1.0" xmlns:ns0="http://StottCreations.com/XSL.DataDefinition">
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
  <xsl:template match="/">
    <xsl:apply-templates select="/ns0:Input" />
  </xsl:template>
  <xsl:template match="/ns0:Input">
    <ns0:Output>
      <xsl:for-each select="Record">
        <Line>
          <xsl:call-template name="MakeOutput">
            <xsl:with-param name="InputValue" select="string(@FirstName)" />
            <xsl:with-param name="OutputNode" select="&quot;FName&quot;" />
          </xsl:call-template>
          <xsl:call-template name="MakeOutput">
            <xsl:with-param name="InputValue" select="string(@LastName)" />
            <xsl:with-param name="OutputNode" select="&quot;LName&quot;" />
          </xsl:call-template>
          <xsl:call-template name="MakeOutput">
            <xsl:with-param name="InputValue" select="string(@Address)" />
            <xsl:with-param name="OutputNode" select="&quot;AddressLine&quot;" />
          </xsl:call-template>
          <xsl:call-template name="MakeOutput">
            <xsl:with-param name="InputValue" select="string(@City)" />
            <xsl:with-param name="OutputNode" select="&quot;City&quot;" />
          </xsl:call-template>
          <xsl:call-template name="MakeOutput">
            <xsl:with-param name="InputValue" select="string(@State)" />
            <xsl:with-param name="OutputNode" select="&quot;State&quot;" />
          </xsl:call-template>
          <xsl:call-template name="MakeOutput">
            <xsl:with-param name="InputValue" select="string(@Zip)" />
            <xsl:with-param name="OutputNode" select="&quot;ZipCode&quot;" />
          </xsl:call-template>
        </Line>
      </xsl:for-each>
    </ns0:Output>
  </xsl:template>
  <xsl:template name="MakeOutput">
 <xsl:param name="InputValue" />
 <xsl:param name="OutputNode" />
 <xsl:element name="($OutputNode)">
   <xsl:value-of select="$InputValue" />
 </xsl:element>
</xsl:template>
</xsl:stylesheet>