XSD 根据我的 XML 文档的两个部分正确验证模式
XSD schema validating correctly against two sections of my XML document
所以我有一个 XSD 文档,它在一定程度上针对我的 XML 进行了正确验证。一旦我有另一个 DatabaseName 元素或 Webserver 元素或两者,就会出现错误 "element not expected here." 不太确定是什么问题 - 谁能帮忙?
XML:
<?xml version="1.0" encoding="ISO8859-1" ?>
<?xml-stylesheet type="text/xsl" href="xrt.xsl"?>
<Inventory>
<DatabaseName>
<GlobalName>Tom</GlobalName>
<Function>production</Function>
<Domain>tom.info</Domain>
<Administrator EmailAlias="xrichards" Extension="221">Xavier Richards</Administrator>
<Attributes Type="Production" Version="20ix"/>
<Comments>
...
</Comments>
<Usage>
500
</Usage>
</DatabaseName>
<WebserverName>
<GlobalName>Jim</GlobalName>
<Function>distribution</Function>
<Domain>jim1235.com</Domain>
<Administrator EmailAlias="rknowles" Extension="134237">Richard Knowles</Administrator>
<Administrator EmailAlias="thoffman" Extension="222237">Tom Hoffman</Administrator>
<Attributes Type="Production" Version="20ix"/>
<Comments>
...
</Comments>
<Usage>
1200
</Usage>
</WebserverName>
<DatabaseName>
<GlobalName>Tom</GlobalName>
<Function>production</Function>
<Domain>tom.info</Domain>
<Administrator EmailAlias="xrichards" Extension="221">Xavier Richards</Administrator>
<Attributes Type="Production" Version="20ix"/>
<Comments>
...
</Comments>
<Usage>
500
</Usage>
</DatabaseName>
</Inventory>
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.yourwebsite.com" xmlns="http://www.yourwebsite.com"
elementFormDefault="qualified">
<xs:element name="Inventory">
<xs:complexType>
<xs:all>
<xs:element name="DatabaseName">
<xs:complexType>
<xs:sequence>
<xs:element name="GlobalName" type="xs:string"/>
<xs:element name="Function" type="xs:string"/>
<xs:element name="Domain" type="xs:string"/>
<xs:element name="Administrator" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="EmailAlias" type="xs:string"
use="required"/>
<xs:attribute name="Extension">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="1000"/>
<xs:maxInclusive value="9999"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="Attributes">
<xs:complexType>
<xs:attribute name="Type" type="xs:string" use="required"/>
<xs:attribute name="Version" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="20ix|29i"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="Comments" type="xs:string"/>
<xs:element name="Usage" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="WebserverName">
<xs:complexType>
<xs:sequence>
<xs:element name="GlobalName" type="xs:string"/>
<xs:element name="Function" type="xs:string"/>
<xs:element name="Domain" type="xs:string"/>
<xs:element name="Administrator" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="EmailAlias" type="xs:string"
use="required"/>
<xs:attribute name="Extension">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="1000"/>
<xs:maxInclusive value="9999"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="Attributes">
<xs:complexType>
<xs:attribute name="Type" type="xs:string" use="required"/>
<xs:attribute name="Version" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="6i|7i|8i|9i"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="Comments" type="xs:string"/>
<xs:element name="Usage" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
您的 Inventory 内容模型只允许一个数据库名称。
而不是all(DatabaseName, WebserverName)
,使用内容模型
choice minOccurs=0 maxOccurs=unbounded
element DatabaseName
element WebserverName
所以我有一个 XSD 文档,它在一定程度上针对我的 XML 进行了正确验证。一旦我有另一个 DatabaseName 元素或 Webserver 元素或两者,就会出现错误 "element not expected here." 不太确定是什么问题 - 谁能帮忙?
XML:
<?xml version="1.0" encoding="ISO8859-1" ?>
<?xml-stylesheet type="text/xsl" href="xrt.xsl"?>
<Inventory>
<DatabaseName>
<GlobalName>Tom</GlobalName>
<Function>production</Function>
<Domain>tom.info</Domain>
<Administrator EmailAlias="xrichards" Extension="221">Xavier Richards</Administrator>
<Attributes Type="Production" Version="20ix"/>
<Comments>
...
</Comments>
<Usage>
500
</Usage>
</DatabaseName>
<WebserverName>
<GlobalName>Jim</GlobalName>
<Function>distribution</Function>
<Domain>jim1235.com</Domain>
<Administrator EmailAlias="rknowles" Extension="134237">Richard Knowles</Administrator>
<Administrator EmailAlias="thoffman" Extension="222237">Tom Hoffman</Administrator>
<Attributes Type="Production" Version="20ix"/>
<Comments>
...
</Comments>
<Usage>
1200
</Usage>
</WebserverName>
<DatabaseName>
<GlobalName>Tom</GlobalName>
<Function>production</Function>
<Domain>tom.info</Domain>
<Administrator EmailAlias="xrichards" Extension="221">Xavier Richards</Administrator>
<Attributes Type="Production" Version="20ix"/>
<Comments>
...
</Comments>
<Usage>
500
</Usage>
</DatabaseName>
</Inventory>
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.yourwebsite.com" xmlns="http://www.yourwebsite.com"
elementFormDefault="qualified">
<xs:element name="Inventory">
<xs:complexType>
<xs:all>
<xs:element name="DatabaseName">
<xs:complexType>
<xs:sequence>
<xs:element name="GlobalName" type="xs:string"/>
<xs:element name="Function" type="xs:string"/>
<xs:element name="Domain" type="xs:string"/>
<xs:element name="Administrator" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="EmailAlias" type="xs:string"
use="required"/>
<xs:attribute name="Extension">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="1000"/>
<xs:maxInclusive value="9999"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="Attributes">
<xs:complexType>
<xs:attribute name="Type" type="xs:string" use="required"/>
<xs:attribute name="Version" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="20ix|29i"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="Comments" type="xs:string"/>
<xs:element name="Usage" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="WebserverName">
<xs:complexType>
<xs:sequence>
<xs:element name="GlobalName" type="xs:string"/>
<xs:element name="Function" type="xs:string"/>
<xs:element name="Domain" type="xs:string"/>
<xs:element name="Administrator" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="EmailAlias" type="xs:string"
use="required"/>
<xs:attribute name="Extension">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="1000"/>
<xs:maxInclusive value="9999"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="Attributes">
<xs:complexType>
<xs:attribute name="Type" type="xs:string" use="required"/>
<xs:attribute name="Version" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="6i|7i|8i|9i"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="Comments" type="xs:string"/>
<xs:element name="Usage" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
您的 Inventory 内容模型只允许一个数据库名称。
而不是all(DatabaseName, WebserverName)
,使用内容模型
choice minOccurs=0 maxOccurs=unbounded
element DatabaseName
element WebserverName