XML XSD 命名空间问题 [英] XML XSD namespace issues
问题描述
我正在尝试使用银行帐户示例定义一个简单的 XML 和 XSD 文件.
I am trying to define a simple XML and XSD file using the example of bank accounts.
这是我的 XSD 定义我的 XML 文件:
Here is my XSD defining my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:bank="http://www.w3.org/2001/XMLSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.auto-owners.com/accounts"
>
<xs:element name="accounts" >
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="account"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="account" type="account">
<xs:complexType>
<xs:sequence>
<xs:element ref="uuid"/>
<xs:element ref="name"/>
<xs:element ref="balance"/>
<xs:element ref="status"/>
<xs:element ref="opened"/>
<xs:element ref="closed"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="uuid" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="balance" type="xs:decimal"/>
<xs:element name="status" type="xs:NCName"/>
<xs:element name="opened" type="xs:string"/>
<xs:element name="closed" type="xs:string"/>
</xs:schema>
这是我使用上述 XSD 文件的 XML 文件:
And here is my XML file using the XSD file above:
<?xml version="1.1" encoding="UTF-8"?>
<bank:accounts
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.auto-owners.com/Account account.xsd"
xmlns:bank="http://www.auto-owners.com/Account"
>
<bank:account>
<bank:uuid>19cab0a2-c44b-4f3e-b24e-5f21dd23c7e8
</bank:uuid>
<bank:name>Bob Dylan</bank:name>
<bank:balance>1233.12</bank:balance>
<bank:status>active</bank:status>
<bank:opened>01/02/2000</bank:opened>
<bank:closed></bank:closed>
</bank:account>
<bank:account>
<bank:uuid>2e2142a2-1c36-4604-9bfd-f0681b89f775
</bank:uuid>
<bank:name>Bonnie Tyler</bank:name>
<bank:balance>34566.21</bank:balance>
<bank:status>active</bank:status>
<bank:opened>03/02/2000</bank:opened>
<bank:closed></bank:closed>
</bank:account>
<bank:account>
<bank:uuid>216361b1-0bd5-455d-b6a0-400f92f61d68
</bank:uuid>
<bank:name>Dolly Parton</bank:name>
<bank:balance>9876.32</bank:balance>
<bank:status>active</bank:status>
<bank:opened>01/05/2011</bank:opened>
<bank:closed></bank:closed>
</bank:account>
<bank:account>
<bank:uuid>140ee47a-d323-448d-a5e6-db2454a16934
</bank:uuid>
<bank:name>Gary Moore</bank:name>
<bank:balance>8764.12</bank:balance>
<bank:status>hold</bank:status>
<bank:opened>01/22/2010</bank:opened>
<bank:closed></bank:closed>
</bank:account>
</bank:accounts>
我一直在尝试定义要使用的命名空间,每次尝试绑定到 bank: namespace 时,我都会遇到一系列错误.我目前在:
I have been running around trying to define the namespace to use and every time I try to bind to the bank: namespace I get a revolving set of errors. I am currently on:
解析组件帐户"时出错.检测到 'account' 没有命名空间,但是没有目标命名空间的组件是无法从架构文档file:///.../account.xsd"中引用.
Error resolving component 'account'. It was detected that 'account' has no namespace, but components with no target namespace are not referenceable from schema document 'file:///.../account.xsd'.
有什么想法吗?
推荐答案
以下是您必须更改的内容:
- 在您的 XSD 中,如 Jim Garrison 所述,
xmlns:bank="http://www.w3.org/2001/XMLSchema"
错误;这应该是xmlns:bank="http://www.auto-owners.com/accounts"
. - 在你的 XSD 中,当你引用一个类型或元素时,在它前面加上目标命名空间的命名空间前缀.
- 在您的 XML 中,
xsi:schemaLocation="http://www.auto-owners.com/Accountaccount.xsd"
应该是xsi:schemaLocation="http://www.auto-owners.com/accountsaccount.xsd"
; 必须注意大小写,以匹配字面上的命名空间.
- In your XSD, as noted by Jim Garrison,
xmlns:bank="http://www.w3.org/2001/XMLSchema"
is wrong; it should bexmlns:bank="http://www.auto-owners.com/accounts"
. - In your XSD, when you reference a type or element, prefix it with the namespace prefix of the target namespace.
- In your XML,
xsi:schemaLocation="http://www.auto-owners.com/Account account.xsd"
should bexsi:schemaLocation="http://www.auto-owners.com/accounts account.xsd"
; case matters and care must be taken to match the namespace literally.
修正后的XSD如下:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:bank="http://www.auto-owners.com/accounts"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.auto-owners.com/accounts">
<xs:element name="accounts">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="bank:account"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="account">
<xs:complexType>
<xs:sequence>
<xs:element ref="bank:uuid"/>
<xs:element ref="bank:name"/>
<xs:element ref="bank:balance"/>
<xs:element ref="bank:status"/>
<xs:element ref="bank:opened"/>
<xs:element ref="bank:closed"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="uuid" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="balance" type="xs:decimal"/>
<xs:element name="status" type="xs:NCName"/>
<xs:element name="opened" type="xs:string"/>
<xs:element name="closed" type="xs:string"/>
</xs:schema>
更正后的 XML 如下:
<?xml version="1.1" encoding="UTF-8"?>
<bank:accounts
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.auto-owners.com/accounts account.xsd"
xmlns:bank="http://www.auto-owners.com/accounts">
<bank:account>
<bank:uuid>19cab0a2-c44b-4f3e-b24e-5f21dd23c7e8
</bank:uuid>
<bank:name>Bob Dylan</bank:name>
<bank:balance>1233.12</bank:balance>
<bank:status>active</bank:status>
<bank:opened>01/02/2000</bank:opened>
<bank:closed></bank:closed>
</bank:account>
<bank:account>
<bank:uuid>2e2142a2-1c36-4604-9bfd-f0681b89f775
</bank:uuid>
<bank:name>Bonnie Tyler</bank:name>
<bank:balance>34566.21</bank:balance>
<bank:status>active</bank:status>
<bank:opened>03/02/2000</bank:opened>
<bank:closed></bank:closed>
</bank:account>
<bank:account>
<bank:uuid>216361b1-0bd5-455d-b6a0-400f92f61d68
</bank:uuid>
<bank:name>Dolly Parton</bank:name>
<bank:balance>9876.32</bank:balance>
<bank:status>active</bank:status>
<bank:opened>01/05/2011</bank:opened>
<bank:closed></bank:closed>
</bank:account>
<bank:account>
<bank:uuid>140ee47a-d323-448d-a5e6-db2454a16934
</bank:uuid>
<bank:name>Gary Moore</bank:name>
<bank:balance>8764.12</bank:balance>
<bank:status>hold</bank:status>
<bank:opened>01/22/2010</bank:opened>
<bank:closed></bank:closed>
</bank:account>
</bank:accounts>
以这种方式使用命名空间,XML 现在将根据 XSD 进行验证.
这篇关于XML XSD 命名空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!