XSL with parameter


Recently, I had an assignment of updating the existing VB 6 codes to send parameters to XSL. It was using another class asp page to use parameters with XSL. I had limited knowledge of XML and XSL and investigated it a little bit, googling a lot.

Reference

Insturction

Using and referring to a variable

<xsl:variable name="minRating" select="6" as="xs:integer" />
Minimum rating: <xsl:value-of select="$minRating" />

Fortunately, we were using MXSML3 and it can pass parameters to XSL.

VB 예제

Dim xslt As New Msxml2.XSLTemplate30
Dim xslDoc As New Msxml2.FreeThreadedDOMDocument30
Dim xmlDoc As New Msxml2.DOMDocument30
Dim xslProc As IXSLProcessor
xslDoc.async = False
xslDoc.load App.Path & "\createProcessor.xsl"
If (xslDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xslDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   Set xslt.stylesheet = xslDoc
   xmlDoc.async = False
   xmlDoc.load App.Path & "\books.xml"
   If (xmlDoc.parseError.errorCode <> 0) Then
      Set myErr = xmlDoc.parseError
      MsgBox("You have error " & myErr.reason)
   Else
      Set xslProc = xslt.createProcessor()
      xslProc.input = xmlDoc
      xslProc.addParameter "param1", "Hello"
      xslProc.Transform
      MsgBox xslProc.output
   End If
End If

<xsl:Choose> example

<xsl:param name="Mode" select="1" />
<xsl:param name="IndustrySectorID" select="0" />
<xsl:param name="SiteID" select="4" />

<xsl:output method="xml" omit-xml-declaration="no" indent="yes" encoding="iso-8859-1"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:choose>
<xsl:when test = "$Mode = 1"><xsl:apply-templates select="/" mode="Map1" /></xsl:when>
<xsl:when test = "$Mode = 2"><xsl:apply-templates select="/" mode="Map2" /></xsl:when>
<xsl:when test = "$Mode = 4"><xsl:apply-templates select="/" mode="Map4" /></xsl:when>
<xsl:when test = "$Mode = 5"><xsl:apply-templates select="/" mode="Map5" /></xsl:when>
</xsl:choose>
</xsl:template>