简体   繁体   English

XML文档未验证到XSD模式

[英]xml document not validating to xsd schema

Working with eclipse I've got rid of all errors but when I alter the element contents in my xml document beyond what the restrictions are set as in the .xsd file no validation errors appear. 使用eclipse,我摆脱了所有错误,但是当我更改xml文档中的元素内容超出.xsd文件中设置的限制时,没有出现验证错误。 I've tried validating it online with http://www.freeformatter.com/xml-validator-xsd.html and I get the error "Cvc-elt.1: Cannot Find The Declaration Of Element 'DatabaseInventory'.. Line '4', Column '69'" yet in eclipse it validates fine. 我尝试使用http://www.freeformatter.com/xml-validator-xsd.html在线进行验证,但出现错误“ Cvc-elt.1:找不到元素'DatabaseInventory'的声明。 4”,列“ 69””,但在日食中验证良好。 Not sure what I've done wrong. 不知道我做错了什么。

This is my xml 这是我的xml

     <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- standalone is no as this doc references external schema -->
<DatabaseInventory xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="DatabaseInventory.xsd">
<!-- tried to include "http://www.w3schools.com" alongside "DatabaseInventory.xsd" but      wouldn't validate  -->
<!-- default name space declaration -->
<!-- declare XML Schema Instance namespace, so schemaLocation attribute can be called -->
<!--  declare SYSTEM schema to use for this namespace "DatabaseInventory.xsd -->

  <DatabaseName>
    <GlobalDatabaseName>production.iDevelopment.info</GlobalDatabaseName>
    <OracleSID>Productio</OracleSID>
    <DatabaseDomain>iDevelopment.info</DatabaseDomain>
    <Administrator EmailAlias="jhunter" Extension="6007">
      Jeffrey Hunter
    </Administrator>
    <DatabaseAttributes Type="Production" Version="9i"/>
    <Comments>
      The following database should be considered the most stable for up-to-date 
      data. The backup strategy includes running the database in Archive Log Mode 
      and performing nightly backups. All new accounts need to be approved by the 
      DBA Group before being created.
    </Comments>
  </DatabaseName>

  <DatabaseName>
    <GlobalDatabaseName>development.iDevelopment.info</GlobalDatabaseName>
    <OracleSID>Development</OracleSID>
    <DatabaseDomain>iDevelopment.info</DatabaseDomain>
    <Administrator EmailAlias="jhunter" Extension="6007">
      Jeffrey Hunter
    </Administrator>
    <Administrator EmailAlias="mhunter" Extension="6008">
      Melody Hunter
    </Administrator>
    <DatabaseAttributes Type="Development" Version="9i"/>
    <Comments>
      The following database should contain all hosted applications. Production       
      data will be exported on a weekly basis to ensure all development environments        
      have stable and current data.
    </Comments>
  </DatabaseName>
<DatabaseName>
    <GlobalDatabaseName>testing.iDevelopment.info</GlobalDatabaseName>
    <OracleSID>Testing</OracleSID>
    <DatabaseDomain>iDevelopment.info</DatabaseDomain>
    <Administrator EmailAlias="jhunter" Extension="6007">
      Jeffrey Hunter
    </Administrator>
    <Administrator EmailAlias="mhunter" Extension="6008">
      Melody Hunter
    </Administrator>
    <Administrator EmailAlias="ahunter">
      Alex Hunter
    </Administrator>
    <DatabaseAttributes Type="Testing" Version="9i"/>
    <Comments>
      The following database will host more than half of the testing for our hosting 
      environment.
    </Comments>
  </DatabaseName>

</DatabaseInventory>

This is the schema 这是架构

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

<!-- using Venetian blinds layout, s3346141 - 1 mod 3 = 1 -->

<xs:complexType name="DatabaseAttributes-type">
    <xs:attribute name="Type" type="restricted-Type-values" use="required"/>
    <xs:attribute name="Version" type="restricted-Version-values" default="9i"/>
    <!--  set default value for Version -->
</xs:complexType>

<xs:simpleType name="restricted-Extention-values">
    <xs:restriction base="xs:integer">
      <xs:pattern value="6[0-9][0-9][0-9]"/>
      <!-- set REGEX for first digit = 6 followed by 3 digits -->
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="restricted-Type-values">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Production"/>
      <xs:enumeration value="Development"/>
      <xs:enumeration value="Testing"/>
      <!--  converted enumerated list into discrete values--> 
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="restricted-Version-values">
    <xs:restriction base="xs:string">
      <xs:enumeration value="7"/>
      <xs:enumeration value="8"/>
      <xs:enumeration value="8i"/>
      <xs:enumeration value="9i"/>
    </xs:restriction>
</xs:simpleType>


<xs:simpleType name="max-string-type">
    <xs:restriction base="xs:string">
            <xs:maxLength value="300"/> 
            <!-- increased char length to 300 to enable validation of DatabaseInventory.xml -->
    </xs:restriction>
</xs:simpleType>

<xs:complexType name="Administrator-type">
<!--  felt I needed to discriminate a name difference between element names and repeated types to make code clearer -->
  <xs:attribute name="EmailAlias" type="xs:string" use = "required"/>
  <xs:attribute name="Extension" type="restricted-Extention-values" use = "optional"/>
</xs:complexType>

<xs:complexType name="DatabaseName-type">
        <xs:sequence>
            <xs:element name="GlobalDatabaseName" type="xs:string" />
            <xs:element name="OracleSID" type="restricted-Type-values" />
            <xs:element name="DatabaseDomain" type="xs:string" />
            <xs:element name="Administrator-type" type="Administrator-type" minOccurs="1" maxOccurs="3"/>
            <xs:element name="DatabaseAttributes" type="DatabaseAttributes-type" />
            <xs:element name="Comments" type="max-string-type" />
        </xs:sequence>
    </xs:complexType>

<xs:complexType name="DatabaseInventory-type">
<xs:sequence>
 <xs:element name="DatabaseName" type="DatabaseName-type" minOccurs="1" maxOccurs="unbounded"/>
 <!-- set databaseName element to occur 1 or more times -->
 </xs:sequence>
</xs:complexType>    

<xs:element name="DatabaseInventory" type="DatabaseInventory-type" />
<!-- set root element to link to DatabaseInventory-type -->
</xs:schema>

In this case, eclipse doesn't perform any validation, since it cannot associate a schema with the instance document. 在这种情况下,eclipse不会执行任何验证,因为它无法将架构与实例文档相关联。

First, your schema does not specify a target namespace, which means that your instance document should not declare the default namespace: 首先,你的架构没有指定目标命名空间,这意味着你的实例文档应该声明默认命名空间:

<DatabaseInventory xmlns="http://www.w3schools.com" >

Change that to: 更改为:

<DatabaseInventory >

Second, the xsi:schemaLocation attribute takes a list of {URI, URL} pairs, where URI is the namespace URI and URL the location of the corresponding schema. 其次, xsi:schemaLocation属性采用{URI, URL}对的列表,其中URI是名称空间URI,URL是相应模式的位置。 However, since your schema does not declare a target name space, you should instead use the xsi:noNamespaceSchemaLocation attribute. 但是,由于您的架构未声明目标名称空间,因此您应该使用xsi:noNamespaceSchemaLocation属性。 Your first element should look like this: 您的第一个元素应如下所示:

<DatabaseInventory 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="DatabaseInventory.xsd">

You have two things wrong. 你有两件事错了。 1. -You have this in the schema: 1.-您在模式中具有以下内容:

 <xs:simpleType name="restricted-Type-values">
 <xs:restriction base="xs:string">
 <xs:enumeration value="Production"/>
 <xs:enumeration value="Development"/>
 <xs:enumeration value="Testing"/>
 <!--  converted enumerated list into discrete values--> 
 </xs:restriction>
 </xs:simpleType>

And this in the XML: 而在XML中:

<OracleSID>Productio</OracleSID>

and must be 并且必须

<OracleSID>Production</OracleSID>

which is one of the valid values. 这是有效值之一。

2.- You have this element in your XML: 2.-您的XML中包含以下元素:

<Administrator EmailAlias="jhunter" Extension="6007">

but is not defined in your XSD. 但未在您的XSD中定义。 Maybe you wanted to use this other element: 也许您想使用其他元素:

<xs:element name="Administrator-type" type="Administrator-type" minOccurs="1" maxOccurs="3"/>

So you must replace this: 因此,您必须替换以下内容:

    <Administrator EmailAlias="jhunter" Extension="6007">
       Jeffrey Hunter
    </Administrator>

with this 有了这个

    <Administrator-type EmailAlias="jhunter" Extension="6007">
        Jeffrey Hunter
    </Administrator-type>

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

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