我是 XSLT 的新手,但最近我不得不编写自己的 xslt 来处理 XML,我写了一些东西,但我想问一下专业人士,如果我做得好,或者还有什么我可以做的提升。
我知道 XSLT 使用模板和东西,但在我的 XSLT 中,我认为测试中有太多可重复的东西......可以减少吗?
这是我的 XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="Today" />
<xsl:param name="ViewType" />
<xsl:template match="/">
<table>
<tr>
<th>Priority</th>
<th>Title</th>
<th>Due date</th>
</tr>
<xsl:apply-templates select="All_Results/Result">
<xsl:sort select="duedate" />
</xsl:apply-templates>
</table>
</xsl:template>
<xsl:template match="Result">
<xsl:variable name="varOverdue" select="ddwrt:DateTimeTick(ddwrt:GenDisplayName(string(duedate))) < ddwrt:DateTimeTick(ddwrt:GenDisplayName(string($Today)))" ddwrt:cf_explicit="1"/>
<xsl:variable name="varOverduePlus14" select="ddwrt:DateTimeTick(ddwrt:GenDisplayName(string(duedate))) < ddwrt:DateTimeTick(ddwrt:GenDisplayName(string($Today)))+12096000000000" ddwrt:cf_explicit="1"/>
<xsl:variable name="varOverdueToday" select="ddwrt:DateTimeTick(ddwrt:GenDisplayName(string(duedate))) = ddwrt:DateTimeTick(ddwrt:GenDisplayName(string($Today)))" ddwrt:cf_explicit="1"/>
<xsl:choose>
<xsl:when test="$ViewType='active'">
<xsl:if test="($varOverdue=0) and (taskstatus ='In Progress')">
<tr>
<td><xsl:value-of select="priority"/></td>
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="url"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="title"/>
</xsl:attribute>
</a>
</td>
<td>
<xsl:value-of select="duedate"/>
</td>
</tr>
</xsl:if>
</xsl:when>
<xsl:when test="$ViewType='overdue'">
<xsl:if test="($varOverdue=1) and (taskstatus !='Completed')">
<tr>
<td><xsl:value-of select="priority"/></td>
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="url"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="title"/>
</xsl:attribute>
</a>
</td>
<td>
<xsl:attribute name="style">
color:red;
</xsl:attribute>
<xsl:value-of select="duedate"/>
</td>
</tr>
</xsl:if>
</xsl:when>
<xsl:when test="$ViewType='upcoming'">
<xsl:if test="(taskstatus !='Completed') and ($varOverduePlus14=1)">
<tr>
<td><xsl:value-of select="priority"/></td>
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="url"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="title"/>
</xsl:attribute>
</a>
</td>
<td>
<xsl:if test="$varOverdue=1">
<xsl:attribute name="style">
color:red;
</xsl:attribute>
</xsl:if>
<xsl:value-of select="duedate"/>
</td>
</tr>
</xsl:if>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
XML 如下所示:
<All_Results>
<Result>
<id>1</id>
<workid>2258</workid>
...
</Result>
<Result>
<id>2</id>
<workid>4537</workid>
....
</Result>
...
</All_Results>
那么有没有办法改进我的 XSLT 来解析当前的 XML?