【问题标题】:Namespace error in XSDXSD 中的命名空间错误
【发布时间】:2014-05-15 04:40:05
【问题描述】:

使用 SAX 解析器验证时的错误是:

"org.xml.sax.SAXParseException;systemId:file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd;lineNumber:10;columnNumber:31;src-resolve.4.1:错误解析组件“名称”。 检测到“name”没有命名空间,但没有目标命名空间的组件无法从模式文档“file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd”中引用。 如果 'name' 打算有一个命名空间,也许需要提供一个前缀。如果打算让“name”没有命名空间,则应将没有“命名空间”属性的“import”添加到“file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd”。 "

address.xml

<?xml version ="1.0" encoding="UTF-8"?>
<address 

    xmlns:personal="Personal things"
    xmlns:houses="Regarding to houses"  

    xmlns="http://www.w3schools.com" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
    xsd:schemaLocation="address.xsd"

>
    <name>  
        <personal:title>Mr.</personal:title>
        <first-name>Samitha</first-name>
        <last-name>Chathuranga</last-name>
    </name>
    <sssd></sssd>
    <house-id>
        <houses:title>107 B</houses:title>
        <NAME>Sam&apos;s Home</NAME>
        <!--  An intnal entity is used for the single quote in House Name here-->
    </house-id>
    <village>Poramba</village>
    <city district="Galle" province="Southern">AG</city>
    <postal-code>80300</postal-code>
    <country>Sri Lanka</country>
</address>

address.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/SampleSchema" 
    xmlns:tns="http://www.example.org/SampleSchema" 

    >
    <xsd:element name="address">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="name" />
                <xsd:element ref="house-id" />
                <xsd:element ref="village" />
                <xsd:element ref="city" />
                <xsd:element ref="postal-code" />
                <xsd:element ref="country" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="name">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="title" />
                <xsd:element ref="first-name" />
                <xsd:element ref="last-name" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="title" type="xsd:string" />
    <xsd:element name="first-name" type="xsd:string" />
    <xsd:element name="last-name" type="xsd:string" />

    <xsd:element name="house-id">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="title" />
                <xsd:element ref="NAME" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="NAME" type="xsd:string" />


    <xsd:element name="village" type="xsd:string" />
    <xsd:element name="country" type="xsd:string" />
    <xsd:element name="city">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:length value="2" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:element>
    <xsd:element name="postal-code">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:pattern value="[0-9]{5}(-[0-9]{4})?" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:element>

</xsd:schema>

如何解决这个错误?

【问题讨论】:

  • 当引用一个元素时,总是添加前缀:&lt;xsd:element ref="tns:name" /&gt;,因为你声明的所有元素都有一个命名空间(目标命名空间)。这类似于在 Java 中使用完全限定的类名。

标签: java xml namespaces xsd saxparser


【解决方案1】:

那么你的 xsd 是无效的。你构建它是错误的。以下是我的建议。下载 XMLSpy 或 Liquid XML studio,然后查看我的示例以了解如何构建适当的 XSD,然后针对它验证 XSD。这些编辑器是一种直观地查看 XML 文档和 xsd 的好方法。他们会有很大帮助。

基本上,您需要在 XSD 中声明您的类型,然后根据这些类型创建元素。虽然您的方法可行,但我建议您研究我采用的方法,这很容易。我可以解释这一切,但我想你会很快明白的。

Address.xsd

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2013 Designer Edition 11.1.0.4725 (http://www.liquid-technologies.com)-->
<xsd:schema xmlns:address="http://www.example.org/AddressSchema"
        targetNamespace="http://www.example.org/AddressSchema"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="address">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="person_name"
                         type="address:person_name_type" />
            <xsd:element name="ssd"
                         type="address:ssd_type" />
            <xsd:element name="house_id"
                         type="address:house_id_type" />
            <xsd:element name="village"
                         type="address:village_type" />
            <xsd:element name="city"
                         type="address:city_type" />
            <xsd:element name="postal_code"
                         type="address:postalcode_type" />
            <xsd:element name="country"
                         type="address:country_type" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>
<xsd:simpleType name="name_type">
    <xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="postalcode_type">
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="[0-9]{5}(-[0-9]{4})?" />
    </xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="city_type">
    <xsd:restriction base="xsd:string">
        <xsd:length value="2" />
    </xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="village_type">
    <xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="country_type">
    <xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="title_type">
    <xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="first_name_type">
    <xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:simpleType name="last_name_type">
    <xsd:restriction base="xsd:string" />
</xsd:simpleType>
<xsd:complexType name="house_id_type">
    <xsd:sequence>
        <xsd:element name="title"
                     type="address:title_type" />
        <xsd:element name="name"
                     type="address:name_type" />
    </xsd:sequence>
</xsd:complexType>
<xsd:complexType name="person_name_type">
    <xsd:sequence>
        <xsd:element name="title"
                     type="address:title_type" />
        <xsd:element name="first_name"
                     type="address:first_name_type" />
        <xsd:element name="last_name"
                     type="address:last_name_type" />
    </xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="ssd_type">
    <xsd:restriction base="xsd:string" />
</xsd:simpleType>
</xsd:schema>

地址.xml

 <?xml version="1.0" encoding="utf-8"?>
 <!-- Created with Liquid XML 2013 Designer Edition 11.1.0.4725 (http://www.liquid-technologies.com) -->
 <address xsi:schemaLocation="http://www.example.org/AddressSchema   D:\GroundZero\address.xsd" xmlns="http://www.example.org/AddressSchema"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <person_name>
      <title>string</title>
      <first_name>string</first_name>
      <last_name>string</last_name>
  </person_name>
  <ssd>string</ssd>
  <house_id>
      <title>string</title>
      <name>string</name>
  </house_id>
  <village>string</village>
  <city>AB</city>
  <postal_code>80300</postal_code>
  <country>string</country>
</address>

【讨论】:

    【解决方案2】:

    我还要注意,您的实例文档在命名空间http://www.w3schools.com 中具有根(地址)元素,而您的架构在命名空间http://www.example.org/AddressSchema 中定义了一个地址元素。你不能只是像仙尘一样散布命名空间并希望它们为你的代码增添魅力;它们是基本的,您需​​要正确处理它们。

    【讨论】:

      猜你喜欢
      • 2012-07-16
      • 2010-12-23
      • 1970-01-01
      • 2012-06-01
      • 2023-04-09
      • 2013-06-17
      • 1970-01-01
      • 1970-01-01
      • 2014-08-01
      相关资源
      最近更新 更多