XML/XML Schema/pattern
Содержание
- 1 Define a comma-separated list ignoring whitespaces between values and commas
- 2 Define the intersection of the block L (all the letters) and the BasicLatin (ASCII characters below #x7F)
- 3 ISBN type by pattern
- 4 Oring and Grouping: "|" character is the "or" operator
- 5 The xs:pattern facet is the only facet that can be applied multiple times.
- 6 Unicode blocks
- 7 Use pattern to control the extension name of image type
- 8 Wildcard: integers that are multiples of 10
- 9 xs:pattern defines a pattern that must be matched by the lexical value of the datatype:
Define a comma-separated list ignoring whitespaces between values and commas
<source lang="xml">
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru" elementFormDefault="qualified"> <xs:simpleType name="myType"> <xs:restriction base="xs:token"> <xs:pattern value="(1|2|3)( *, *(1|2|3))*" /> </xs:restriction> </xs:simpleType>
</xs:schema>
</source>
Define the intersection of the block L (all the letters) and the BasicLatin (ASCII characters below #x7F)
<source lang="xml">
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru" elementFormDefault="qualified"> <xs:simpleType name="BasicLatinLetters"> <xs:restriction> <xs:simpleType> <xs:restriction base="xs:token"> <xs:pattern value="\p{IsBasicLatin}*" /> </xs:restriction> </xs:simpleType> <xs:pattern value="\p{L}*" /> </xs:restriction> </xs:simpleType>
</xs:schema>
</source>
ISBN type by pattern
<source lang="xml">
File: Data.xml
<?xml version="1.0"?> <Books xmlns="http://www.wbex.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.wbex.ru Schema.xsd"> <Book> <Title>title 1</Title> <Author>author 1</Author> <Date>2008</Date> <ISBN>1-11111-111-1</ISBN> <Publisher>Publisher 1</Publisher> </Book> <Book> <Title>title 2</Title> <Author>author 2</Author> <Date>2007</Date> <ISBN>0-111-11111-1</ISBN> <Publisher>Publisher 2</Publisher> </Book>
</Books> File: Schema.xsd <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru" elementFormDefault="qualified"> <xsd:simpleType name="ISBN-type"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{1}-\d{5}-\d{3}-\d{1}|\d{1}-\d{3}-\d{5}-\d{1}|\d{1}-\d{2}-\d{6}-\d{1}"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="Books"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:gYear"/> <xsd:element name="ISBN" type="ISBN-type"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element>
</xsd:schema>
</source>
Oring and Grouping: "|" character is the "or" operator
<source lang="xml">
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru" elementFormDefault="qualified"> <xs:simpleType name="myByte"> <xs:restriction base="xs:byte"> <xs:pattern value="1|2|3" /> </xs:restriction> </xs:simpleType>
</xs:schema>
</source>
The xs:pattern facet is the only facet that can be applied multiple times.
<source lang="xml">
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru" elementFormDefault="qualified"> <xsd:simpleType name="nonScientific"> <xsd:restriction base="xsd:float"> <xsd:pattern value="[^eE]*" /> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="noScientificNoLeading0"> <xsd:restriction base="nonScientific"> <xsd:pattern value="[^0].*" /> </xsd:restriction> </xsd:simpleType>
</xsd:schema>
</source>
Unicode blocks
<source lang="xml">
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru" elementFormDefault="qualified"> <xs:simpleType name="BasicLatinToken"> <xs:restriction base="xs:token"> <xs:pattern value="\p{IsBasicLatin}*" /> </xs:restriction> </xs:simpleType> <xs:simpleType name="Latin-1Token"> <xs:restriction base="xs:token"> <xs:pattern value="[\p{IsBasicLatin}\p{IsLatin-1Supplement}]*" /> </xs:restriction> </xs:simpleType>
</xs:schema>
</source>
Use pattern to control the extension name of image type
<source lang="xml">
File: Data.xml
<?xml version="1.0"?> <products xmlns ="http://www.wbex.ru"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.wbex.ru Schema.xsd"> <product> <name>name 1</name> <image>a.gif</image> <description>description 1</description> <warranty>lifetime warranty</warranty> <cost>41.95</cost> <retailer>http://www.wbex.ru</retailer> </product> <product> <name>name 2</name> <image>b.gif</image> <description>description 2</description> <cost>239.00</cost> <retailer>http://www.wbex.ru</retailer> </product>
</products> File: Schema.xsd <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru" elementFormDefault="qualified"> <xsd:element name="products"> <xsd:complexType> <xsd:sequence> <xsd:element name="product" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="image" type="imageType"/> <xsd:element name="description" type="xsd:string"/> <xsd:element name="warranty" type="xsd:string" minOccurs="0"/> <xsd:element name="weight" type="xsd:positiveInteger" minOccurs="0"/> <xsd:element name="cost" type="money" maxOccurs="unbounded"/> <xsd:element name="retailer" type="xsd:anyURI"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:simpleType name="money"> <xsd:restriction base="xsd:decimal"> <xsd:fractionDigits value="2"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="imageType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="(.)+\.(gif|jpg|jpeg|bmp)"/> </xsd:restriction> </xsd:simpleType>
</xsd:schema>
</source>
Wildcard: integers that are multiples of 10
<source lang="xml">
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru" elementFormDefault="qualified">
<xs:simpleType name="multipleOfTen"> <xs:restriction base="xs:integer"> <xs:pattern value=".*0" /> </xs:restriction> </xs:simpleType>
</xs:schema>
</source>
xs:pattern defines a pattern that must be matched by the lexical value of the datatype:
<source lang="xml">
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru" elementFormDefault="qualified"> <xsd:simpleType name="nonScientific"> <xsd:restriction base="xsd:float"> <xsd:pattern value="[^eE]*" /> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="noLeading0"> <xsd:restriction base="xsd:float"> <xsd:pattern value="[^0].*" /> </xsd:restriction> </xsd:simpleType>
</xsd:schema>
</source>