Xml 条件语句无法工作-XPath 2.0
我正在尝试完成一个家庭作业,在ovedueCell和categoryCell类中,我在XPath 2.0中使用以下格式执行两个条件语句。每次运行代码时都会出现错误:缺少必需的属性“select”Xml 条件语句无法工作-XPath 2.0,xml,functional-programming,xpath-2.0,Xml,Functional Programming,Xpath 2.0,我正在尝试完成一个家庭作业,在ovedueCell和categoryCell类中,我在XPath 2.0中使用以下格式执行两个条件语句。每次运行代码时都会出现错误:缺少必需的属性“select” if (test1) then result1 else if (test2) then result2 else if (test3) then result3 else result4 我对body和h2标记下面的formatDate函数也有问题 <?x
if (test1) then result1
else if (test2) then result2
else if (test3) then result3
else result4
我对body和h2标记下面的formatDate函数也有问题
<?xml version="1.0" encoding="UTF-8" ?>
<!--
New Perspectives on XML, 3rd Edition
Tutorial 8
Tutorial Project
Denison Public Library XSLT Style Sheet
Author: Brigitte Arcoite
Date: 08/04/2019
Filename: library.xsl
Supporting Files: book.png, dvd.png
-->
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="html"
doctype-system="about:legacy-compat"
encoding="UTF-8"
indent="yes" />
<xsl:variable name="thisDate" select="2017-04-12" as="xs:date" />
<xsl:template match="/">
<html>
<head>
<title>Denison Public Library</title>
<link href="libstyles.css" rel="stylesheet" type="text/css"
/>
</head>
<header>
<h1>Denison Public Library</h1>
<h2>Ennis, Montana</h2>
</header>
<body>
<h2>
<xsl:value-of select="formatDate($thisDate, '[MNn] [D],
[Y]')" />
</h2>
<h1>Checked Out Items</h1>
<table id="checkoutList">
<thead>
<tr>
<th>Call No.</th>
<th>Title</th>
<th>Due Date</th>
<th>Overdue? (Y/N)</th>
<th>Overdue Category</th>
</tr>
</thead>
<tbody>
<xsl:apply-templates select="itemlist/item" />
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="item">
<tr>
<td class="callnoCell">
<xsl:value-of select="callno" />
</td>
<td class="titleCell">
<xsl:value-of select="title" />
</td>
<td class="duedateCell">
<xsl:value-of select="status/@return" />
</td>
<td class="overdueCell">
<xsl:variable name="overdue" select="
if($thisDate>status/@return)
then $overdue='Y'
else $overdue='N'" />
<xsl:value-of select="$overdue" />
</td>
<td class="categoryCell">
<xsl:variable name="lostDate" select="$thisdate-
dayTimeDuration(90)" as="xs:date" />
<xsl:variable name="longoverdueDate" select="$thisdate-
dayTimeDuration(30)" as="xs:date" />
<xsl:variable name="category" select="
if($lostDate>status/@return) then $category='Lost'
else if ($longoverdueDate>status/@return then
$category='Long Overdue'
else if (status/@return>$thisDate) $category='Overdue'
else $category='' " />
<xsl:value-of select="$category" />
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
函数名应拼写为format date,而不是format date dayTimeDuration上的调用应以xs:为前缀,减号周围需要空格,因为连字符可以出现在名称中 对$thisdate的引用应为$thisdate 我怀疑,但我猜这段代码:
<xsl:variable name="overdue" select="
if($thisDate>status/@return)
then $overdue='Y'
else $overdue='N'" />
应该是
<xsl:variable name="overdue" select="
if($thisDate>status/@return)
then 'Y'
else 'N'" />
<xsl:variable name="category" select="
if($lostDate>status/@return) then 'Lost'
else if ($longoverdueDate>status/@return then
'Long Overdue'
else if (status/@return>$thisDate) then 'Overdue'
else '' " />
但更好的是,为什么不将其设置为布尔变量,而不是设置为“Y”或“N”的字符串
同样地
<xsl:variable name="category" select="
if($lostDate>status/@return) then $category='Lost'
else if ($longoverdueDate>status/@return then
$category='Long Overdue'
else if (status/@return>$thisDate) $category='Overdue'
else $category='' " />
应该是
<xsl:variable name="overdue" select="
if($thisDate>status/@return)
then 'Y'
else 'N'" />
<xsl:variable name="category" select="
if($lostDate>status/@return) then 'Lost'
else if ($longoverdueDate>status/@return then
'Long Overdue'
else if (status/@return>$thisDate) then 'Overdue'
else '' " />
通过这些更改,我们消除了所有语法错误,但仍然存在语义错误:
<xsl:variable name="thisDate" select="2017-04-12" as="xs:date" />
报告该值是整数而不是日期。这显示了声明变量类型的价值—您在这里做过,但不是在任何地方都做过。它应该是select=xs:date'2017-04-12'
另外,我无法解释为什么您会收到特定的错误消息select属性丢失。也许你还解决了另一个问题