【问题标题】:XSD for simplecontent with attribute and text带有属性和文本的简单内容的 XSD
【发布时间】:2011-07-24 08:30:15
【问题描述】:

如何验证具有属性的元素的文本长度。 例如:

    <sport code="FB">Football</sport>

现在我需要限制代码属性的可能值(如“FB”、“BB”、“TT”) 我还需要限制文本的可能值和长度(“足球”、“篮球”、“乒乓球”)以及这些文本的最大长度(“足球”、“篮球”、“乒乓球”)可以是20.

我试过了

<complexType name="sport">
  <simpleContent>
    <extension base="string">
        <attribute name="code" type="code" />
    </extension>
  </simpleContent>
</complexType>
<simpleType name="code">
    <restriction base="string">
        <enumeration value="FB" />
        <enumeration value="BB" />
        <enumeration value="TT" />
    </restriction>
</simpleType>

但我无法验证文本“Foolball”的长度(也是可能的值) 您能否帮助了解如何验证代码和文本。 谢谢

【问题讨论】:

标签: xml validation xsd restriction


【解决方案1】:

我有同样的问题,当我看到有一个被接受的答案时充满希望。但是,这个答案正是我尝试过的,但我得到了一个模式无效的错误。显然,您不能将simpleContent 限制在complexType 内,只能扩展它。此外,complexType 中不能同时具有属性和 simpleContent。在办公室周围的书籍中搜索示例,我想出了一个解决方法,我适应了这个问题,以防其他人将来遇到这个问题:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:sp="http://www.ckhrysze.net/sports/1.0"
            targetNamespace="http://www.ckhrysze.net/sports/1.0"
        >

  <xsd:element name="sports">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="sport" type="sp:sportType" minOccurs="1" maxOccurs="unbounded" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:complexType name="sportType">
    <xsd:simpleContent>
      <xsd:extension base="sp:sportEnumeration">
        <xsd:attribute name="code" type="sp:codeEnumeration" />
      </xsd:extension>
    </xsd:simpleContent>
  </xsd:complexType>

  <xsd:simpleType name="sportEnumeration">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="Football" />
      <xsd:enumeration value="Basketball" />
    </xsd:restriction>
  </xsd:simpleType>

  <xsd:simpleType name="codeEnumeration">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="FB" />
      <xsd:enumeration value="BB" />
    </xsd:restriction>
  </xsd:simpleType>

</xsd:schema>

【讨论】:

  • 这实际上对我有用,与上述解决方案相反。
  • @Chris,你说你不能在 complexType 中限制 simpleContent 是什么意思? 7.3.2.2. Derivation by restriction 章节展示了如何...
【解决方案2】:

我的元素:

<xsd:element name="From" type="FromType" minOccurs="0" maxOccurs="1"/>

不验证正确的枚举值接受所有。

<xsd:complexType name="FromType">
        <xsd:simpleContent>
            <xsd:extension base="FromTypeEnum">
                <xsd:attribute name="acronym" type="xsd:string"/>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>

    <xsd:simpleType name="FromTypeEnum">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="Discard">
                <xsd:annotation>
                    <xsd:documentation>Discard</xsd:documentation>
                </xsd:annotation>
            </xsd:enumeration>
            <xsd:enumeration value="SANDBOX">
                <xsd:annotation>
                    <xsd:documentation>SANDBOX</xsd:documentation>
                </xsd:annotation>
            </xsd:enumeration>
            <xsd:enumeration value="Catalogue">
                <xsd:annotation>
                    <xsd:documentation>Catalogue</xsd:documentation>
                </xsd:annotation>
            </xsd:enumeration>
            <xsd:enumeration value="Current experimentation">
                <xsd:annotation>
                    <xsd:documentation>Current experimentation</xsd:documentation>
                </xsd:annotation>
            </xsd:enumeration>
            <xsd:enumeration value="Current session">
                <xsd:annotation>
                    <xsd:documentation>Current session</xsd:documentation>
                </xsd:annotation>
            </xsd:enumeration>
            <xsd:enumeration value="Restart">
                <xsd:annotation>
                    <xsd:documentation>Restart</xsd:documentation>
                </xsd:annotation>
            </xsd:enumeration>
        </xsd:restriction>
    </xsd:simpleType>   

【讨论】:

    【解决方案3】:

    解决您的问题的方法是创建一个带有 extensionsimpleContent,其 base 受到一种约束,而其 attribute 受到另一种约束。

    XML 架构

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="sport" type="sportType" />
    
        <xs:complexType name="sportType">
            <xs:simpleContent>
                <xs:extension base="sportNameType">
                    <xs:attribute name="code" type="sportCodeType" />
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    
        <xs:simpleType name="sportNameType">
            <xs:restriction base="xs:string">
                <xs:enumeration value="Football" />
                <xs:enumeration value="Basketball" />
                <xs:maxLength value="20"/>
            </xs:restriction>
        </xs:simpleType>
    
        <xs:simpleType name="sportCodeType">
            <xs:restriction base="xs:string">
                <xs:enumeration value="FB" />
                <xs:enumeration value="BB" />
            </xs:restriction>
        </xs:simpleType>
    </xs:schema>
    

    正确验证所需的 XML 实例文档

    <?xml version="1.0" encoding="UTF-8"?>
    <sport code="FB">Football</sport>
    

    注意 #1:maxLength 元素满足您的要求之一,但实际上并不是必需的,因为枚举了运动名称。

    注意 #2:此架构不强制运动名称和运动代码之间的关联。换句话说,这个模式也验证了这个 XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <sport code="BB">Football</sport>
    

    要解决此问题,您需要使用 Schematron 或 XSD 1.1。

    【讨论】:

      【解决方案4】:

      元素必须是复杂类型,其内容基于字符串,并以与属性相同的方式受枚举限制。此外,当您通过枚举进行限制时,这意味着最大长度。

      附带说明,不要为类型和元素/属性使用相同的名称,因为这会造成混淆。

      编辑:添加了一个完整的示例:

      <element name="sport" type="tns:SportType" />
      
      <complexType name="SportType">
          <simpleContent>
              <restriction base="string">
                  <enumeration value="Football" />
                  <enumeration value="Basketball" />
              </restriction>
          </simpleContent>
          <attribute name="code" type="tns:CodeType" />
      </complexType>
      
      <simpleType name="CodeType">
          <restriction base="string">
              <enumeration value="FB" />
              <enumeration value="BB" />
          </restriction>
      </simpleType>
      

      【讨论】:

      • 谢谢。我已将元素修改为“complexttype”,并尝试在复杂类型“sport”下添加运动名称(“Football”)的“限制”。但我找不到正确的方法。
      • 这似乎没有验证。我在attribute 标记上收到错误“'属性'和内容模型是互斥的”。请参阅stackoverflow.com/questions/376582/… 以获得更好的答案。
      • attribute 和 contentModel 互斥
      • @Thiyanesh 请不要接受这个答案,因为它显然是不正确的。
      • @forty-2 完成,但响应非常延迟
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多