gpt4 book ai didi

java - XSD 中的命名空间错误

转载 作者:行者123 更新时间:2023-12-01 18:32:16 25 4
gpt4 key购买 nike

使用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 resolving component 'name'. It was detected that 'name' has no namespace, but components with no target namespace are not referenceable from schema document 'file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd'. If 'name' is intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'name' has no namespace, then an 'import' without a "namespace" attribute should be added to 'file:///home/samitha/svnrepo/XML/XML_XSDValidator/src/address.xsd'."

地址.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>

地址.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>

如何解决这个错误?

最佳答案

那么你的 xsd 无效。你构建它是错误的。这是我的建议。下载 XMLSpy 或 Liquid XML studio,然后查看我的示例,了解如何构建正确的 XSD,然后根据它验证 XSD。这些编辑器是直观地查看 XML 文档和 xsd 的好方法。他们会提供很多帮助。

本质上,您需要在 XSD 中声明您的类型,然后根据这些类型创建元素。虽然你的方法可行,但我建议你学习我所采用的方法,它非常简单。我可以解释这一切,但我认为您很快就会明白。

地址.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>

关于java - XSD 中的命名空间错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23669385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com