Fo...">

XSD for simple content with attribute and text

How can I check the text length of an element with an attribute. For instance:

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

Now I need to limit the possible values ​​of the code attribute (for example, "FB", "BB", "TT") and also I need to limit the possible values ​​and length of the text ("Football", "BasketBall", "TableTennis"), as well as the maximum the length of this text (Soccer, BasketBall, TableTennis) can be 20.

I tried using

 <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 can’t check the length of the text “Foolball” (also possible values) Could you please help how to check both the code and the text. Thanks

+6
xml validation xsd restriction
source share
4 answers

I had this identical question, and I was hoping when I saw that there was an accepted answer. However, this answer is exactly what I tried, but I got the wrong schema error. Apparently, you cannot restrict simpleContent inside complexType , but extend it. In addition, you cannot have both an attribute and simpleContent inside complexType . By acquiring examples in books throughout the office, I came up with a fix that I adapted to this issue if someone else comes across this issue 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> 
+12
source share

My item:

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

Does not check the correct enumeration values, 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> 
+1
source share

The solution to your problem is to create a simpleContent with extension , whose base limited in one way and whose attribute limited in 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> 

Validates the required 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 needed because the names of the sports are listed.

Note No. 2: This pattern does not provide a correlation between the name of the sport and the sports 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 will need to use Schematron or XSD 1.1.

0
source share

An element must be a complex type in which the content is line-based and limited to enumeration in the same way as for an attribute. Also, when you are restricting to an enumeration, this implies a maximum length.

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

EDIT: added full 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> 
-1
source share

All Articles