4

我正在创建 XML 模式,我的值之一是一年。因此,我想确保所有值都恰好有 4 个字符。为此,我使用以下语法:

<xs:element name="publish_year" maxOccurs="1">
  <xs:simpleType>
    <xs:restriction base="xs:positiveInteger">
      <xs:totalDigits value="4"/>
    </xs:restriction>
  </xs:simpleType>                                      
</xs:element>

如果我正确理解“totalDigits”,有人可以传入“2008”或“200”的“publish_year”值。两者都是有效的。因此,我如何构建我的 XSD 以确保需要 4 位数字?乍一看,我猜我会使用正则表达式,但我想知道我是否忽略了一些已经嵌入的东西(比如“totalDigits”)

更新:

我采用了以下解决方案。这可能有点矫枉过正,但它明白了这一点:

<xs:simpleType>
    <xs:restriction base="xs:positiveInteger">
        <xs:totalDigits value="4" fixed="true"/>
        <xs:minInclusive value="1900"/>
        <xs:pattern value="^([1][9]\d\d|[2]\d\d\d)$"/>
    </xs:restriction>
</xs:simpleType>
4

2 回答 2

7

作为附加限制的价值宝藏怎么样?

( minInclusive - maxInclusive )

例如 ?

<xs:minInclusive value="1900"/> et <xs:maxInclusive value="2008"/>

但是要回到totalDigits约束,为什么不将fixed属性设置为 true ?

如果 {fixed} 为真,则当前类型为 {base type definition} 的类型不能为 totalDigits 指定 {value} 以外的值。

<xs:totalDigits value="4" fixed="true" />
于 2008-11-18T20:17:18.757 回答
0

我会根据需要使用基本类型 xs:gYear,并带有额外的约束方面。有关更多信息,可能会有所帮助。

于 2011-08-24T00:38:22.527 回答