Make XSLT params optional

This XSLT snippet makes the param passed into the XSLT file optional.

Rather than have to force params in the url to get a certain view e.g. http://justawebsite.com/shoppinglist/index.xml?id=all&view=tree , default params can be hidden away inside the XSLT file leaving the url to be simplified e.g. http://justawebsite.com/shoppinglist/index.xml

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:param name="view">summary</xsl:param>  

  <xsl:template match="tasks">

    <div id="articles">
      <div id="records">
         <p>123 GO! <xsl:value-of select="$view"/></p>
          <xsl:if test="$view='blue'"><p>great show!</p></xsl:if>
          <xsl:if test="$view='summary'"><p>average show!</p></xsl:if>
        <xsl:apply-templates select="records"/>
      </div>
    </div>
  </xsl:template>
  
  ...

Note: The important thing to remember here is the param declaration is at the top, and outside the template.

test 1):

http://justawebsite2.com/mdynarex/tasks/tasksgeneral/index.xml?id=all&view=blue

output extract 1):

123 GO! blue

great show!
...

test 2):

http://justawebsite2.com/mdynarex/tasks/tasksgeneral/index.xml?id=all

output extract 2):

123 GO! summary

average show!
...

Resources