使用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' . 检测到'name'没有名称空间,但没有目标名称空间的组件不能从模式文档'file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd'引用 . 如果'name'意图具有命名空间,则可能需要提供前缀 . 如果'name'没有命名空间,那么应该将'import'不带“namespace”属性添加到'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'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 回答
那么你的xsd是无效的 . 你构建错了 . 这是我的建议 . 下载XMLSpy或Liquid XML studio,然后查看我的示例,了解如何构建正确的XSD,然后针对它验证XSD . 这些编辑器是直观地查看XML文档和xsd的好方法 . 他们会帮助很多 .
基本上,您需要在XSD中声明类型,然后根据这些类型创建元素 . 虽然您的方法可行,但我建议您研究我采用的方法非常简单 . 我可以解释一下,但我认为你会很快得到这个想法 .
Address.xsd
Address.xml
我还要注意,您的实例文档在命名空间http://www.w3schools.com中具有根(地址)元素,而您的模式在命名空间http://www.example.org/AddressSchema中定义了一个地址元素 . 你不能只是像童话一样撒上名字空间,希望它们为你的代码增添魅力;它们是基础,你需要让它们正确 .