Re: [xsl] Only output element when parameter value is not equal to blank (or null)

Subject: Re: [xsl] Only output element when parameter value is not equal to blank (or null)
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Thu, 01 Feb 2007 13:26:20 +0100
Hi Chris,

You can make your stylesheet miles simpler and shorter by using a different approach. Instead of tons of parameters, use something extensible, like an XML structure (why not, it's all xml anyway). You can then do it the 'xslt way' by defining templates. You code can become this:

<xsl:param name="key-settings">
   <setting name="MASTERCATALOGNAME" value="myvalue" />
   <setting name="PRODUCTID" value="myprodid" />
   ....
</xsl:param>

<xsl:template match="/" >

 <DataService>
    <Identity>
  [...]
    </Identity>
    <Transaction>
       <Command type="Query">
          <MasterCatalogRecord etype="Entity">
              <ExternalKeys>
                  <xsl:apply-template select="$key-settings/setting" />
              </ExternalKeys>
      [...]
    </Transaction>
 </DataService>

</xsl:template>

<xsl:template match="setting">
   <Key name="{@name}" type="string">
      <xsl:value-of select="@value" />
   </Key>
</xsl:template>



That's all there is too it. There is no need to add any xsl:if anymore, as the processor will do the thinking for you. If you don't want to chain the name of the Key to the name of the Setting, you can choose to add an external source that contains a lookup table (also in XML) with all your paramname/keyname mappings. You can get this resource with doc() or document() function.

If you use XSLT 1, you must use the node-set() extension function.

Cheers!

-- Abel Braaksma

Current Thread