Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 532
Thiyanesh Avatar asked Mar 28 '11 09:03

Thiyanesh


People also ask

What is XS simpleContent in XSD?

<xsd:simpleContent> ElementContains extensions or restrictions on a complexType element with character data or a simpleType element as content and contains no elements. Copy. <simpleContent id = ID {any attributes with non-schema Namespace}...> Content: (annotation?, ( restriction | extension)) </simpleContent>

What is complexType and simpleType in XSD?

XSD elements can be of type simpleType , complexType , or anyType . An element of type simpleType contains only text. It cannot have attributes and elements. An element of type complexType can contain text, elements, and attributes.

What does complexType mean in XSD?

A complex type is essentially a type definition for elements that may contain attributes and elements. An element can be declared with a type attribute that refers to a complexType element that defines the structure, content, and attributes of that element.


2 Answers

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>
like image 187
Chris Avatar answered Oct 14 '22 21:10

Chris


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>   
like image 37
cubiczx Avatar answered Oct 14 '22 19:10

cubiczx