Thiyanesh
Thiyanesh

Reputation: 549

XSD for simplecontent with attribute and text

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

Upvotes: 11

Views: 12212

Answers (4)

Chris
Chris

Reputation: 934

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>

Upvotes: 17

james.garriss
james.garriss

Reputation: 13397

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.

Upvotes: 2

cubiczx
cubiczx

Reputation: 19

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>   

Upvotes: 1

forty-two
forty-two

Reputation: 12817

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>

Upvotes: -1

Related Questions