如何将参数从ASP传递给XSL?

时间:2009-12-03 14:17:56

标签: xslt parameters asp-classic

我正在编写一个简单的asp页面,以显示基于某些XML的团队rota。这是XML:

<rota>
<shift date="20091201" primary="Chan" secondary="John" notes="notes"></shift>
<shift date="20091202" primary="Mike" secondary="Alex" notes="notes"></shift>
<shift date="20091203" primary="Ross" secondary="Mike" notes="notes"></shift>
<shift date="20091204" primary="Neil" secondary="Ross" notes="notes"></shift>
</rota>

我希望我的asp页面显示今天的rota详细信息,以及未来几天的详细信息。从那以后,我希望能够在将来设置一天,看看谁在努力,然后我希望能够将ASP的YYYYMMDD日期传递给处理XML的XSL。

这是我到目前为止的XSL,现在突出显示硬编码的“当前”日期:

<xsl:template match="rota">
    <html>
    <head>
        <title>Team Rota</title>
        <LINK type="text/css" rel="stylesheet" href="http://www.csfb.net/homepage/global/scripts/csfb_intranet.css"/> 
    </head>
    <body>
    <table border="1">
    <TR><TH>Date</TH><TH>Primary</TH><TH>Secondary</TH><TH>Notes</TH></TR>
    <xsl:for-each select="shift">
        <tr>
            <xsl:choose>
                <xsl:when test="@date = '20091203'">
                    <td bgcolor='FFF0F0'><xsl:value-of select="@date"/></td>
                </xsl:when>
                <xsl:otherwise>
                    <td><xsl:value-of select="@date"/></td>
                </xsl:otherwise>
            </xsl:choose>
            <td><xsl:value-of select="@primary"/></td>
            <td><xsl:value-of select="@secondary"/></td>
            <td><xsl:value-of select="@notes"/></td>
        </tr>       
    </xsl:for-each>
    </table>
    </body>
    </html>
</xsl:template>

这是ASP,还没有传递任何参数:

''// Load the XML
sourcefile = "rota.xml"
set source = Server.CreateObject("Microsoft.XMLDOM")
source.async = false
source.load(sourceFile)

''// Load the XSL
styleFile = Server.MapPath("rota.xsl")
set style = Server.CreateObject("Microsoft.XMLDOM")
style.async = false
style.load(styleFile)

htmltext = source.transformNode(style)
Response.Write htmltext

那么我如何a)将参数传递给XSL并b)选择该参数并在XSL中使用它?

感谢任何指导。

2 个答案:

答案 0 :(得分:6)

在你xsl中,在所有模板之前在顶部创建一个xsl:parameter

<xsl:parameter name="showdate"/>

您现在可以像对待变量一样对待它: -

<xsl:when test="@date = $showdate">

为了传递参数,您需要使用XSL处理器对象,该对象允许您在处理之前添加参数。反过来,您从XSL模板对象的实例获得处理器。反过来,您需要一个FreeThreadedDOMDocument来分配模板样式表参数。因此代码更复杂: -

 Dim xsl : Set xsl = CreateObject("MSXML2.FreeThreadedDOMDocument.3.0")
 xsl.async = false
 xsl.load xslFile

 Dim xml : Set xml = CreateObject("MSXML2.DOMDocument.3.0")
 xml.async = false
 xml.load sourceFile

 Dim xslTemplate : Set xslTemplate = CreateObject("MSXML2.XSLTemplate.3.0")
 xslTemplate.stylesheet = xsl;

 Dim processor : Set processor = xslTemplate.createProcessor()
 processor.addParameter "showDate", "20091203"
 processor.input = source
 processor.transform()  

 Response.Write processor.output

很容易将Response对象分配给处理器的输出参数,该参数运行良好且效率更高。但是,最近的MSXML Service Pack使得这种技术与ASP在Response对象中的IStream实现不兼容。

那就是你如何正式地做到这一点,我通常是如何将一些任意属性注入源XML的根节点然后使用变量: -

xml.documentElement.setAttribute("_showDate", "20091203")

现在您可以在主模板中使用变量而不是参数: -

<xsl:template match="rota">
  <xsl:variable name="showdate" select="@_showDate" />
    <html> ...
         <xsl:when test="@date = $showdate">

在这种方法中,您可以使用已经使用的变换代码,这更简单。

答案 1 :(得分:1)

您应该声明您的参数:

<xsl:stylesheet ... >
  <xsl:parameter name="your_parameter"/>
  <xsl:template match="rota">
      $your_parameter = <xsl:value-of select="$your_parameter" />
  ...

并像这样传递

'// Load the XSL
styleFile = Server.MapPath("rota.xsl")
set style = Server.CreateObject("MSXML2.FreeThreadedDOMDocument.3.0")
style.async = false
style.load(styleFile)

Set template = Server.CreateObject("MSXML2.XSLTemplate.3.0")
template.stylesheet = style

Set processor = myTemplate.createProcessor()
processor.addParameter "your_parameter", "value"
processor.input = source
processor.output = Response
processor.transform()
相关问题