如何为 XSD 日期设置自定义模式?

How can I set up a custom pattern for an XSD date?

我正在尝试将以下模式放入 XSD 架构中: 2020 年 1 月 1 日

我尝试使用模式标签,但我什至无法验证最简单的 dd/mm/yyyy 格式。 有没有办法实现上面的模式,只有月份可以在那里,而不仅仅是一个简单的字符串? 我还尝试将其作为字符串的基础并对其进行模式限制,但我无法弄清楚如何实现它。

我正在使用 XML 1.0

我的XSD:

  <xs:simpleType name="ReleaseYearDesc">
    <xs:restriction base="xs:gYear">
      <xs:minInclusive value="1900"/>
      <xs:maxInclusive value="2020"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="DateAddedDesc">
    <xs:restriction base="xs:date">
      <xs:pattern value=""/>
    </xs:restriction>
  </xs:simpleType>
  <xs:complexType name="DatesDescription">
    <xs:sequence>
      <xs:element name="ReleaseYear" type="ReleaseYearDesc" minOccurs="1" maxOccurs="1"/>
      <xs:element name="DateAdded" type="DateAddedDesc" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
  </xs:complexType>

我想要的结果:

  <Dates>
    <ReleaseYear>2020</ReleaseYear>
    <DateAdded>September 9, 2019</DateAdded>
  </Dates>

我在我的 XSD 文件中调用下面的元素,我认为没有必要包含该部分。

您不能使用内置 XML 架构简单类型来描述字符串的单独部分。一个简单的类型定义需要描述整个值。这里唯一的方法是使用模式面创建 xs:string 的限制。

遇到这种情况我的标准做法是:

  1. 理解问题
  2. 了解language/tool
  3. 使用 language/tool
  4. 找到问题的解决方案

在你的例子中,语言是 XML 模式,语言规范的相关部分在这里:https://www.w3.org/TR/xmlschema-2/#rf-pattern

Schema Representation Constraint: Multiple patterns If multiple element information items appear as [children] of a , the [value]s should be combined as if they appeared in a single ·regular expression· as separate ·branch·es. Note: It is a consequence of the schema representation constraint Multiple patterns (§4.3.4.3) and of the rules for ·restriction· that ·pattern· facets specified on the same step in a type derivation are ORed together, while ·pattern· facets specified on different steps of a type derivation are ANDed together.

换句话说,您可以在一个简单的类型限制上指定多个模式方面,它们将被视为替代个有效模式。

我认为这样的事情应该可行:

<xs:simpleType name="customDateFormat">
    <xs:restriction base="xs:string">
      <xs:pattern value="January +[0-9], +[0-9]{4}"/>
      <xs:pattern value="February +[0-9], +[0-9]{4}"/>
      <xs:pattern value="March +[0-9], +[0-9]{4}"/>
      <xs:pattern value="April +[0-9], +[0-9]{4}"/>
      <xs:pattern value="May +[0-9], +[0-9]{4}"/>
      <xs:pattern value="June +[0-9], +[0-9]{4}"/>
      <xs:pattern value="July +[0-9], +[0-9]{4}"/>
      <xs:pattern value="August +[0-9], +[0-9]{4}"/>
      <xs:pattern value="September +[0-9], +[0-9]{4}"/>
      <xs:pattern value="October +[0-9], +[0-9]{4}"/>
      <xs:pattern value="November +[0-9], +[0-9]{4}"/>
      <xs:pattern value="December +[0-9], +[0-9]{4}"/>
    </xs:restriction>
</xs:simpleType>

图案允许各部分之间有多个空格,但您可以根据需要调整图案。