I have the following xsd schema
<?xml version="1.1" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace = "http://www.ludd21.com/kPartModel"
xmlns = "http://www.ludd21.com/kPartModel"
elementFormDefault="qualified"
>
<xs:element name="kPartModel">
<xs:complexType>
<xs:sequence>
<xs:element ref="kPartsPiece" minOccurs="1" maxOccurs = "unbounded"/>
</xs:sequence>
<xs:attribute name="modelName" type="xs:NCName" use = "required"/>
</xs:complexType>
<!--piecename must be unique within kpModel-->
<xs:unique name= "kPartModel">
<xs:selector xpath="*"/>
<xs:field xpath= "#pieceName"/>
</xs:unique>
</xs:element>
<xs:element name="kPartsPiece">
<xs:complexType>
<xs:sequence>
<xs:element ref= "kPartsList"/>
</xs:sequence>
<xs:attribute name="pieceName" type="xs:NCName"/>
<!-- if they exist previousnum and nextsnum must contain valid partNumber that is referring to a part in kpartsList-->
<xs:assert test = "every $x in kPartsList/#previousnums satisfies exists(kPartsList/#partNumber)"/>
<xs:assert test = "every $x in kPartsList/#nextsnums satisfies exists (kPartsList/#partNumber)"/>
</xs:complexType>
<!-- #partNumber is unique across kPartsList -->
<xs:unique name= "kPartsList">
<xs:selector xpath="*"/>
<xs:field xpath= "#partNumber"/>
</xs:unique>
</xs:element>
<xs:element name = "kPartsList" >
<xs:complexType>
<xs:sequence>
<xs:choice >
<xs:element ref = "castOnPartSeg"/>
<xs:element ref = "castOnPartPoint"/>
</xs:choice>
<xs:choice minOccurs= "0" maxOccurs = "unbounded">
<xs:element ref = "castOnPartSeg" />
<xs:element ref = "castOnPartPoint" />
<xs:element ref = "castOffPartSeg" />
<xs:element ref = "castOffPartPoint"/>
<xs:element ref = "joinPart"/>
<xs:element ref = "splitPart" />
<xs:element ref = "segSplitPart"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="castOnPartPoint">
<xs:complexType>
<xs:attribute name ="nextsnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="castOnPartSeg">
<xs:complexType>
<xs:attribute name = "nextsnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="castOffPartPoint">
<xs:complexType>
<xs:attribute name ="previousnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" fixed = "0" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="castOffPartSeg">
<xs:complexType>
<xs:attribute name ="previousnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" fixed="0" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="splitPart">
<xs:complexType>
<xs:attribute name ="previousnum" type = "kpRefsList" use = "required"/>
<xs:attribute name ="nextsnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="segSplitPart">
<xs:complexType>
<xs:attribute name ="previousnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" fixed= "0" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="joinPart">
<xs:complexType>
<xs:attribute name ="nextsnum" type = "kpRefsList" use = "required"/>
<xs:attribute name ="previousnum" type = "kpRefsListmin2" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" use = "required"/>
</xs:complexType>
</xs:element>
<xs:simpleType name = "kpRefsList">
<xs:list itemType= "xs:nonNegativeInteger"/>
</xs:simpleType>
<xs:simpleType name="kpRefsListmin2">
<xs:restriction base = "kpRefsList">
<xs:minLength value = "2"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name ="decimal5digits">
<xs:restriction base = "xs:decimal">
<xs:fractionDigits value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
It includes assertions in lines 28 and 29 which are incorrect . I have tried several solutions but being a complet newbie in XPath I did not make it. The goal is to test that every number in previousnum (same for nextsnum) is a valid partNumber for a part in the kPartsList of the kPartsPiece.
Below is an xml fragment which should be validated against the schema;
<ns0:kPartModel xmlns:ns0="http://www.ludd21.com/kPartModel" modelName="manysingulars">
<ns0:kPartsPiece pieceName="manysingulars">
<ns0:kPartsList>
<ns0:castOnPartSeg nextsnum="1" partNumber="0" heightd="11.82912" />
<ns0:joinPart nextsnum="3" previousnum="0 2" partNumber="1" heightd="8.08932" />
<ns0:castOnPartPoint nextsnum="1" partNumber="2" heightd="3.80881" />
<ns0:joinPart nextsnum="6" previousnum="1 5 4" partNumber="3" heightd="8.65979" />
<ns0:castOnPartSeg nextsnum="3" partNumber="4" heightd="16.36741" />
<ns0:castOnPartSeg nextsnum="3" partNumber="5" heightd="6.48911" />
<ns0:joinPart nextsnum="8 9 10" previousnum="3 7" partNumber="6" heightd="5.23755" />
<ns0:castOnPartPoint nextsnum="6" partNumber="7" heightd="18.12524" />
<ns0:splitPart previousnum="6" nextsnum="12" partNumber="8" heightd="8.63775" />
<ns0:splitPart previousnum="6" nextsnum="11" partNumber="9" heightd="8.63775" />
<ns0:segSplitPart previousnum="6" partNumber="10" heightd="0.00000" />
<ns0:castOffPartSeg previousnum="9" partNumber="11" heightd="0.00000" />
<ns0:castOffPartSeg previousnum="8" partNumber="12" heightd="0.00000" />
</ns0:kPartsList>
</ns0:kPartsPiece>
</ns0:kPartModel>
Here is an example a bit simplified but similar to your needs, with a list type and an assertion:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="parts"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="parts">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="part"/>
</xs:sequence>
<xs:assert id="test-reference-id" test="every $part in part satisfies every $id in $part/data(#next) satisfies some $part2 in part satisfies $id = $part2/#id"/>
</xs:complexType>
</xs:element>
<xs:element name="part">
<xs:complexType>
<xs:attribute name="id" use="required" type="xs:string"/>
<xs:attribute name="next" type="id-list"/>
</xs:complexType>
</xs:element>
<xs:simpleType name="id-list">
<xs:list itemType="xs:string"></xs:list>
</xs:simpleType>
</xs:schema>
The example
<root>
<parts>
<part id="p1" next="p2 p4"/>
<part id="p2" next="p3 p4 p5"/>
<part id="p3" next="p4"/>
<part id="p4"/>
</parts>
</root>
then fails the assertion while
<root>
<parts>
<part id="p1" next="p2 p4"/>
<part id="p2" next="p3 p4"/>
<part id="p3" next="p4"/>
<part id="p4"/>
</parts>
</root>
is valid.
My attempt to implement your assertions is
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace = "http://www.ludd21.com/kPartModel"
xmlns = "http://www.ludd21.com/kPartModel"
elementFormDefault="qualified"
vc:minVersion = "1.1"
xpathDefaultNamespace="##targetNamespace"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
>
<xs:element name="kPartModel">
<xs:complexType>
<xs:sequence>
<xs:element ref="kPartsPiece" minOccurs="1" maxOccurs = "unbounded"/>
</xs:sequence>
<xs:attribute name="modelName" type="xs:NCName" use = "required"/>
</xs:complexType>
<!--piecename must be unique within kpModel-->
<xs:unique name= "kPartModel">
<xs:selector xpath="*"/>
<xs:field xpath= "#pieceName"/>
</xs:unique>
</xs:element>
<xs:element name="kPartsPiece">
<xs:complexType>
<xs:sequence>
<xs:element ref= "kPartsList"/>
</xs:sequence>
<xs:attribute name="pieceName" type="xs:NCName"/>
<!-- if they exist previousnum and nextsnum must contain valid partNumber that is referring to a part in kpartsList-->
<xs:assert test = "every $x in data(kPartsList/*/#previousnum) satisfies some $part in kPartsList/* satisfies $part/#partNumber = $x"/>
<xs:assert test = "every $x in data(kPartsList/*/#nextsnum) satisfies some $part in kPartsList/* satisfies $part/#partNumber = $x"/>
</xs:complexType>
<!-- #partNumber is unique across kPartsList -->
<xs:unique name= "kPartsList">
<xs:selector xpath="*/*"/>
<xs:field xpath= "#partNumber"/>
</xs:unique>
</xs:element>
<xs:element name = "kPartsList" >
<xs:complexType>
<xs:sequence>
<xs:choice >
<xs:element ref = "castOnPartSeg"/>
<xs:element ref = "castOnPartPoint"/>
</xs:choice>
<xs:choice minOccurs= "0" maxOccurs = "unbounded">
<xs:element ref = "castOnPartSeg" />
<xs:element ref = "castOnPartPoint" />
<xs:element ref = "castOffPartSeg" />
<xs:element ref = "castOffPartPoint"/>
<xs:element ref = "joinPart"/>
<xs:element ref = "splitPart" />
<xs:element ref = "segSplitPart"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="castOnPartPoint">
<xs:complexType>
<xs:attribute name ="nextsnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="castOnPartSeg">
<xs:complexType>
<xs:attribute name = "nextsnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="castOffPartPoint">
<xs:complexType>
<xs:attribute name ="previousnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" fixed = "0" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="castOffPartSeg">
<xs:complexType>
<xs:attribute name ="previousnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" fixed="0" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="splitPart">
<xs:complexType>
<xs:attribute name ="previousnum" type = "kpRefsList" use = "required"/>
<xs:attribute name ="nextsnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="segSplitPart">
<xs:complexType>
<xs:attribute name ="previousnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" fixed= "0" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="joinPart">
<xs:complexType>
<xs:attribute name ="nextsnum" type = "kpRefsList" use = "required"/>
<xs:attribute name ="previousnum" type = "kpRefsListmin2" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" use = "required"/>
</xs:complexType>
</xs:element>
<xs:simpleType name = "kpRefsList">
<xs:list itemType= "xs:nonNegativeInteger"/>
</xs:simpleType>
<xs:simpleType name="kpRefsListmin2">
<xs:restriction base = "kpRefsList">
<xs:minLength value = "2"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name ="decimal5digits">
<xs:restriction base = "xs:decimal">
<xs:fractionDigits value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
final model which passes in XMLSchema Python Package and validates correctly instances:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace = "http://www.ludd21.com/kPartModel"
xmlns = "http://www.ludd21.com/kPartModel"
elementFormDefault="qualified"
vc:minVersion = "1.1"
xpathDefaultNamespace="##targetNamespace"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
>
<xs:element name="kPartModel">
<xs:complexType>
<xs:sequence>
<xs:element ref="kPartsPiece" minOccurs="1" maxOccurs = "unbounded"/>
</xs:sequence>
<xs:attribute name="modelName" type="xs:NCName" use = "required"/>
</xs:complexType>
<!--piecename must be unique within kpModel-->
<xs:unique name= "kPartModel">
<xs:selector xpath="*"/>
<xs:field xpath= "#pieceName"/>
</xs:unique>
</xs:element>
<xs:element name="kPartsPiece">
<xs:complexType>
<xs:sequence>
<xs:element ref= "kPartsList"/>
</xs:sequence>
<xs:attribute name="pieceName" type="xs:NCName"/>
<!-- if they exist previousnum and nextsnum must contain valid partNumber that is referring to a part in kpartsList-->
<xs:assert id = "test-nexts" test = "every $x in data(kPartsList/*/#nextsnum) satisfies some $part in kPartsList/* satisfies $part/#partNumber = $x"/>
</xs:complexType>
<!-- #partNumber is unique across kPartsList -->
<xs:unique name= "kPartsList">
<xs:selector xpath="*/*"/>
<xs:field xpath= "#partNumber"/>
</xs:unique>
</xs:element>
<xs:element name = "kPartsList" >
<xs:complexType>
<xs:sequence>
<xs:choice >
<xs:element ref = "castOnPartSeg"/>
<xs:element ref = "castOnPartPoint"/>
</xs:choice>
<xs:choice minOccurs= "0" maxOccurs = "unbounded">
<xs:element ref = "castOnPartSeg" />
<xs:element ref = "castOnPartPoint" />
<xs:element ref = "castOffPartSeg" />
<xs:element ref = "castOffPartPoint"/>
<xs:element ref = "joinPart"/>
<xs:element ref = "splitPart" />
<xs:element ref = "segSplitPart"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="castOnPartPoint">
<xs:complexType>
<xs:sequence>
<xs:element ref ="basePoint"/>
</xs:sequence>
<xs:attribute name ="nextsnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="castOnPartSeg">
<xs:complexType>
<xs:sequence>
<xs:element ref ="baseSeg"/>
</xs:sequence>
<xs:attribute name = "nextsnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="castOffPartPoint">
<xs:complexType>
<xs:sequence>
<xs:element ref ="basePoint"/>
</xs:sequence>
<xs:attribute name ="previousnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" fixed = "0" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="castOffPartSeg">
<xs:complexType>
<xs:sequence>
<xs:element ref ="baseSeg"/>
</xs:sequence>
<xs:attribute name ="previousnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" fixed="0" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="splitPart">
<xs:complexType>
<xs:sequence>
<xs:element ref ="baseSeg"/>
</xs:sequence>
<xs:attribute name ="previousnum" type = "kpRefsList" use = "required"/>
<xs:attribute name ="nextsnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="segSplitPart">
<xs:complexType>
<xs:sequence>
<xs:element ref ="baseSeg"/>
</xs:sequence>
<xs:attribute name ="previousnum" type = "kpRefsList" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" fixed= "0" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name="joinPart">
<xs:complexType>
<xs:sequence>
<xs:element ref ="baseSeg"/>
</xs:sequence>
<xs:attribute name ="nextsnum" type = "kpRefsList" use = "required"/>
<xs:attribute name ="previousnum" type = "kpRefsListmin2" use = "required"/>
<xs:attribute name = "partNumber" type = "xs:nonNegativeInteger" use = "required"/>
<xs:attribute name = "heightd" type = "decimal5digits" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name = "basePoint">
<xs:complexType>
<xs:attribute name= "start" type = "point" use = "required"/>
</xs:complexType>
</xs:element>
<xs:element name = "baseSeg">
<xs:complexType>
<xs:attribute name= "start" type = "point" use = "required"/>
<xs:attribute name= "end" type = "point" use = "required"/>
<!--startY = endY!-->
<!--<xs:assert test = "#start[1] = #end[1]"/> -->
</xs:complexType>
</xs:element>
<xs:simpleType name= "point">
<xs:restriction>
<xs:simpleType>
<xs:list itemType = "decimal5digits"/>
</xs:simpleType>
<xs:length value = "2"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name = "kpRefsList">
<xs:list itemType= "xs:nonNegativeInteger"/>
</xs:simpleType>
<xs:simpleType name="kpRefsListmin2">
<xs:restriction base = "kpRefsList">
<xs:minLength value = "2"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name ="decimal5digits">
<xs:restriction base = "xs:decimal">
<xs:fractionDigits value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Related
I have a following schema (1.1). I would like to have schema that will check that a sequence of parameters has mendatory parameter of type '1' and optional parameter of type '2'. I did it using alternatives because different types have different list of attributes.
How write an assertion to check that parameter of type 1 is mendatory?
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" vc:minVersion="1.1">
<xs:element name="rule">
<xs:complexType>
<xs:sequence>
<xs:element name="conditions" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="condition" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="parameter" maxOccurs="unbounded" minOccurs="1">
<xs:alternative test="#type = '1'" type="typeOne"/>
<xs:alternative test="#type = '2'" type="typeTwo" />
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="typeOne">
<xs:attribute name="type" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="typeTwo">
<xs:attribute name="type" type="xs:string" use="required" />
</xs:complexType>
</xs:schema>
I did it using alternatives because different types have different list of attributes.
It would be like this:
<xs:element name="condition" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="parameter" maxOccurs="unbounded" minOccurs="1">
<xs:alternative test="#type = '1'" type="typeOne"/>
<xs:alternative test="#type = '2'" type="typeTwo" />
</xs:element>
</xs:sequence>
<xs:assert id="type-1-mandatory" test="parameter[#type eq '1']"></xs:assert>
</xs:complexType>
</xs:element>
I would like to write a XSD element that permit something like that :
<CustomFields>
<CustomField id="1">some text data</CustomField>
<CustomField id="2">some text data</CustomField>
</CustomFields>
But I have some restrictions : I have to restrict the text (maxLenght = 36). And I would like to be impossible to have 2 CustomField with the same id.
So far I wrote this, but it not what I want :
<xs:element name="CustomFields" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="CustomField" minOccurs="0" maxOccurs="20">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="id" type="xs:integer" use="required"></xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
Thanks for any help.
Regards.
You could use following attempt
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:simpleType name="restrictedString">
<!-- Make a new type to be a "descendant" of string-->
<xs:restriction base="xs:string">
<xs:maxLength value="36"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="CustomFields">
<xs:complexType>
<xs:sequence>
<xs:element name="CustomField" minOccurs="0" maxOccurs="20">
<xs:complexType>
<xs:simpleContent>
<!-- reference new type you declared above -->
<xs:extension base="restrictedString">
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<!-- Restrict #id to be unique-->
<xs:unique name="id_uq">
<xs:selector xpath="CustomField"/>
<xs:field xpath="#id"/>
</xs:unique>
</xs:element>
</xs:schema>
I have some complexType element which is parent of some element:
<xs:complexType name="elementParent">
<xs:sequence>
<xs:element name="b" type="xs:String" />
<xs:element name="c" type="xs:String" />
</xs:complexType>
now I want to create anothex complex type which will be extension of my parent. Problem is in order. First element should be a then b and then c. I just know this option where order will be b c a which I dont want:
<xs:complexType name="elementChild">
<xs:complexContent>
<xs:extension base="elementParent">
<xs:sequence>
<xs:element name="a" type="xs:String" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:extension> creates a type which is a sequence containing first the base type and then the explicit content defined in <xs:extension>. If you only want to add new elements preceding the elements of another type, you could define the common elements as an <xs:group> and then refer to that group in both of the type definitions.
Something like this:
<!-- common elements defined as a group -->
<xs:group name="elementGroup">
<xs:sequence>
<xs:element name="b" type="xs:string" />
<xs:element name="c" type="xs:string" />
</xs:sequence>
</xs:group>
<!-- group is referred to in element type definitions -->
<xs:element name="elementParent">
<xs:complexType>
<xs:sequence>
<xs:group ref="elementGroup" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="elementChild">
<xs:complexType>
<xs:sequence>
<xs:element name="a" type="xs:string" />
<xs:group ref="elementGroup" />
</xs:sequence>
</xs:complexType>
</xs:element>
I want to generate the following XSD Schema using JAXB and ObjectFactory class of org/w3/_2001/xmlschema
<xs:element name="result">
<xs:complexType>
<xs:sequence>
<xs:element name="sfobject" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="id"/>
<xs:element type="xs:string" name="type"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
But I am getting XSD as :
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="result">
<xs:complexType>
<xs:sequence/>
</xs:complexType>
</xs:element>
<xs:element name="sfobject" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence/>
</xs:complexType>
</xs:element>
<xs:element type="xs:string" name="id"/>
<xs:element type="xs:string" name="type"/>
</xs:schema>
I am not able to add one element inside other element. How do I do that in Java using JAXB and ObjectFactory methods.
Sample Code:
List schemaElementList = schema.getSimpleTypeOrComplexTypeOrGroup();
TopLevelElement resultElement = xsdObjFactory.createTopLevelElement();
resultElement.setName("result");
LocalComplexType resultCompType = xsdObjFactory.createLocalComplexType();
resultElement.setComplexType(resultCompType);
resultCompType.setSequence(expGroup);
Element sfElement = (Element)xsdObjFactory.createLocalElement();
sfElement.setName("sfobject");
sfElement.setMaxOccurs("unbounded");
sfElement.setMinOccurs(new BigInteger("0"));
LocalComplexType sfCompType = xsdObjFactory.createLocalComplexType();
sfElement.setComplexType(sfCompType);
schemaElementList.add(resultElement);
If I define my xsd schema like below , the xs:element is giving an error. I want to have a 1:1 relationship between BitStatType and ValidationCountMessage? How can I define a class within a class -- This is for JAXB java.
<xs:complexType name="BitStatType">
<xs:element name="BitAgeViolation" type="ValidationCountMessage" />
<xs:attribute name="WiId" type="xs:int" use="required"/>
<xs:attribute name="Title" type="xs:string" use="required"/>
<xs:attribute name="TriggerRulesCount" type="xs:int" use="required"/>
<xs:attribute name="TriggerEventsCount" type="xs:int" use="required"/>
<xs:attribute name="LastPublishedDate" type="xs:date" use="required"/>
</xs:complexType>
Firstly, you're missing an xs:sequence (just in case, not sure if it got lost in posting or what).
<xs:complexType name="BitStatType">
<xs:sequence>
<xs:element name="BitAgeViolation" type="ValidationCountMessage"/>
</xs:sequence>
<xs:attribute name="WiId" type="xs:int" use="required"/>
<xs:attribute name="Title" type="xs:string" use="required"/>
<xs:attribute name="TriggerRulesCount" type="xs:int" use="required"/>
<xs:attribute name="TriggerEventsCount" type="xs:int" use="required"/>
<xs:attribute name="LastPublishedDate" type="xs:date" use="required"/>
</xs:complexType>
If your ValidationCountMessage is a complexType then JAXB should create two classes, one for BitStatType and one for ValidationCountMessage; the BitStatType class will have a field bitAgeViolation, of type ValidationCountMessage. So, you'll have an association between these two classes.