11

How can i validate the text length of the element that has an attribute. E.g:

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

Now i need to restrict the possible values of the code attribute(like "FB", "BB", "TT") and also i need to restrict the possible values and length of the text("Football", "BasketBall", "TableTennis") and also the maximum length of these text("Football", "BasketBall", "TableTennis") can be 20.

I tried with

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

But i cant validate the length of the text "Foolball" (also the possible values) Can you please help about how to validate both the code and the text. Thanks

Thiyanesh
  • 519
  • 1
  • 6
  • 14
  • possible duplicate of [XML Schema: Element with attributes containing only text?](http://stackoverflow.com/questions/376582/xml-schema-element-with-attributes-containing-only-text) – Martijn Feb 05 '13 at 13:39

4 Answers4

17

I had this identical question, and was hopeful when I saw there was an accepted answer. However, that answer is exactly what I tried, but I was getting a schema invalid error. Apparently, you can't restrict simpleContent within a complexType, only extend it. Additionally, you can't have both an attribute and simpleContent within a complexType. Searching for examples in books around the office, I came up with a fix, which I adapted to this question in case someone else has this problem in the future:

<?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>
zx485
  • 28,498
  • 28
  • 50
  • 59
Chris
  • 866
  • 9
  • 19
  • This actually worked for me, as opposed to the above solutions. – Nels Beckman Oct 18 '13 at 10:04
  • @Chris , what do you mean saying _you can't restrict simpleContent within a complexType_? [7.3.2.2. Derivation by restriction](https://docstore.mik.ua/orelly/xml/schema/ch07_03.htm) chapter shows howto... – Boolean_Type May 04 '19 at 00:30
2

The solution to your problem is to create a simpleContent with an extension whose base is constrained one way and whose attribute is constrained another.

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

correctly validates the desired XML instance document:

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

Note #1: The maxLength element meets one of your requirements, but is not really necessary because the sport names are enumerated.

Note #2: This schema does not enforce the correlation between sport name and sport code. In other words, this schema also validates this XML:

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

To fix this, you would either need to use Schematron or XSD 1.1.

james.garriss
  • 12,959
  • 7
  • 83
  • 96
1

My Element:

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

Not validates corractly values of enumeration 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>   
cubiczx
  • 19
  • 3
-1

The element must be a complex type where the content is based on string and restricted by enumeration in th same manner as for the attribute. Also, when you restrict by enumeration, this implies a maximum length.

As a side note, do not use the same names for types and elements/attributes since this is confusing.

EDIT: Added a complete example:

<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>
forty-two
  • 12,204
  • 2
  • 26
  • 36
  • Thanks. i have modified the element to "complexttype" and tried adding the "restriction" for the sport names("Football") under the complex type "sport". But i couldnt exactly find the correct way. – Thiyanesh Mar 28 '11 at 10:03
  • This doesn't seem to validate. I get the error "**'attribute' and content model are mutually exclusive**" on the `attribute` tag. See http://stackoverflow.com/questions/376582/xml-schema-element-with-attributes-containing-only-text for a better answer. – Martijn Feb 05 '13 at 13:37
  • attribute and contentModel are mutual exclusive – om471987 May 24 '13 at 14:53
  • @Thiyanesh Please unaccept this answer, since it is obviously incorrect. – forty-two Jul 29 '14 at 21:25
  • @forty-two Done, though very delayed response – Thiyanesh May 29 '17 at 20:23