xml文档未验证xsd架构

时间:2014-01-05 10:20:38

标签: xml eclipse validation xsd schema

使用eclipse我已经摆脱了所有错误但是当我更改我的xml文档中的元素内容超出了.xsd文件中设置的限制时,没有出现验证错误。我已经尝试用http://www.freeformatter.com/xml-validator-xsd.html在线验证它,我收到错误“Cvc-elt.1:无法找到元素声明'数据库库存'..行'4',列'69'”但是它已经在eclipse中了验证罚款。不知道我做错了什么。

这是我的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>

这是架构

<?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>

2 个答案:

答案 0 :(得分:1)

在这种情况下,eclipse不执行任何验证,因为它无法将模式与实例文档相关联。

首先,您的架构未指定目标命名空间,这意味着您的实例文档应该声明默认命名空间:

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

将其更改为:

<DatabaseInventory >

其次,xsi:schemaLocation属性采用{URI, URL}对列表,其中URI是名称空间URI,URL是相应模式的位置。但是,由于您的架构未声明目标名称空间,因此您应该使用xsi:noNamespaceSchemaLocation属性。你的第一个元素应如下所示:

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

答案 1 :(得分:0)

你有两件事是错的。 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>

这在XML中:

<OracleSID>Productio</OracleSID>

并且必须

<OracleSID>Production</OracleSID>

这是有效值之一。

2.-你的XML中有这个元素:

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

但未在您的XSD中定义。也许你想使用这个其他元素:

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

所以你必须替换它:

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

用这个

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