Xml 带属性和文本的simplecontent的XSD

Xml 带属性和文本的simplecontent的XSD,xml,validation,xsd,restriction,Xml,Validation,Xsd,Restriction,如何验证具有属性的元素的文本长度。 例如: 足球 现在我需要限制代码属性的可能值(如“FB”、“BB”、“TT”) 我还需要限制文本(“足球”、“篮球”、“乒乓球”)的可能值和长度,并且这些文本(“足球”、“篮球”、“乒乓球”)的最大长度可以是20 我试过了 <complexType name="sport"> <simpleContent> <extension base="string"> <attribute name

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

足球
现在我需要限制代码属性的可能值(如“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>

但我无法验证文本“傻瓜球”的长度(以及可能的值) 你能帮我验证一下代码和文本吗。
感谢

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

作为旁注,不要对类型和元素/属性使用相同的名称,因为这很容易混淆

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

<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>

我有一个相同的问题,当我看到有一个公认的答案时,我满怀希望。然而,这个答案正是我所尝试的,但是我得到了一个模式无效的错误。显然,不能将
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>

我的元素:

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

Not验证枚举accept all的正确值

<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>   

丢弃
沙箱
目录
当前实验
本届会议
重新启动

问题的解决方案是创建一个带有
扩展名的
simpleContent
,其
以一种方式约束,其
属性
以另一种方式约束

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 version="1.0" encoding="UTF-8"?>
<sport code="FB">Football</sport>

要解决这个问题,您需要使用Schematron或XSD1.1。

谢谢。我已将元素修改为“complexttype”,并尝试在复杂类型“sport”下添加运动名称(“Football”)的“restriction”。但是我找不到正确的方法。这似乎不正确。我在
属性
标记上得到错误“'attribute'和content model是互斥的”。有关更好的答案,请参阅。属性和内容模型是相互的exclusive@Thiyanesh请不要接受这个答案,因为它显然是不正确的。@四十二个完成了,尽管非常延迟的响应。与上述解决方案相反,这个可能的副本实际上对我有效。@Chris,你说不能在complexType中限制simpleContent是什么意思?第二章展示了如何。。。
<?xml version="1.0" encoding="UTF-8"?>
<sport code="FB">Football</sport>
<?xml version="1.0" encoding="UTF-8"?>
<sport code="BB">Football</sport>