我正在创建XML Schema,我的一个值是一年.因此,我想确保所有值都有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>
Run Code Online (Sandbox Code Playgroud)
如果我正确理解"totalDigits",有人可以传入"publish_year"值"2008"或"200".两者都有效.因此,如何构建我的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>
Run Code Online (Sandbox Code Playgroud)
如果将价值作为附加限制呢?
例如 ?
<xs:minInclusive value="1900"/> et <xs:maxInclusive value="2008"/>
Run Code Online (Sandbox Code Playgroud)
但是要回到totalDigits约束,为什么不将该fixed属性设置为true?
如果{fixed}为true,则当前类型为{base type definition}的类型不能指定除{value}之外的totalDigits的值.
<xs:totalDigits value="4" fixed="true" />
Run Code Online (Sandbox Code Playgroud)