validation - XSD key not in root element and complex keys -
i searched this, can't find solution problem, please don't trash question.
we have service complex xml configuration described xsd. there many rules must obeyed things run smoothly. xsd describes structure of configuration, not rules, must now. after creating global rules have more complex , encountered problem. seams validator uses key/keyref/unique root element. i've created small xsd , xml file illustrate that:
xsd:
<?xml version="1.0" encoding="utf-8"?> <xs:schema targetnamespace="http://test.org/xmlschema.xsd" elementformdefault="qualified" xmlns="http://test.org/xmlschema.xsd" xmlns:t="http://test.org/xmlschema.xsd" xmlns:xs="http://www.w3.org/2001/xmlschema"> <xs:element name="orders" type="t:orderslist"> <xs:key name="orderno"> <xs:selector xpath="./t:order" /> <xs:field xpath="@number" /> </xs:key> </xs:element> <xs:complextype name="orderslist"> <xs:sequence> <xs:element minoccurs="0" maxoccurs="unbounded" name="order" nillable="false" type="t:order" /> </xs:sequence> </xs:complextype> <xs:complextype name="order"> <xs:sequence> <xs:element minoccurs="1" maxoccurs="1" name="lines" nillable="false" type="t:orderslineslist" /> </xs:sequence> <xs:attribute name="number" use="optional" type="xs:string" /> <xs:attribute name="clientid" use="optional" type="xs:int" /> </xs:complextype> <xs:complextype name="orderslineslist"> <xs:sequence> <xs:element minoccurs="0" maxoccurs="unbounded" name="line" nillable="false" type="t:orderline"> <!-- problem --> <xs:key name="linenokey"> <xs:selector xpath="./t:line" /> <xs:field xpath="@linenumber" /> </xs:key> </xs:element> </xs:sequence> </xs:complextype> <xs:complextype name="orderline"> <xs:attribute name="linenumber" use="optional" type="xs:string" /> <xs:attribute name="productid" use="optional" type="xs:int" /> <xs:attribute name="amount" use="optional" type="xs:decimal" /> </xs:complextype> </xs:schema> xml:
<?xml version="1.0" encoding="utf-8"?> <orders xmlns="http://test.org/xmlschema.xsd"> <order number="0001/5/13" clientid="123"> <lines> <line linenumber="1" productid="123" amount="4" /> <line linenumber="2" productid="124" amount="4" /> </lines> </order> <order number="0002/5/13" clientid="123"> <lines> <line linenumber="1" productid="123" amount="4" /> <!-- duplicate number - validate expected. --> <line linenumber="1" productid="124" amount="4" /> </lines> </order> <!-- duplicate number - doesn't validate expected. --> <order number="0002/5/13" clientid="123"> <lines> <line linenumber="1" productid="123" amount="4" /> <line linenumber="2" productid="124" amount="4" /> </lines> </order> </orders> i have few questions can't find sensible answer:
- how solve above. linenumber must unique, within order/lines.
- is possible allow keyref contain non existing virtual value (our service uses predefined... let things. custom ones defined in xml allow extending functionality, in of cases, users use predefined ones)
- is there way determine if list of elements within tag (something lines above) has @ least 1 tag attribute set value. example describe list of tables unique names, has list of columns unique names , @ least 1 of columns primary key.
this corrected constraint lines/line:
<xs:complextype name="order"> <xs:sequence> <xs:element minoccurs="1" maxoccurs="1" name="lines" nillable="false" type="t:orderslineslist"> <!-- problem solved --> <xs:key name="linenokey"> <xs:selector xpath="t:line"/> <xs:field xpath="@linenumber"/> </xs:key> </xs:element> </xs:sequence> <xs:attribute name="number" use="optional" type="xs:string"/> <xs:attribute name="clientid" use="optional" type="xs:int"/> </xs:complextype> if helps if can visualize constraints, understand scope act upon:
the correct diagram:

vs. original one:

you can see selector rooted in element line looking yet line (./t:line); , if fix selector, it'll match @ 1 attribute. idea of key selector should match a set of nodes among field must present , unique.
the above should take care of 1.
- no.
- not based on attribute. stick parallel, xsd 1.0 enforce element called primarykey, should contain 1 or more references names of other columns; if instead of using primary key constraint clause in column definition, enforce use of same @ table level.
Comments
Post a Comment