XSD extend restricted type - xsd

Can you have an allready restricted type and then derive from this type by extension and add elements that do not fit to a base type?
<xsd:complexType name="absHcontainerType">
<xsd:complexContent>
<xsd:restriction base="e:urContentType">
<xsd:sequence>
<xsd:element ref="e:absMcontainer" minOccurs="0"
maxOccurs="1" />
<xsd:element ref="e:absHtitle" minOccurs="1" maxOccurs="unbounded" />
<xsd:choice minOccurs="1" maxOccurs="unbounded">
<xsd:element ref="e:absMcontainer" />
<xsd:element ref="e:absHcontainer" />
<xsd:element ref="e:absContainer" />
</xsd:choice>
</xsd:sequence>
<xsd:attributeGroup ref="e:typehcontainer" />
<xsd:attributeGroup ref="e:anyattr" />
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
And then derive this like this:
<xsd:complexType name="absHcontainerType2">
<xsd:complexContent>
<xsd:extension base="absHcontainerType">
<xs:sequence>
<xs:element name="xy" type="xs:string"/>
<xs:element name="xyz" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xsd:complexContent>
</xsd:complexType>

As long as you don't put back things that got restricted away, I wouldn't expect a problem. Are you in fact getting an error when you try it?

Related

JAXB Not unmarshalling correctly optional Long element (SAXParseException2: For input string: "")

I've found similar questions but some of the conditions don't apply to my case or the answers do not work.
I created a CXF (3.2.1) SOAP client to consume an external web service so I don't have control over the WSDL and schemas.
The relevant part (not the entire schema) of the service schema is:
<xsd:complexType name="LegalEntityService1">
<xsd:annotation>
<xsd:appinfo source="http://xmlns.oracle.com/adf/svc/metadata/">
<key xmlns="http://xmlns.oracle.com/adf/svc/metadata/">
<attribute>LegalEntityId</attribute>
</key>
</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element minOccurs="0" name="LegalEntityId" type="xsd:long" sdoXML:dataType="sdoJava:LongObject"/>
<xsd:element minOccurs="0" name="PartyId" type="xsd:long" sdoXML:dataType="sdoJava:LongObject"/>
<xsd:element minOccurs="0" name="LegalEntityIdentifier" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="Name" type="xsd:string"/>
<xsd:element default="1" minOccurs="0" name="GeographyId" type="xsd:long" sdoXML:dataType="sdoJava:LongObject"/>
<xsd:element minOccurs="0" name="TransactingEntityFlag" nillable="true" type="xsd:boolean"/>
<xsd:element minOccurs="0" name="EffectiveFrom" nillable="true" type="ns0:date-Date"/>
<xsd:element minOccurs="0" name="EffectiveTo" nillable="true" type="ns0:date-Date"/>
<xsd:element minOccurs="0" name="ObjectVersionNumber" type="xsd:int" sdoXML:dataType="sdoJava:IntObject"/>
<xsd:element minOccurs="0" name="ActivityCode" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="SubActivityCode" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="TypeOfCompany" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="RegistrationCodeLe" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="RegistrationCodeEtb" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="RegistrationCodeLeValue" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="RegistrationCodeEtbValue" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="JurisdictionName" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="PlaceOfRegistration" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="LegalEmployerFlag" nillable="true" type="xsd:boolean"/>
<xsd:element minOccurs="0" name="ParentPsuId" nillable="true" type="xsd:long"/>
<xsd:element minOccurs="0" name="PsuFlag" nillable="true" type="xsd:boolean"/>
<xsd:element minOccurs="0" name="JurisdictionId" nillable="true" type="xsd:long"/>
<xsd:element minOccurs="0" name="GeographyId1" type="xsd:long" sdoXML:dataType="sdoJava:LongObject"/>
<xsd:element minOccurs="0" name="TerritoryShortName" type="xsd:string"/>
<xsd:element minOccurs="0" name="TerritoryCode" type="xsd:string"/>
<xsd:element minOccurs="0" name="Country" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="LegalAddress" type="xsd:string"/>
<xsd:element minOccurs="0" name="Name1" type="xsd:string"/>
<xsd:element minOccurs="0" name="LegalEntityId1" type="xsd:long" sdoXML:dataType="sdoJava:LongObject"/>
I'm specifically having problems with:
<xsd:element minOccurs="0" name="LegalEntityId1" type="xsd:long" sdoXML:dataType="sdoJava:LongObject"/>
It is minOccurs=0 so it should allow an empty tag but when it unmarshalles the following response it fails with javax.xml.ws.soap.SOAPFaultException: Unmarshalling Error: For input string: "".
The annotation seems correct as it is required=false.
#XmlElementRef(name = "LegalEntityId1", namespace = "http://xmlns.oracle.com/apps/financials/legalEntity/legalEntities/legalEntityService/", type = JAXBElement.class, required = false)
protected JAXBElement<Long> legalEntityId1;
I don't understand why it is failing. I know the problem is LegalEntityId1 because I checked it with the debugger.
EDIT #1
Here is the entire stacktrace https://pastebin.com/jS9Pu4rB.
This issue was due to the type of the element.
<LegalEntityId1></LegalEntityId1> this is not a Long format because it is parsed as "" empty string.
if you need instead a "null" meaning, that in xml is "nil" you should change your xsd in <xsd:element minOccurs="0" name="LegalEntityId1" type="xsd:long" nillable="true" /> that in xml will be <LegalEntityId1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> .

Is It Impossible to have elements with same name in xsd?

My XML looks like . I have two elements with same name. But I am not being able to get those elements in XSD.
Here Book is the element that appears twice but has different attributes. All attributes in their respective Book elements are required.
The error says
Element Books is not consistent with element Books
XML :
<TestRoot>
<Test Shelf="1">
<Value>
<Book Name="Wolves" />
</Value>
</Test>
<Test Shelf="2">
<Value>
<Book Name="Dogs" Pages="500" Photos="50" />
</Value>
</Test>
</TestRoot>
My XSD looks like :
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema....>
<xsd:element name="TestRoot">
<xsd:complexType>
<xsd:sequence minOccurs="1" maxOccurs="1">
<xsd:element name="Test" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType>
<xsd:choice>
<xsd:element name="Value">
<xsd:complexType>
<xsd:choice minOccurs="1" maxOccurs="1">
<xsd:element name="Book">
<xsd:complexType>
<xsd:attribute name="Name" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="40"/>
<xsd:enumeration value="Wolves"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="Book">
<xsd:complexType>
<xsd:attribute name="Book" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="40"/>
<xsd:enumeration value="Dogs"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="Pages" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="Photos" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="24"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:choice>
<xsd:attribute name="Shelf" use="optional">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="100"/>
<xsd:enumeration value="1"/>
<xsd:enumeration value="2"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Not sure where I am wrong ? Any help?
Try something like this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="TestRoot" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="TestRoot" msdata:IsDataSet="true" msdata:Locale="en-US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Test">
<xs:complexType>
<xs:sequence>
<xs:element name="Value" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<!-- the <Book> node is just of "BookType" type -->
<xs:element name="Book" type="BookType" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Shelf" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
<!-- define the "BookType" which represents a single <Book> node -->
<xs:complexType name="BookType">
<xs:attribute name="Name" type="xs:string" use="required" />
<xs:attribute name="Pages" type="xs:string" />
<xs:attribute name="Photos" type="xs:string" />
</xs:complexType>
</xs:schema>
This XSD defines a new complex type BookType which is used to define how the <Book> nodes inside <Value> look like.
In general, I would also define a complex type for any of the XML nodes you have - I'd create a ValueType to handle how a <Value> node is made up, and I'd create a TestType for <Test> and a TestRootType for the <TestRoot> as well.
Having those really deeply nested XSD structures makes it really hard to "grasp" what's going on - defining a type for each node and then assigning them to the nodes makes it much more readable and understandable, in my opinion

XSD choice inside all

You can't put choice tag inside the all tag. So, is there any workaround to get this functionallity?
For example, I have<settings> tag like:
<settings>
<logging />
<sending />
<useonly />
</settings>
Or something like
<settings>
<logging />
<notuseonly />
<sending />
</settings>
So I want to prevent <useonly> and <notuseonly> showing up together, while the order is not important. And if allowed, in XSD it would look like:
<xs:all>
<xs:element minOccurs="0" maxOccurs="1" ref="sending" />
<xs:element minOccurs="0" maxOccurs="1" ref="logging" />
<xs:choice>
<xs:element minOccurs="0" maxOccurs="1" ref ="useonly" />
<xs:element minOccurs="0" maxOccurs="1" ref ="notuseonly" />
</xs:choice>
</xs:all>
Any thoughts?
Check this link: http://www.w3.org/wiki/Needs_choice_inside_all
I summarize for you the solutions proposed:
One solution is to wrap the element that can change inside another:
<xsd:all>
<xsd:element minOccurs="0" maxOccurs="1" ref="sending" />
<xsd:element minOccurs="0" maxOccurs="1" ref="logging"/>
<xsd:element minOccurs="0" maxOccurs="1" ref ="usetype"/>
</xsd:all>
<xsd:element name="usetype">
<xsd:complexType>
<xsd:choice>
<xsd:element ref="useonly"/>
<xsd:element ref="notuseonly"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
The other one is to use a substitution group:
<xsd:all>
<xsd:element ref="sending"/>
<xsd:element ref="logging"/>
<xsd:element ref="usetype"/>
</xsd:all>
</xsd:complexType>
<xsd:element name="usetype" abstract="true"/>
<xsd:element name="useonly" substitutionGroup="usetype"> ... </xsd:element>
<xsd:element name="notuseonly" substitutionGroup="usetype"> ... </xsd:element>

Any order + constraints for each element

I am trying to create an XSD, which allows child elements to be in any order. But each child element has its own minOccurs and maxOccurs.
My XSD:
<xsd:complexType name="Samples">
<xsd:sequence >
<xsd:element name="Sample1" minOccurs="1" maxOccurs="1">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:boolean" />
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="Sample2" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string" />
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
For Example a valid XML:
<Samples>
<Sample2></Sample2>
<Sample1></Sample1>
<Sample2></Sample2>
</Samples>
For Example a not valid XML (Sample1 can be choose only one time):
<Samples>
<Sample2></Sample2>
<Sample1></Sample1>
<Sample2></Sample2>
<Sample1></Sample1>
</Samples>
But i don't know, how i can mix the order, while all elements have its own constraint.
Thanks for help
What if you try this:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Samples">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Sample2" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="Sample1" minOccurs="1" maxOccurs="1"/>
<xsd:element name="Sample2" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Of course, you should add your restrictions to each element:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="Sample1Type">
<xsd:simpleContent>
<xsd:extension base="xsd:boolean" />
</xsd:simpleContent>
</xsd:complexType>
<xsd:complexType name="Sample2Type">
<xsd:simpleContent>
<xsd:extension base="xsd:string" />
</xsd:simpleContent>
</xsd:complexType>
<xsd:element name="Samples">
<xsd:complexType>
<xsd:sequence>
<xsd:element type="Sample2Type" name="Sample2" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element type="Sample1Type" name="Sample1" minOccurs="1" maxOccurs="1"/>
<xsd:element type="Sample2Type" name="Sample2" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
OR even shorter for simple types:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Samples">
<xsd:complexType>
<xsd:sequence>
<xsd:element type="xsd:string" name="Sample2" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element type="xsd:boolean" name="Sample1" minOccurs="1" maxOccurs="1"/>
<xsd:element type="xsd:string" name="Sample2" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
P.S.::
There are unfortunately no XSD elements like xsd:sequence, which will allow, what you ask.
What you want looks like xsd:all; however, since xsd:all only works for elements for which maxOccurs is 1, it will not work here. If you can combine choices and sequences to constrain your content, one way to go around duplication of definition (avoid defining twice) is to define Sample1 and Sample 2 as global elements and then ref them under your compositors. If defining global elements is not an option except for your root elements, then you could at least define the types globally - this way you maximize the reuse, and somewhat getting closer to your requirement "without defining twice"...

How to validate textfield element in xsd:schema

I am using xsd:schema which will be used to generated desired xml, I have a title field in xsd:schema.
I want to validate it from xsd:schema only that whenever user try to put values more than 10 characters, it will generate the error.
Below is the part of my xsd:schema
<xsd:sequence>
<xsd:element name="Title" minOccurs="0" maxOccurs="1" type="xsd:normalizedString"/>
<xsd:element name="City" minOccurs="0" maxOccurs="1" type="tcmi:SimpleLink">
<xsd:annotation>
<xsd:appinfo>
<tcm:linktype>ComponentLink</tcm:linktype>
<tcm:AllowMultimediaLinks>false</tcm:AllowMultimediaLinks>
<tcm:AllowedTargetSchemas>
<tcm:TargetSchema xlink:href="tcm:227-190428-8" xlink:title="City"/>
</tcm:AllowedTargetSchemas>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="FlightLinkText" minOccurs="0" maxOccurs="1" type="xsd:normalizedString"/>
</xsd:sequence>
I means that can we validate it from <xsd:element name="Title" minOccurs="0" maxOccurs="1" type="xsd:normalizedString"/>
Please suggest!
Have you tried something like:
<xsd:element name="Title" minOccurs="0" maxOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:normalizedString">
<xsd:maxLength value="10"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>

Resources