XSD中的命名空间错误

时间:2014-05-15 04:40:05

标签: java xml namespaces xsd saxparser

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

  

“org.xml.sax.SAXParseException; systemId:file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd; lineNumber:10; columnNumber:31; src-resolve.4.1:error解析组件'名称'。       检测到'name'没有名称空间,但没有目标名称空间的组件不能从模式文档'file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd'引用。       如果'name'意图具有命名空间,则可能需要提供前缀。如果'name'没有名称空间,那么应该在'file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd'中添加一个没有“namespace”属性的'import'。 “

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>

如何解决此错误?

2 个答案:

答案 0 :(得分:3)

你的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>

Address.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>

答案 1 :(得分:1)

我还要注意,您的实例文档在名称空间http://www.w3schools.com中有根(地址)元素,而您的模式在名称空间http://www.example.org/AddressSchema中定义了一个地址元素。你不能只是像童话一样撒上名字空间,希望它们为你的代码增添魅力;它们是基础,你需要把它们弄好。