XSD для simplecontent с атрибутом и текстом
Как я могу проверить длину текста элемент, который имеет атрибут. Например:
    <sport code="FB">Football</sport>
теперь мне нужно ограничить возможные значения атрибута кода(например, "FB", "BB", " TT") а также мне нужно ограничить возможные значения и длину текста ("футбол"," баскетбол"," TableTennis"), а также максимальная длина этих текстов ("футбол"," баскетбол"," TableTennis") может быть 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 "( также возможные значения) Не могли бы вы помочь о том, как проверить как код, так и текст. Спасибо
4 ответов
simpleContent внутри complexType, только продлить его. Кроме того, вы не можете иметь как атрибут, так и simpleContent внутри complexType. В поисках примеров в книгах по всему офису я придумал исправление, которое я адаптировал к этому вопросу, если у кого-то еще есть эта проблема в будущее:
<?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"/>
не проверяет значения corractly перечисления принять все.
<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 С extension чей base ограничен одним способом и чей attribute ограничена другой.
этой XML schema:
<?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.
элемент должен быть сложным типом, где содержимое основано на строке и ограничено перечислением таким же образом, как и для атрибута. Кроме того, когда вы ограничиваете перечислением, это подразумевает максимальную длину.
в качестве примечания не используйте одинаковые имена для типов и элементов / атрибутов, так как это сбивает с толку.
EDIT: добавлен полный пример:
<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>