passing a variable to XSLT from .vb file - without

2019-09-15 19:27发布

问题:

How to pass the constant variable to xslt , here I hard coded the college name in the xslt, but I don't want to do that way instead I want to pass that college name "CollegeName" as string.

< xsl: template match="/">
   < xsl:for-each select="Analysis">
   <html>
  <body>
  <table>
  <tr>
            <td width='100%' class='subLogo'>Rowan-cabarrus Comm College</td>
        </tr>
</table>
</body>
</html>
     </xsl:for-each>
  </xsl:template>

.VB file

Dim CollegeName as Constant = Rowan-cabarrus Comm College

Dim reader As New XmlTextReader(New System.IO.StringReader(xmlstring))

    reader.Read()
    Dim objXSLTransform As New XslCompiledTransform()
    objXSLTransform.Load(xsltFilePath)
    Dim htmlOutput As New StringBuilder()
    Dim htmlWriter As TextWriter = New StringWriter(htmlOutput)
    objXSLTransform.Transform(reader, Nothing, htmlWriter)
    reader.Close()
    Return htmlOutput.ToString()

回答1:

In order to pass parameters to an XSLT stylesheet you can use the XSLTArgumentList

 Dim argList as XsltArgumentList = new XsltArgumentList()

 argList.AddParam("CollegeName", "", "Rowan-cabarrus Comm College")

Then the argList is used as a parameter in the call to Transform

objXSLTransform.Transform(reader, argList, htmlWriter)

And in your XSLT, you just need to define a correspond parameter using xsl:param, which should be added as a child of the xsl:stylesheet element

<xsl:param name="CollegeName"/>

You can use this in the same way as any variable, with the $ prefix

<td width='100%' class='subLogo'><xsl:value-of select="$CollegeName" /></td>

See http://msdn.microsoft.com/en-us/library/dfktf882(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 for an example.