使用 XML 获取当前日期
我所在的 XML 课程有这个额外的学分。
我们正在测试将课程添加到时间表的日期,以确保它是在 1964 年 9 月 1 日或之后以及今天的当前日期之前或当天。
XLST 代码:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="DateAddedType">
<xs:restriction base="xs:dateTime">
<xs:minInclusive value="1964-09-01T00:00:00" />
<!--<xs:assert test="$value le current-date()"/>-->
<xs:maxInclusive value="Date.NowT00:00:00"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="DepartmentType">
<xs:restriction base="xs:string">
<xs:pattern value ="[A-z]{2,5}&?" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name ="DeptType2">
<xs:restriction base="xs:string">
<xs:pattern value ="[A-Z][A-Z]|[A-Z][A-Z][A-Z&]|[A-Z][A-Z][A-Z][A-Z&]" />
</xs:restriction>
</xs:simpleType>
<xs:element name="Courses">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Course">
<xs:complexType>
<xs:sequence>
<xs:element name="Department" type="DepartmentType" />
<xs:element name="CourseNumber" type="xs:string" />
<xs:element name="Title" type="xs:string" />
<xs:element name="Description" type="xs:string" />
<xs:element name="Credits" type="xs:unsignedByte" />
<xs:element minOccurs="0" name="Prerequisites" type="xs:string" />
<xs:element name="DateAdded" type="DateAddedType" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="Schedule">
<xs:complexType>
<xs:sequence>
<xs:element name="Season" type="xs:string" />
<xs:element name="Year" type="xs:string" />
<xs:element name="ItemNumber" type="xs:string" />
<xs:element name="Department" type="DepartmentType" />
<xs:element name="CourseNumber" type="xs:string" />
<xs:element name="Section" type="xs:string" />
<xs:element name="Room" type="xs:string" />
<xs:element name="MeetingDays" type="xs:string" />
<xs:element name="StartTime" type="xs:dateTime" />
<xs:element name="EndTime" type="xs:dateTime" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML 代码:
<?xml version="1.0" encoding="UTF-8"?>
<Courses xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Courses.xsd">
<Course>
<Department>CIS</Department>
<CourseNumber>141</CourseNumber>
<Title>intro to access</Title>
<Description>db stuff</Description>
<Credits>5</Credits>
<Prerequisites>108, 123</Prerequisites>
<DateAdded>2011-07-01T00:00:00</DateAdded>
</Course>
<Course>
<Department>cap</Department>
<CourseNumber>102</CourseNumber>
<Title>Nutrition for the Food Service Professional</Title>
<Description>Nutrition for the Food Service Professional</Description>
<Credits>5</Credits>
<DateAdded>2001-01-01T00:00:00</DateAdded>
</Course>
<Course>
<Department>cap</Department>
<CourseNumber>103</CourseNumber>
<Title>Applied Food Service Sanitation</Title>
<Description>Applied Food Service Sanitation</Description>
<Credits>5</Credits>
<DateAdded>2001-01-01T00:00:00</DateAdded>
</Course>
<Course>
<Department>cap</Department>
<CourseNumber>104</CourseNumber>
<Title>Supervision in the Hospitality Industry</Title>
<Description>Supervision in the Hospitality Industry</Description>
<Credits>5</Credits>
<DateAdded>2001-01-01T00:00:00</DateAdded>
</Course>
<Course>
<Department>cis</Department>
<CourseNumber>145</CourseNumber>
<Title>Introduction to access</Title>
<Description>Intro to access</Description>
<Credits>5</Credits>
<DateAdded>2001-01-01T00:00:00</DateAdded>
<Schedule>
<Season>Fall</Season>
<Year>2009</Year>
<ItemNumber>0000</ItemNumber>
<Department>cis</Department>
<CourseNumber>145</CourseNumber>
<Section>b</Section>
<Room>34107</Room>
<MeetingDays>daily</MeetingDays>
<StartTime>1899-12-30T08:00:00</StartTime>
<EndTime>1899-12-30T08:50:00</EndTime>
</Schedule>
<Schedule>
<Season>Winter</Season>
<Year>2010</Year>
<ItemNumber>1000</ItemNumber>
<Department>cis</Department>
<CourseNumber>145</CourseNumber>
<Section>d</Section>
<Room>34106</Room>
<MeetingDays>daily</MeetingDays>
<StartTime>1899-12-30T10:00:00</StartTime>
<EndTime>1899-12-30T10:50:00</EndTime>
</Schedule>
</Course>
<Course>
<Department>cis</Department>
<CourseNumber>160</CourseNumber>
<Title>Introduction to Programming</Title>
<Description>Introduction to practices of programming</Description>
<Credits>5</Credits>
<DateAdded>2001-01-01T00:00:00</DateAdded>
<Schedule>
<Season>Winter</Season>
<Year>2010</Year>
<ItemNumber>1200</ItemNumber>
<Department>cis</Department>
<CourseNumber>160</CourseNumber>
<Section>e</Section>
<Room>34107</Room>
<MeetingDays>daily</MeetingDays>
<StartTime>1899-12-30T11:00:00</StartTime>
<EndTime>1899-12-30T11:50:00</EndTime>
</Schedule>
</Course>
<Course>
<Department>cis</Department>
<CourseNumber>245</CourseNumber>
<Title>Applications in Access</Title>
<Description>Developing applications in access</Description>
<Credits>5</Credits>
<DateAdded>2001-01-01T00:00:00</DateAdded>
<Schedule>
<Season>Winter</Season>
<Year>2010</Year>
<ItemNumber>1100</ItemNumber>
<Department>cis</Department>
<CourseNumber>245</CourseNumber>
<Section>c</Section>
<Room>34128</Room>
<MeetingDays>daily</MeetingDays>
<StartTime>1899-12-30T09:00:00</StartTime>
<EndTime>1899-12-30T09:50:00</EndTime>
</Schedule>
</Course>
<Course>
<Department>cis</Department>
<CourseNumber>266</CourseNumber>
<Title>VB.Net Applications</Title>
<Description>Applications in vb.net</Description>
<Credits>5</Credits>
<DateAdded>2001-01-01T00:00:00</DateAdded>
<Schedule>
<Season>Spring</Season>
<Year>2010</Year>
<ItemNumber>3525</ItemNumber>
<Department>cis</Department>
<CourseNumber>266</CourseNumber>
<Section>c</Section>
<Room>34107</Room>
<MeetingDays>daily</MeetingDays>
<StartTime>1899-12-30T09:00:00</StartTime>
<EndTime>1899-12-30T09:50:00</EndTime>
</Schedule>
</Course>
<Course>
<Department>cna</Department>
<CourseNumber>250</CourseNumber>
<Title>Routing and Switching</Title>
<Description>Introduction to Routing and Switching to design, build, and maintain small to medium-size networks.</Description>
<Credits>5</Credits>
<Prerequisites>cna-145</Prerequisites>
<DateAdded>2001-01-01T00:00:00</DateAdded>
</Course>
<Course>
<Department>cna</Department>
<CourseNumber>260</CourseNumber>
<Title>Windows Scripting</Title>
<Description>Windows Scripting</Description>
<Credits>5</Credits>
<DateAdded>2001-01-01T00:00:00</DateAdded>
</Course>
<Course>
<Department>cs</Department>
<CourseNumber>142</CourseNumber>
<Title>Object Oriented Programming I</Title>
<Description>Object Oriented Programming I</Description>
<Credits>5</Credits>
<Prerequisites>cis-160, cis-168</Prerequisites>
<DateAdded>2001-03-01T00:00:00</DateAdded>
</Course>
</Courses>
dateAdded 字段是正在测试的字段。相当于 [DateAdded >= 9/1/1964 AND DateAdded <= Today] 通过使用 MinInclusive 来测试 1964 年的日期,我们将使用 maxInclusive 来测试今天的日期,但我们被告知要使用 DTD操纵架构。是的,这样做很愚蠢,但既然可以做到,老师就希望我们这样做。
但是,他没有介绍如何创建通过使用 DTD 文件的 XSLT 文件进行验证的 XML 文件。我们已经完成了带有 DTD 的 XML 和带有 XSLT 的 XML,但还没有完成这三者。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 XSD 1.0 中,无法参数化模式。文件要么有效,要么无效。它们不可能今天有效明天无效。所以没有办法做你正在寻找的事情。
您可以在 XSD 1.1 中使用断言来完成此操作:
In XSD 1.0, there is no way of parameterizing the schema. Documents are either valid or they are not. They can't be valid today and invalid tomorrow. So there's no way of doing what you are looking for.
You can do it in XSD 1.1 using assertions:
<xs:assert test="$value le current-date()"/>