如何从ASP参数传递给XSL? [英] How do I pass a parameter from ASP to XSL?

查看:106
本文介绍了如何从ASP参数传递给XSL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个简单的ASP页面显示基于XML的一些团队轮班。这里的XML:

I'm writing a simple asp page to show a team rota based on some XML. Here's the 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网页中显示今天的罗塔细节和可能的细节在接下来的几天。因为后来我想能够设置一个有朝一日,看看谁的工作旁边,我希望能够从ASP传递一个YYYYMMDD日期的处理XML的XSL。

I want my asp page to show today's rota details and maybe the details for the next couple of days. Since later on I'd like to be able to set a day in the future to see who's working around then I want to be able to pass a YYYYMMDD date from the ASP to the XSL that processes the XML.

这里的XSL我到目前为止,只突出了硬codeD当前日期现在:

Here's the XSL I have so far, just highlighting the hardcoded 'current' date for now:


<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,还没有传递任何参数:

and here's the ASP, not yet passing any parameters:

''// 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

那么,如何一)参数传递给XSL和b)拿起参数,并在XSL使用它?

So how do I a) pass a parameter to the XSL and b) pick up that parameter and use it in the XSL?

感谢您的任何指导。

推荐答案

在你XSL创建一个 XSL。

In you xsl create a xsl:parameter at the top before all your templates.

<xsl:parameter name="showdate"/>

现在你可以把这个就像你将一个变量: -

You can now treat this like you would a variable:-

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

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

In order to pass a parameter in you need to use an XSL processor object which allows you to add the parameter before processing. In turn you get a processor from an instance of an XSL Template object. In turn you need a FreeThreadedDOMDocument to assign to the templates stylesheet parameter. Hence the code is more complex:-

 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的最新服务包已经做了这样不兼容的技术与ASP的响应对象实施的IStream。

Its tempting to assign the Response object to the processor's output parameter which works quite well and is more efficient. However recent Service packs of MSXML have made such a technique incompatible with ASP's implementation of IStream in the Response object.

因此​​多数民众赞成你怎么做正式的,我是怎么做的,通常是到源XML的根节点注入一些arbitary属性然后使用一个变量: -

So thats how you do it officially, how I do it normally is to inject some arbitary attribute on to the source XML's root node then use a variable:-

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

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

Now you can use a variable instead of a parameter inside your main template:-

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

在此方法,您可以使用您已经使用变换code这要简单得多。

In this approach you can use the transform code you are already using which is much simpler.

这篇关于如何从ASP参数传递给XSL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆