如何使用 XML 和 Schematron 比较日期
Schematron XML 日期比较不起作用。它返回一个假错误。我想我正在使用 schematron 1.01。我正在使用 C# 和 MSDN 中的 DLL: MSDN schematron 链接
这是错误输出(通过它传递 XSD 的方式)验证):
From pattern "Check co-occurrence constraints"
Assert fails: VisitDate must be within a valid date range as specified in dataBeginDate and dataEndDate
At: /MyData[1]/Site[1]/Patient[1]/form_Baseline[1]/dat_VisitDate[1]
<dat_VisitDate signedWho="System" value="2009-02-02">...</dat_VisitDate>
(Line: 7, Column: 10)
Schematron 规则:
<sch:rule context="MyData/Site/Patient/form_Baseline/dat_VisitDate">
<sch:assert test="@value > //MyData/@dataBeginDate">VisitDate must be within a valid date range as specified in dataBeginDate and dataEndDate</sch:assert>
</sch:rule>
XML:
<?xml version="1.0" encoding="utf-8"?>
<MyData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" dataBeginDate="2008-01-01" dataEndDate="2011-04-26">
<Site SiteKey="11">
<Patient PatientKey="33">
<dat_StatusDate signedWho="System" value="2009-01-01" />
<form_Baseline>
<dat_VisitDate signedWho="System" value="2009-02-02" />
</form_Baseline>
<form_Flowsheet VisitDateKey="2009-03-03">
<dat_OtherDate signedWho="System" value="2009-03-03" />
</form_Flowsheet>
</Patient>
</Site>
</MyData>
XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sch="http://www.ascc.net/xml/schematron">
<xs:annotation>
<xs:appinfo>
<sch:title>Schematron Validation</sch:title>
</xs:appinfo>
</xs:annotation>
<xs:simpleType name="dateMyDate">
<xs:restriction base="xs:date">
<xs:minInclusive value="2000-01-01"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="integerMyKey">
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="9999"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="stringNonEmpty">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="32"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="MyData">
<xs:complexType>
<xs:sequence>
<xs:element name="Site" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="Patient" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="dat_StatusDate" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:attribute name="value" type="dateMyDate" use="required" />
<xs:attribute name="signedWho" type="stringNonEmpty" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="form_Baseline" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="dat_VisitDate" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:annotation>
<xs:appinfo>
<sch:pattern name="Check co-occurrence constraints">
<sch:rule context="MyData/Site/Patient/form_Baseline/dat_VisitDate">
<sch:assert test="@value > //MyData/@dataBeginDate">
VisitDate must be within a valid date range as specified in dataBeginDate and dataEndDate
</sch:assert>
</sch:rule>
</sch:pattern>
</xs:appinfo>
</xs:annotation>
<xs:attribute name="value" type="dateMyDate" use="required" />
<xs:attribute name="signedWho" type="stringNonEmpty" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="form_Flowsheet" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="dat_OtherDate" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:attribute name="value" type="dateMyDate" use="required" />
<xs:attribute name="signedWho" type="stringNonEmpty" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="VisitDateKey" type="dateMyDate" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="PatientKey" type="stringNonEmpty" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="SiteKey" type="integerMyKey" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="dataBeginDate" type="dateMyDate" use="required" />
<xs:attribute name="dataEndDate" type="dateMyDate" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
Schematron XML date comparison in not working. And it returns a false error. I think I am using schematron 1.01. I am using C# and the DLL from MSDN:
MSDN schematron link
Here is the error output (by the way it passes XSD validation):
From pattern "Check co-occurrence constraints"
Assert fails: VisitDate must be within a valid date range as specified in dataBeginDate and dataEndDate
At: /MyData[1]/Site[1]/Patient[1]/form_Baseline[1]/dat_VisitDate[1]
<dat_VisitDate signedWho="System" value="2009-02-02">...</dat_VisitDate>
(Line: 7, Column: 10)
Schematron rule:
<sch:rule context="MyData/Site/Patient/form_Baseline/dat_VisitDate">
<sch:assert test="@value > //MyData/@dataBeginDate">VisitDate must be within a valid date range as specified in dataBeginDate and dataEndDate</sch:assert>
</sch:rule>
XML:
<?xml version="1.0" encoding="utf-8"?>
<MyData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" dataBeginDate="2008-01-01" dataEndDate="2011-04-26">
<Site SiteKey="11">
<Patient PatientKey="33">
<dat_StatusDate signedWho="System" value="2009-01-01" />
<form_Baseline>
<dat_VisitDate signedWho="System" value="2009-02-02" />
</form_Baseline>
<form_Flowsheet VisitDateKey="2009-03-03">
<dat_OtherDate signedWho="System" value="2009-03-03" />
</form_Flowsheet>
</Patient>
</Site>
</MyData>
XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sch="http://www.ascc.net/xml/schematron">
<xs:annotation>
<xs:appinfo>
<sch:title>Schematron Validation</sch:title>
</xs:appinfo>
</xs:annotation>
<xs:simpleType name="dateMyDate">
<xs:restriction base="xs:date">
<xs:minInclusive value="2000-01-01"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="integerMyKey">
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="9999"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="stringNonEmpty">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="32"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="MyData">
<xs:complexType>
<xs:sequence>
<xs:element name="Site" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="Patient" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="dat_StatusDate" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:attribute name="value" type="dateMyDate" use="required" />
<xs:attribute name="signedWho" type="stringNonEmpty" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="form_Baseline" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="dat_VisitDate" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:annotation>
<xs:appinfo>
<sch:pattern name="Check co-occurrence constraints">
<sch:rule context="MyData/Site/Patient/form_Baseline/dat_VisitDate">
<sch:assert test="@value > //MyData/@dataBeginDate">
VisitDate must be within a valid date range as specified in dataBeginDate and dataEndDate
</sch:assert>
</sch:rule>
</sch:pattern>
</xs:appinfo>
</xs:annotation>
<xs:attribute name="value" type="dateMyDate" use="required" />
<xs:attribute name="signedWho" type="stringNonEmpty" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="form_Flowsheet" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="dat_OtherDate" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:attribute name="value" type="dateMyDate" use="required" />
<xs:attribute name="signedWho" type="stringNonEmpty" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="VisitDateKey" type="dateMyDate" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="PatientKey" type="stringNonEmpty" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="SiteKey" type="integerMyKey" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="dataBeginDate" type="dateMyDate" use="required" />
<xs:attribute name="dataEndDate" type="dateMyDate" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到答案:
XPath 1.0 仅对数字使用比较运算符
<
和>
。因此,您必须将日期转换为数值。或者更明确地说:
Found answer:
XPath 1.0 only uses comparison operators
<
and>
for numbers. So you must convert dates to a number value.or more explicitly: