XSD restricting complex type element - validation

I have a XSD definition which looks like this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ARTICLES">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="ARTICLE">
<xs:complexType>
<xs:sequence>
<xs:element name="NAME" type="xs:string" />
<xs:element name="TYPE" type="xs:unsignedByte" />
<xs:element name="LENGTH" type="xs:unsignedByte" />
<xs:element name="WIDTH" type="xs:unsignedShort" />
<xs:element name="HEIGHT" type="xs:unsignedShort" />
<xs:element maxOccurs="unbounded" name="PRICELIST">
<xs:complexType>
<xs:sequence>
<xs:element name="SALESCONTRACT" type="xs:string" />
<xs:element name="PRICE" type="xs:decimal" />
<xs:element name="VAT" type="xs:unsignedByte" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
An XML can contain a collection of ATRICLES and each ARTICLE can have 1 or more PRICELIST entries
Now I would like to take an element and restrict the value entries. i.e. With a regex or numbers only or in length.
With simpletype I found lots of examples that work with a pattern tag.
How would this work with complex types?
Regards,
Rick

You can simply apply all of those examples you've found using xs:simpleType to any of the parts of ARTICLE that you wish to further constrain. For example, to limit NAME to be of maximum length of 32 characters:
<xs:element name="NAME">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="32"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Related

Different .xsd using xsd.exe and VisualStudio->Xml->CreateSchema

I have foo.xml, I would like to generate foo.xsd as it is generated with VisualStudio->Xml->CreateSchema, tried xsd.exe but results are not the same. (xsd.exe foo.xml)
How to call same command as VisualStudio->Xml->CreateSchema from command line?
Maybe one small example will help, notce type="xs:string" minOccurs="0" When xml is more complexed differences become huge.
xml:
<foo>
<x />
<y />
</foo>
VS:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="x" />
<xs:element name="y" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
xsd.exe
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="x" type="xs:string" minOccurs="0" />
<xs:element name="y" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="foo" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
Looks like it could be that Visual Studio is using XmlSchemaInference.InferSchema internally instead of xsd.exe? More information on XmlSchemaInference.InferSchema.

How to derive possible XPath expressions from XSD for some element names?

I am now struggling a bit to generate automatically the XPaths leading to a certain type of my XSD schema.
In this example, I would like to get the XPath leading to "firstName" and "lastName", which means:
root/Friend/firstName, root/Friend/lastName, root/TenthGrader/firstName, root/tenthGrader/lastName, root/complicatedPerson/oldSelf/firstName, root/complicatedPerson/oldSelf/lastName
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="Friend" type="Student"/>
<xs:element name="TenthGrader">
<xs:complexType>
<xs:complexContent>
<xs:extension base="Student">
<xs:element name="Sexyteacher"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:element ref="complicatedPerson"/>
<xs:element name="IdoNothing"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="complicatedPerson" type="Person"/>
<xs:complexType name="Person">
<xs:sequence>
<xs:element name="oldSelf" type="Student"/>
<xs:element name="Age" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Student">
<xs:sequence>
<xs:element name="firstName"/>
<xs:element name="lastName"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
This is already giving me a headache.
Is there any known method to do this?
Thank you very much in advance!

Oracle XMLDB Registering XML Schema

Hello I have a problem with Oracle XMLDB Express Edition 10g.
I am trying to register a fully valid and working .xsd Schema via dbms_xmlschema.registerSchema, but keep getting
ORA-01741: illegal zero-length identifier
I have tried everything: triplechecked my xsd and query code.
There were a couple of topics on Oracle forums but there were no answers.
Please help.
Here is my code:
begin
dbms_xmlschema.registerSchema(
'opta',
'<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.org/opta/" targetNamespace="http://www.example.org/opta/">
<xs:element name="Document">
<xs:complexType>
<xs:sequence>
<xs:element name="Order" type="tns:Order" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Order">
<xs:sequence>
<xs:element name="Products" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Product" type="tns:Product" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Date" type="xs:date" minOccurs="1"/>
<xs:element name="Client" type="tns:Client" minOccurs="1"/>
</xs:sequence>
<xs:attribute name="id" type="xs:int"/>
</xs:complexType>
<xs:complexType name="Product">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Price" type="xs:int"/>
<xs:element name="Amount" type="xs:int"/>
<xs:element name="Producer" type="tns:Producer"/>
<xs:element name="Type" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:int"/>
</xs:complexType>
<xs:complexType name="Producer">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Organization" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Client">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Organization" type="xs:string"/>
<xs:element name="Country" type="xs:string"/>
<xs:element name="City" type="xs:string"/>
<xs:element name="Address" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
');
end;

violation of "Unique Particle Attribution"

when I want to validate my xsd file, I got this error
cos-nonambig: "my xsd file":layout and "my xsd file":layout (or elements from their substitution group) violate "Unique Particle Attribution". During validation against this schema, ambiguity would be created for those two particles.
and refers me to this tag
<xs:complexType name="pageType">
<xs:choice>
<xs:element type="main:layoutType" name="layout" minOccurs="0" maxOccurs="1"/>
<xs:group ref="main:WidgetsGroup" maxOccurs="unbounded" minOccurs="0"/>
</xs:choice>
<xs:attribute type="xs:string" name="name"/>
<xs:attribute type="xs:string" name="layout"/>
<xs:attribute type="xs:string" name="dataModel"/>
<xs:attribute type="xs:string" name="domain"/>
</xs:complexType>
what is the problem? and how could I fix it?
I've solved it by inserting WidgetGroup contents to my xsd as :
<xs:complexType name="pageType">
<xs:choice>
<xs:element type="main:layoutType" name="layout" minOccurs="0" maxOccurs="1"/>
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="spinner" type="main:SpinnerType" minOccurs="0"/>
<xs:element name="datePicker" type="main:DatePickerType" minOccurs="0"/>
<xs:element name="button" type="main:ButtonType" minOccurs="0"/>
<xs:element name="combo" type="main:ComboBoxType" minOccurs="0"/>
<xs:element name="checkBox" type="main:CheckBoxType" minOccurs="0"/>
<xs:element name="radioButton" type="main:RadioButtonType" minOccurs="0"/>
<xs:element name="image" type="main:ImageType" minOccurs="0"/>
<xs:element name="label" type="main:LabelType" minOccurs="0"/>
<xs:element name="listBox" type="main:ListBoxType" minOccurs="0"/>
<xs:element name="textBox" type="main:TextBoxType" minOccurs="0"/>
<!--<xs:element name="layout" type="main:layoutType" minOccurs="0"/>-->
</xs:choice>
</xs:sequence>
</xs:choice>
<xs:attribute type="xs:string" name="name"/>
<xs:attribute type="xs:string" name="layout"/>
<xs:attribute type="xs:string" name="dataModel"/>
<xs:attribute type="main:domainType" name="domain"/>
<xs:attribute type="xs:string" name="title"/>
</xs:complexType>

XSD Validation for Visual Studio Intellisense

Hi guys I have a simple XML file with this structure
... ...
<Fields>
<Field name="MainJob.Id" value="t066_id">
<Description nullable="false" type="System.Int32" />
</Field>
What I have actually is this XSD file description:
<xs:element minOccurs="0" maxOccurs="unbounded" name="Fields">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Field">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Description">
<xs:complexType>
<xs:attribute name="nullable" type="xs:string" use="required" /> <xs:attribute name="type" type="xs:string" use="required" />
<xs:attribute name="minLength" type="xs:string" use="optional" />
<xs:attribute name="maxLength" type="xs:string" use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="value" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
How I can define two only available values for the attribute nullable, like 'true' and 'false'?
If I nest a SimpleType inside the attribute the .XSD file is not valid anymore.
thank you
Type should be not xs:string but xs:boolean for your example, or you can use enumeration (example).
You can add a simpleType like so:
<xs:simpleType name="TrueOrFalse">
<xs:restriction base="xs:string">
<xs:enumeration value="false"/>
<xs:enumeration value="true"/>
</xs:restriction>
</xs:simpleType>
and then change your nullable to:
<xs:attribute name="nullable" type="TrueOrFalse" use="required" />
Marc

Resources