在模式限制中使用现有的 XML 架构类型定义

Use existing XML Schema type definitions in pattern restriction

我正在为现有 XML 格式创建 XML 架构。该架构基于已经生成 XML 和示例输出的代码。

有没有办法在模式限制中使用现有数据类型?

例子一

日期在现有 XML 格式中编码为 YYYYMMDD(例如“20210308”)。这与现有的 XML Schema date type definition which uses YYYY-MM-DD (e.g. "2021-03-08"). The best way to define the type then seems to be a string with a pattern restriction. Can I refer to other types in the pattern, such as gYear or yearFrag 不匹配?那会让我写

<xs:pattern value="&yearFrag;&monthFrag;&dayFrag;" />

而不是

<xs:pattern value="-?([1-9][0-9]{3,}|0[0-9]{3})(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])" />

这可能吗?定义这种类型的最佳方式是什么?

例子二

有一个以 XML 格式编码的字符串,其中包含先前定义的自定义简单类型。我可以在新模式中参考它吗?

示例第一种类型:

<xs:simpleType name="myCustomId">
  <xs:restriction base="xs:string">
    <xs:pattern value="m[0-5]t[0-9]{0,10}" />
  </xs:restriction>
</xs:simpleType>

预期重复使用示例:

<xs:simpleType name="idBasedName">
  <xs:restriction base="xs:string">
    <xs:pattern value="export_&myCustomId;_[crs]\.csv" />
  </xs:restriction>
</xs:simpleType>

这可能吗?最好的定义方式是什么?

Can I refer to other types in the pattern, such as gYear or yearFrag?

不,XML 架构正则表达式子集在此处定义:https://www.w3.org/TR/xmlschema-2/#regexs 它不包括对 XSD 语法片段的任何特殊支持。

There is a string coded in the XML format that contains a previously defined custom simple type. Can I refer to that in the new pattern?

我没有比这更好的了:https://www.w3.org/TR/xmlschema-2/#rf-pattern 特别注意标题为 'Multiple patterns'

的部分