XSD definition for simpleContent type string with attributes - xsd

I would like to define the XSD for:
<Group id="someid" parent="someid">some string</Group>
This is what I tried:
<xs:element name="Group" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="xs:string">
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute name="parent" type="xs:IDREF" use="optional"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>
I use Visual Studio for the XSD design. The validator tells me (while underlining "<xs:restriction"):
"Undefined complexType 'http://w3.org/2001/XMLSchema:string' is used as base for a complex type restriction."

It's needed to use <xs:extension> instead of <xs:restriction>:
<xs:element name="Group" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute name="parent" type="xs:IDREF" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

Related

Can xsd parent element (base) be given default value in child (extension/restriction)?

I'm trying to do something like this:
<xs:complexType name="tSomeComponent">
<xs:sequence>
<xs:element name="table" type="xs:string" />
<xs:element name="key" type="tKey" />
<xs:element name="class" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="tStation">
<xs:complexContent>
<xs:restriction base="tSomeComponent">
<xs:sequence>
<xs:element name="table" type="xs:string" default="station" />
<xs:element name="name" type="xs:string" />
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
I know that table default value in child should be done by restriction but i want to extend and restrict at same time. I know that this can be solved by adding more types but that's an overkill.

XSD : how to restrict attribute inherited from base type

Consider the following xsd. For AddressType, Line1 can be any string. I want to restrict Line1 attribute in USAddressType to follow some regular expression. How can I do this ? Thanks !
<xs:complexType name="AddressType">
<xs:sequence>
<xs:element name="Line1" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="USAddressType">
<xs:complexContent>
<xs:extension base="AddressType">
<xs:sequence>
<xs:element name="State" type="xs:string"/>
<xs:element name="Zipcode" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="Line1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="REGEX HERE"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Limitations of regular expressions used in XML Schemas can be found here

xsd error in the following

here is the xml
<?xml version="1.0" encoding="utf-8"?>
<Modules xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="XSDQu3.xsd">
<Module code="CSE1246">
<Name shortName="ADSA">Applied Data Structures and Algorithms</Name>
<Level>1</Level>
<ResourcePerson>
<Name>Anwar</Name>
<Surname>Chutoo</Surname>
</ResourcePerson>
</Module>
<Module code="CSE2041">
<Name shortName="Web 2">Web Technologies II</Name>
<Level>2</Level>
<ResourcePerson>
<FullName>Shehzad Jaunbuccus</FullName>
</ResourcePerson>
</Module>
</Modules>
i'm having an error at name. a resource person can either contain fullname or name and surname. please help. Am i correctly doing this part
here is the xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="NameNSurnameType">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Surname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ResourcePersonType">
<xs:sequence>
<xs:choice>
<xs:element name="NameNSurnameType" type="NameNSurnameType"/>
<xs:element name="FullName" type="xs:string"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:ID">
<xs:pattern value="CSE(\d{4})"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:complexType name="nameType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="shortName" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="Modules">
<xs:complexType>
<xs:sequence>
<xs:element name="Module" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="nameType"/>
<xs:element name="Level" type="xs:positiveInteger"/>
<xs:element name="ResourcePerson" type="ResourcePersonType"/>
</xs:sequence>
<xs:attribute ref="code" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I can see your confusion; a choice "option" can be almost any particle; a compositor (such as xs:sequence or another xs:choice) would match the description. The minimum you need to do is change the following definition.
<xs:complexType name="ResourcePersonType">
<xs:sequence>
<xs:choice>
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Surname" type="xs:string"/>
</xs:sequence>
<xs:element name="FullName" type="xs:string"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
If you want to reference the name as you seem to want to do it with the global complex type, then one can create a group and reference that instead. Below is a modified XSD consistent with the above scenario:
<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:group name="NameNSurnameType">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Surname" type="xs:string"/>
</xs:sequence>
</xs:group>
<xs:complexType name="ResourcePersonType">
<xs:sequence>
<xs:choice>
<xs:group ref="NameNSurnameType"/>
<xs:element name="FullName" type="xs:string"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:ID">
<xs:pattern value="CSE(\d{4})"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:complexType name="nameType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="shortName" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="Modules">
<xs:complexType>
<xs:sequence>
<xs:element name="Module" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="nameType"/>
<xs:element name="Level" type="xs:positiveInteger"/>
<xs:element name="ResourcePerson" type="ResourcePersonType"/>
</xs:sequence>
<xs:attribute ref="code" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

XSD validation error in #AnonType_Apps

When I tried to validate my XSD it gives the error
S4s-elt-invalid-content.1: The Content Of '#AnonType_Apps' Is Invalid.
Element 'sequence' Is Invalid, Misplaced, Or Occurs Too Often.
don't know what should be done
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name ="Apps">
<xs:complexType>
<xs:attribute name ="List_Type" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="new releases"/>
<xs:enumeration value="top rated"/>
<xs:enumeration value="category list"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name ="Server_IP" type="xs:string" fixed="10.144.50.55"/>
<xs:sequence>
<xs:element name ="App" minOccurs="1" maxOccurs="20">
<xs:complexType>
<xs:attribute name ="device_type" use="optinal" >
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="tablet"/>
<xs:enumeration value="phone"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name ="app_id" type="xs:string"/>
<xs:attribute name ="installed" type="xs:boolean" default="false"/>
<xs:sequence>
<xs:element name ="app_name" type="xs:string"></xs:element>
<xs:element name ="catogry" minOccurs="1" maxOccurs="3"></xs:element>
<xs:element name ="version" type="xs:string"></xs:element>
<xs:element name ="description" type="xs:string"></xs:element>
<xs:element name ="reviews" use="required">
<xs:complexType>
<xs:sequence>
<xs:element name ="review" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:all>
<xs:element name ="reviewer_name" type="xs:string"></xs:element>
<xs:element name ="review_date">
<xs:complexType>
<xs:restriction base="xs:string">
<xs:pattern value="\d{4}\-\d{2}\-\d{2}"/>
</xs:restriction>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name ="review_time">
<xs:complexType>
<xs:restriction base="xs:string">
<xs:pattern value="\d{2}:\d{2}:\d{}"/>
</xs:restriction>
</xs:complexType>
</xs:element>
<xs:element name ="content" type="xs:string"></xs:element>
<xs:element name ="rating">
<xs:complexType>
<xs:restriction base="xs:float">
<xs:minInclusive value="0" />
<xs:maxInclusive value="5" />
</xs:restriction>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Within a complexType element, the content model comes first and the attribute declarations come last. Move the outermost sequence element to make it the first child of the complex type.
You might want to try finding a syntax-aware editor in which to edit schema documents, to alert you to problems of this kind.
Other syntactic issues in this fragment:
Other complexType elements have the same syntax error of putting their attribute declarations first.
If you want the type of rating to be a restriction of xs:float, then you want it to have a simple type, not a complex type.
Ditto for review_time and review_date.
The pattern on the type of review_time is not a legal regular expression: braces may contain information on the minimum and/or maximum occurrences of (strings matching) a subexpression, in a variety of forms, but they must contain something, so \d{} is not a legal expression. Drop the empty braces, or make it \d{1} if you like, or make it say what you want.
The xs:element element has no attribute named use; I think you may mean to say <xs:element name="reviews" minOccurs="1" maxOccurs="1"> ...
The use attribute does not accept the value optinal.
Get an editor!
Your xs:sequence element needs to follow immidiately on the complexType element.
Your review_date and review_time elements should be a simpleType with a restriction.
If you fix those issues your xsd seems validd.

XSD Schema that JAXB would find acceptable

I have a REST XML response that has a chunk of data like this:
<tag>
<total-pages type="integer">5</total-pages>
<previous-page nil="true"></previous-page>
<next-page nil="true"></next-page>
<offset type="integer">5</offset>
</tag>
Now, sometimes the data can come back like this:
<tag>
<total-pages type="integer">5</total-pages>
<previous-page type="integer">0</previous-page>
<next-page type="integer">1</next-page>
<offset type="integer">5</offset>
</tag>
I have been trying to come up with a XSD schema structure that will account for both possibilities that would be acceptable to JAXB.
I have tried:
<tag>
<total-pages type="numeric-type" />
<previous-page type="numeric-type" />
<next-page type="numeric-type" />
<offset type="numeric-type" />
</tag>
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:integer">
<xsd:attribute type="xsd:string" name="type" use="optional">
<xsd:attribute type="xsd:boolean" name="nil" use="optional">
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
but JAXB blows up with an unmarshalling error.
Thoughts on what XSD schema structure I could use to account for the variability in the returning XML (I cannot change the XML, it is coming from a third-party which I have no control).
Thank you,
Perry
I've started by generating one XSD from your XML samples (two in your case):
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="tag">
<xs:complexType>
<xs:sequence>
<xs:element name="total-pages">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:unsignedByte">
<xs:attribute name="type" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="previous-page">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="type" type="xs:string" use="optional" />
<xs:attribute name="nil" type="xs:boolean" use="optional" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="next-page">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="type" type="xs:string" use="optional" />
<xs:attribute name="nil" type="xs:boolean" use="optional" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="offset">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:unsignedByte">
<xs:attribute name="type" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
When you compare with your XSD, you'll notice the use of string instead of integer as the base type. Which explains why you have a problem with unmarshalling: an empty string is not a valid number.
If instead of "nil" you would get an xsi:nil attribute, the XSD would've been generated differently, i.e. it would've had nillable="true" for your elements.
Since you cannot change the XML, then you're stuck with the use of string; alternatively, you could create a union between an empty string and a numeric value... as to how that'll look in JAXB, you can try it...

Resources