【发布时间】:2014-05-02 18:53:30
【问题描述】:
我想对 studentId 创建一个限制。所有 Id 都以 2 个大写字母开头,后跟 6 个数字。
这是我的 XML:
<resource studentId="BB244663" type="course">
<course>Advanced Mechanics</course>
<courseNumber>764839211-19-H</courseNumber>
</resource>
这是我制定的架构规则
<xs:element name="resource">
<xs:complexType>
<xs:sequence>
<xs:element name="course" type="xs:string"/>
<xs:element name="courseNumber" type="xs:string"/>
</xs:sequence>
<xs:attribute name="studentId"> <!-- rule starts here ###-->
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z] [A-Z] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute> <!-- rule ends here ###-->
<xs:attribute name="type" type="xs:string" use="required"/>
</xs:complexType>
每次我尝试验证 XML 文件时都会抛出此错误:
cvc-pattern-valid: Value 'BB244663' is not facet-valid with respect to pattern '[A-Z] [A-Z] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]' for type '#AnonType_studentIdresource'. [13]
cvc-attribute.3: The value 'BB244663' of attribute 'studentId' on element 'resource' is not valid with respect to its type, 'null'. [13]
我在网上看了,但我看不出这个限制有什么问题,除了可能是因为它是一个属性而不是一个元素?
【问题讨论】:
-
模式值中的空格是有意的吗?不会 pattern='[A-Z]{2}[0-9]{6}' 工作吗?
-
@MarvinSmit 傻傻的我!是的,把它放在一起工作:)!把它作为一个问题发布,我会给你正确的答案
标签: xml validation schema xsd