希望这将是一个简单的修复。我是XSL的新手,但我设法得到一个按日期对我的数据进行排序的小脚本。我现在想使用ASP将这些数据限制在一个月内的所有条目(这通过使用DateDiff()函数)。当我加载XSL脚本输出时,它说我的XML格式错误。有人可以帮帮我吗?
这是我的XML文件:
<?xml version="1.0"?>
<Rota>
<Shift>
<date>23/07/2010</date>
<title1>GM Neurosciences</title1>
<gm>Katie Cusick</gm>
<title2>Chief Operating Officer</title2>
<director>Patrick Mitchell</director>
<nurse>n/a</nurse>
</Shift>
<Shift>
<date>30/07/2010</date>
<title1>GM Specialised Medicine</title1>
<gm>Alison Watson</gm>
<title2>DDO Medicine & Cardio.</title2>
<director>Suzanne Marsello</director>
<nurse>n/a</nurse>
</Shift>
</Rota>
这是我的XSL文件:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="no" version="1.0" />
<xsl:param name="sortBy" select="'date'"/>
<xsl:param name="strXPath" select="//Shift"/>
<xsl:template match="/">
<xsl:apply-templates select="$strXPath">
<xsl:sort select="substring(date,7,4)"/> <!-- year sort -->
<xsl:sort select="substring(date,4,2)"/> <!-- month sort -->
<xsl:sort select="substring(date,1,2)"/> <!-- day sort -->
</xsl:apply-templates>
</xsl:template>
<xsl:template match="Shift">
<Shift>
<date><xsl:value-of select="date"/></date>
<title1><xsl:value-of select="title1"/></title1>
<gm><xsl:value-of select="gm"/><gm>
<title2><xsl:value-of select="title2"/></title2>
<director><xsl:value-of select="director"/></director>
<nurse><xsl:value-of select="nurse"/></nurse>
</Shift>
</xsl:template>
</xsl:stylesheet>
这是我的ASP代码:
<%
Set entries = Server.CreateObject("Microsoft.XMLDOM")
Set entry = Server.CreateObject("Microsoft.XMLDOM")
Set xml = Server.CreateObject("Microsoft.XMLDOM")
Set xsl = Server.CreateObject("Microsoft.XMLDOM")
Set newxml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False
xml.load (Server.MapPath("rota.xml"))
xsl.async = False
xsl.load(Server.MapPath("indexrota.xsl"))
newxml.async = False
newxml.load(xml.transformNode(xsl))
set entries = newxml.getElementsbyTagName("Shift")
noOfEntries = entries.length
startDate = "15/07/2010"
LastDate = DateAdd("m",1,date())
Response.Write("<table><tr><th>Date</th><th>Title</th><th>GM</th><th>Title</th><th>Director</th><th>Nurse / Matron</th></tr>")
For i = 0 To (noOfEntries - 1)
Set entry = entries.item(i)
If isDate(entry.childNodes(0).text) Then
meh = CDate(entry.childNodes(0).text)
beginning = DateDiff("d", meh, startDate)
ended = DateDiff("d", meh, LastDate)
If beginning + 1 <= 0 And ended >= 0 Then
Response.Write("<tr>"&_
"<td>" & entry.childNodes(0).text & "</td>"&_
"<td>" & entry.childNodes(1).text & "</td>"&_
"<td>" & entry.childNodes(2).text & "</td>"&_
"<td>" & entry.childNodes(3).text & "</td>"&_
"<td>" & entry.childNodes(4).text & "</td>"&_
"<td>" & entry.childNodes(5).text & "</td>"&_
"</tr>")
End If
End If
Next
Response.Write("</table>")
%>
我确定这是XSL的一个例子,因为ASP之前已经完善了XML
答案 0 :(得分:1)
您的XSL存在两个问题。
首先,在复制gm元素的地方,你没有正确关闭gm标签。
<gm><xsl:value-of select="gm"/><gm>
这应该成为
<gm><xsl:value-of select="gm"/></gm>
其次,althuogh您正在复制Shift元素,您生成的XML不包含根元素。您需要在您的xsl:apply-templates调用
之外放置一个Rota元素<xsl:template match="/">
<Rota>
<xsl:apply-templates select="$strXPath">
<xsl:sort select="substring(date,7,4)"/> <!-- year sort -->
<xsl:sort select="substring(date,4,2)"/> <!-- month sort -->
<xsl:sort select="substring(date,1,2)"/> <!-- day sort -->
</xsl:apply-templates>
</Rota>
</xsl:template>
请注意,只需执行此操作即可简化Shift元素的复制
<xsl:template match="Shift">
<xsl:copy-of select="." />
</xsl:template>
ASP代码也存在问题。 newxml.load(xml.transformNode(xsl))行不正确。因为transformNode返回包含XML的字符串,所以您确实需要执行以下操作
newxml.loadXml(xml.transformNode(xsl))
从文件加载xml时使用加载。加载包含Xml
的字符串时,请使用 loadXml答案 1 :(得分:1)
此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Rota">
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<xsl:sort select="substring(date,7)"/>
<xsl:sort select="substring(date,4,2)"/>
<xsl:sort select="substring(date,1,2)"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
使用此输入:
<Rota>
<Shift>
<date>30/07/2010</date>
<title1>GM Specialised Medicine</title1>
<gm>Alison Watson</gm>
<title2>DDO Medicine & Cardio.</title2>
<director>Suzanne Marsello</director>
<nurse>n/a</nurse>
</Shift>
<Shift>
<date>23/07/2010</date>
<title1>GM Neurosciences</title1>
<gm>Katie Cusick</gm>
<title2>Chief Operating Officer</title2>
<director>Patrick Mitchell</director>
<nurse>n/a</nurse>
</Shift>
</Rota>
输出:
<Rota>
<Shift>
<date>23/07/2010</date>
<title1>GM Neurosciences</title1>
<gm>Katie Cusick</gm>
<title2>Chief Operating Officer</title2>
<director>Patrick Mitchell</director>
<nurse>n/a</nurse>
</Shift>
<Shift>
<date>30/07/2010</date>
<title1>GM Specialised Medicine</title1>
<gm>Alison Watson</gm>
<title2>DDO Medicine & Cardio.</title2>
<director>Suzanne Marsello</director>
<nurse>n/a</nurse>
</Shift>
</Rota>
此外,此样式表有效:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<xsl:sort select="substring(date,7)"/>
<xsl:sort select="substring(date,4,2)"/>
<xsl:sort select="substring(date,1,2)"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
注意:正确的子字符串。我更改输入顺序,因为默认顺序是升序。如果您希望后代订单添加到xsl:sort
此属性order="descending"
。
编辑:还原了错误的子字符串。另外,为什么不用XSLT做所有事情?例如,这个样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="startDate" select="20100715"/>
<xsl:param name="endDate" select="$startDate + 100"/>
<xsl:template match="Rota">
<table>
<tr>
<th>Date</th>
<th>Title</th>
<th>GM</th>
<th>Title</th>
<th>Director</th>
<th>Nurse / Matron</th>
</tr>
<xsl:apply-templates select="Shift[concat(substring(date,7),
substring(date,4,2),
substring(date,1,2))
>= $startDate]
[$endDate >=
concat(substring(date,7),
substring(date,4,2),
substring(date,1,2))]">
<xsl:sort select="substring(date,7)"/>
<xsl:sort select="substring(date,4,2)"/>
<xsl:sort select="substring(date,1,2)"/>
</xsl:apply-templates>
</table>
</xsl:template>
<xsl:template match="Shift">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="Shift/*">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
</xsl:stylesheet>
输出:
<table>
<tr>
<th>Date</th>
<th>Title</th>
<th>GM</th>
<th>Title</th>
<th>Director</th>
<th>Nurse / Matron</th>
</tr>
<tr>
<td>23/07/2010</td>
<td>GM Neurosciences</td>
<td>Katie Cusick</td>
<td>Chief Operating Officer</td>
<td>Patrick Mitchell</td>
<td>n/a</td>
</tr>
<tr>
<td>30/07/2010</td>
<td>GM Specialised Medicine</td>
<td>Alison Watson</td>
<td>DDO Medicine & Cardio.</td>
<td>Suzanne Marsello</td>
<td>n/a</td>
</tr>
</table>