简体   繁体   English

XSD模式针对我的XML文档的两部分正确验证

[英]XSD schema validating correctly against two sections of my XML document

So I have an XSD document that's validating correctly against my XML up to a certain level. 因此,我有一个XSD文档,可以针对我的XML正确地进行验证,直至达到特定级别。 As soon as a I had another DatabaseName element or Webserver element or both, an error comes up saying "element not expected here." 一旦我有了另一个DatabaseName元素或Webserver元素,或同时拥有这两个元素,就会出现一个错误,提示“此处没有期望的元素”。 Not too sure what's the problem - can anyone help? 不太清楚这是什么问题-有人可以帮忙吗?

XML: 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: 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>

Your content model for Inventory only allows a single DatabaseName. 您的清单内容模型仅允许一个DatabaseName。

Instead of all(DatabaseName, WebserverName) , use the content model 代替all(DatabaseName, WebserverName) ,使用内容模型

choice minOccurs=0 maxOccurs=unbounded
  element DatabaseName
  element WebserverName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM