【问题标题】:XSD:restriction for an attribute keeps throwing me an errorXSD:属性的限制不断向我抛出错误
【发布时间】: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


【解决方案1】:

您的模式并不完全正确。里面的空格其实是作为pattern使用的。

将您的模式值更改为

[A-Z]{2}[0-9]{6}

应该能解决问题。

【讨论】:

  • 再次感谢! [A-Z]{2}[0-9{6} 是否比 xs:length="8" 更好?
  • xs:length 仅限制字符串的大小,该模式更明确地指定“2 个大写字母 A 到 Z”和“6 个数字 0-9”。两者在逻辑上产生相同的结果,CPU 性能方面我不能说直到测试。因此,在做出此选择时,需要更多的编码指南等来发挥作用。
  • 好吧,这是有道理的。再次感谢马文!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-25
  • 1970-01-01
  • 2020-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-10
相关资源
最近更新 更多