使用XML获取当前日期

时间:2011-11-10 03:34:25

标签: xml dom xsd

我正在使用这一额外功劳的XML课程。

我们正在测试将课程添加到课程表中的日期,以确保课程安排在9/1/1964或之后以及之前或之后的当前日期。

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}&amp;?" />     
        </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&amp;]|[A-Z][A-Z][A-Z][A-Z&amp;]" />     
        </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字段是正在测试的字段。 Equivelant to [DateAdded&gt; = 9/1/1964 AND DateAdded&lt; = Today]利用MinInclusive来测试1964年的日期,我们将使用maxInclusive作为今天的日期,但我们被告知使用DTD来操纵架构。是的,这样做很愚蠢,但由于可以完成,教师希望我们这样做。

但是,他还没有讨论如何通过使用DTD文件的XSLT文件验证XML文件。我们使用DTD和使用XSLT的XML完成了XML,但不是全部三种。

1 个答案:

答案 0 :(得分:1)

在XSD 1.0中,无法参数化架构。文件有效或不有效。它们今天无效,明天无效。所以没有办法做你想要的。

您可以使用断言:<xs:assert test="$value le current-date()"/>

在XSD 1.1中执行此操作