【问题标题】:How to provide multiple values for a xs:simpleType?如何为 xs:simpleType 提供多个值?
【发布时间】:2017-08-01 08:07:00
【问题描述】:

所以我目前正在处理一个 XSD 文件,其中包含一个名为 ipaddress 的 simpleType:

  <xs:simpleType name="ipaddress">
    <xs:restriction base ="xs:string">
      <xs:pattern value="((1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]).){3}(1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])"/>
    </xs:restriction>
  </xs:simpleType>

这个适用于我使用的所有 IP 地址。 但我希望 ipaddress 接受 ipaddress 本身或字符串“localhost”。我怎么做?我已经尝试过这样的事情

 <xs:simpleType name="ipaddress">
    <xs:restriction base ="xs:string">
        <xs:choice minOccurs="1" maxOccurs="1">
            <xs:pattern value="((1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]).){3}(1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])"/>    
            <xs:enumeration value="localhost"/>
      <xs:choice>
    </xs:restriction>
  </xs:simpleType>

还是这样的

  <xs:complexType name="ipaddress">    
        <xs:choice minOccurs="1" maxOccurs="1">
            <xs:pattern value="((1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]).){3}(1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])" type="xs:string"/>   
            <xs:enumeration value="localhost" type="xs:string"/>
      <xs:choice>    
  </xs:complexType>

但是在针对 xsd 架构验证我的 xml 文件时,这些解决方案都不起作用。我认为我不知道如何使用xs:choice 是对的 - 我刚开始学习 XSD,我仍然对所有这些标签和元素以及如何正确连接它们感到有些困惑。

【问题讨论】:

    标签: xml xslt xsd xsd-validation


    【解决方案1】:

    根据W3C page on xs:restriction,您可以在一个xs:restriction中使用多个xs:pattern

    注意:包含多个元素的 XML 会在集合中产生一个单一的正则表达式;这个·正则表达式·是作为元素内容的·正则表达式·的“或”。

    所以以下确实有效:

    <xs:simpleType name="ipaddress">
      <xs:restriction base ="xs:string">
        <xs:pattern value="((1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]).){3}(1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])"/>                                
        <xs:pattern value="localhost"/>
      </xs:restriction>
    </xs:simpleType>
    

    【讨论】:

      【解决方案2】:

      我认为选择是针对复杂类型的,对于您的简单类型限制,您可以简单地添加另一个模式

      <xs:simpleType name="ipaddress">
          <xs:restriction base ="xs:string">
              <xs:pattern value="((1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]).){3}(1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])"/>
              <xs:pattern value="localhost"/>
          </xs:restriction>
      </xs:simpleType>
      

      【讨论】:

        猜你喜欢
        • 2013-11-21
        • 1970-01-01
        • 1970-01-01
        • 2014-08-02
        • 1970-01-01
        • 2020-05-30
        • 2017-11-21
        • 1970-01-01
        • 2020-02-12
        相关资源
        最近更新 更多