Invoke an attribute in the XSD schema as a primary key (and sort)

In the XML shown below, there is a way to create a schema that will describe the XML so that the id value increases by 1 with the addition of each node book. The goal is to use id as a primary key whose minimum value is 1. In addition, id values ​​must be sorted in ascending order.

<Books> <Book id="1"></Book> <Book id="2"></Book> <Book id="3"></Book> <Book id="4"></Book> ... </Books> 
+4
source share
2 answers

Not. you cannot do this in xsd.

xsd defines the xml schema, not the XML data. you will need to do this in your code that parses the xml data.

+1
source

I think the closest you can get is this:

 <?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="Books"> <xs:complexType> <xs:sequence> <xs:element name="Book"> <xs:complexType> <xs:attribute name="id" type="id_type" /> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> <xs:key name="PK_BookID"> <xs:selector xpath="Books/Book" /> <xs:field xpath="@id" /> </xs:key> <xs:unique name="BookIdUnique"> <xs:selector xpath="Books/Book" /> <xs:field xpath="@id" /> </xs:unique> </xs:element> <xs:simpleType name="id_type"> <xs:restriction base="xs:integer"> <xs:minInclusive value="1" /> </xs:restriction> </xs:simpleType> </xs:schema> 

I cannot think of any way to provide sorting and sequence of identifiers without spaces, but since this .__ curious_geek has already stated that XSD is not intended for this.

+1
source

Source: https://habr.com/ru/post/1299860/


All Articles