如何使用 XML 和 Schematron 比较日期

发布于 2024-11-04 04:31:48 字数 6504 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星光不落少年眉 2024-11-11 04:31:48

找到答案:

XPath 1.0 仅对数字使用比较运算符 <>。因此,您必须将日期转换为数值。

<sch:assert test="translate(@value, '-', '') > translate(//MyData/@dataBeginDate, '-', '')">

或者更明确地说:

<sch:assert test="number(translate(@value, '-', '')) > number(translate(//MyData/@dataBeginDate, '-', ''))">

Found answer:

XPath 1.0 only uses comparison operators < and > for numbers. So you must convert dates to a number value.

<sch:assert test="translate(@value, '-', '') > translate(//MyData/@dataBeginDate, '-', '')">

or more explicitly:

<sch:assert test="number(translate(@value, '-', '')) > number(translate(//MyData/@dataBeginDate, '-', ''))">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文