编辑:我在id属性中尝试了推荐的正则表达式,但是当我尝试验证XSD文件时,我现在收到以下错误:"第7行出错:元素架构有额外的内容:element"
以下是相关代码:
<xs:attribute name="id" type="xs:ID" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:patterrn value="[X|Y][0-9]{3}"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
如果我将其切换回以下代码,则验证就好了:
<xs:attribute name="id" type="xs:ID" use="required"/>
完整的XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<presentations xmlns="http://www.brett.com/presentations"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.brett.com/presentations presentations.xsd"
>
<presentation date="2013-07-31" length="PT30M">
<topic genre="Music">PianoML</topic>
<presenters>
<name title="Mr." id="Y258">
<first>Elvis</first>
<middle>A</middle>
<last>Presley </last>
</name>
<name title="Miss" id="X365">
<first>Lady</first>
<last>Gaga</last>
</name>
</presenters>
</presentation>
<presentation date="2013-08-05" length="PT35M">
<topic genre="Science">AlienML</topic>
<presenters>
<name title="Mr." id="Y007">
<first>Will</first>
<last>Smith</last>
</name>
<name title="Mr." id="Y360">
<first>Tommy</first>
<first>Lee</first>
<last>Jones</last>
</name>
</presenters>
</presentation>
</presentations>
完整的XSD文件:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.brett.com/presentations"
targetNamespace="http://www.brett.com/presentations"
elementFormDefault="qualified">
<xs:element name="presentations">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element ref="presentation"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="presentation">
<xs:complexType>
<xs:sequence>
<xs:element ref="topic"/>
<xs:element ref="presenters"/>
</xs:sequence>
<xs:attribute name="date" type="xs:date" use="required"/>
<xs:attribute name="length" type="xs:duration" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="topic">
<xs:complexType>
<xs:attribute name="genre" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="ART"/>
<xs:enumeration value="Music"/>
<xs:enumeration value="Science"/>
<xs:enumeration value="Technology"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="presenters">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element ref="name"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="name">
<xs:complexType>
<xs:sequence>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element ref="first"/>
</xs:sequence>
<xs:sequence minOccurs="0" maxOccurs="1">
<xs:element ref="middle"/>
</xs:sequence>
<xs:element ref="last"/>
</xs:sequence>
<!-- New changes -->
<xs:attribute name="id" type="xs:ID" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:patterrn value="[X|Y][0-9]{3}"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="title" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Mr."/>
<xs:enumeration value="Mrs."/>
<xs:enumeration value="Ms."/>
<xs:enumeration value="Miss"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="first" type="xs:string"/>
<xs:element name="middle" type="xs:string"/>
<xs:element name="last" type="xs:string"/>
</xs:schema>
答案 0 :(得分:1)
XSD中的正则表达式略有不同,这里有一篇很好的文章XML Schema Regular Expressions
你想要的是如下。
<xs:attribute name="id" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[X|Y][0-9]{3}" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
编辑后的答案: 以下是您所需要的,请注意xs:ID是限制而不是属性。
<xs:attribute name="id" use="required">
<xs:simpleType>
<xs:restriction base="xs:ID">
<xs:pattern value="[X|Y][0-9]{3}" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>