当前位置: 首页 > 知识库问答 >
问题:

xml文档未验证为xsd架构

秦博达
2023-03-14

使用eclipse时,我消除了所有错误,但当我更改xml文档中的元素内容时,超出了中设置的限制。xsd文件没有出现验证错误。我已尝试使用联机验证http://www.freeformatter.com/xml-validator-xsd.html我得到了错误“Cvc elt.1:找不到元素“DatabaseInventory”…行“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个答案

叶英哲
2023-03-14

你有两件事做错了。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>
<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>
马琛
2023-03-14

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

首先,您的模式没有指定目标命名空间,这意味着您的实例文档不应声明默认命名空间:

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

将其更改为:

<DatabaseInventory >

其次,xsi: Schemaloce属性接受{URI, URL}对的列表,其中URI是命名空间URI,URL是相应模式的位置。但是,由于您的模式没有声明目标名称空间,因此您应该改用xsi: noNamespaceSchemaLoce属性。您的第一个元素应该如下所示:

<DatabaseInventory 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="DatabaseInventory.xsd">
 类似资料:
  • 我没有编写C++代码,因为我的首要任务是创建有效的xml和XSD。 xml: XSD: 为了验证,我转到:https://www.freeformatter.com/xml-validator-xsd.html 错误:s4s-att-invalid-value:元素“Element”中“type”的属性值无效。记录原因:cvc-datatype-valid.1.2.1:“xs:”不是“QName”

  • 问题内容: 但是,这将返回一条错误消息:线程“ main”中的异常java.lang.IllegalArgumentException:无法加载实现由http://www.w3.org/2001/XMLSchema -instance 指定的模式语言的SchemaFactory。 这是我的代码还是实际的xsd文件有问题? 问题答案: 该错误意味着您安装的Java没有任何可解析XMLSchema文件

  • 我在验证xml和xsd时遇到问题。我从xsd模式中得到这个错误。 src解决方案。4.2:解析组件“urn:id”时出错。检测到“urn:id”位于命名空间“urn:schemas microsoft com:xml-diffgram-v1”中,但此命名空间中的组件无法从架构文档中引用virtual://server/schema.xsd。如果名称空间不正确,可能需要更改“urn:id”的前缀。如

  • 我是XSD新手,不知道为什么我的XSD没有进行验证。我收到以下错误: s4s-elt-无效-内容.1:“参数信息”的内容无效。元素“复杂类型”无效、放错位置或出现过于频繁。 cvc复合型。2.4。d: 发现以元素“exception”开头的无效内容。此时不需要任何子元素。 XML: XSD: 我错过了什么吗?我想通过使用复杂类型并引用它们来分解它,从而使其更容易...

  • 问题内容: 我正在生成一些XML模式,并希望确保我们的生成器正在创建有效的XML模式文档(不是XML)。我试图提出验证XML Schema文档的代码,但失败了。我不认为会这么复杂。 这段代码与我需要做的非常接近,但是出现以下错误。 我在调用setSchema(uri)时使用URL,以确保XMLSchema.dtd和datatypes.dtd是相对的,希望它们可用,但是我不确定如何进行检查。 我猜想

  • 问题内容: 我需要使用给定的XSD文件验证XML文件。我只需要如果验证正常就返回true,否则返回false的方法。 问题答案: 仅返回true或false(也不需要任何外部库):