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

the*_*axe 1 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>
Run Code Online (Sandbox Code Playgroud)

我希望我的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>
Run Code Online (Sandbox Code Playgroud)

这是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
Run Code Online (Sandbox Code Playgroud)

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

谢谢你的指导.

Ant*_*nes 6

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

<xsl:parameter name="showdate"/>
Run Code Online (Sandbox Code Playgroud)

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

<xsl:when test="@date = $showdate">
Run Code Online (Sandbox Code Playgroud)

为了传递参数,您需要使用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
Run Code Online (Sandbox Code Playgroud)

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

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

xml.documentElement.setAttribute("_showDate", "20091203")
Run Code Online (Sandbox Code Playgroud)

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

<xsl:template match="rota">
  <xsl:variable name="showdate" select="@_showDate" />
    <html> ...
         <xsl:when test="@date = $showdate">
Run Code Online (Sandbox Code Playgroud)

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