繁体   English   中英

何时使用 W3C 的 xmlns 和 xmlns:xsi 命名空间?

[英]When to use W3C's xmlns and xmlns:xsi namespaces?

我在这里有一个关于 XML 的相当基本的问题。 在声明元素的xmlns属性时,使用http://www.w3.org/2001/XMLSchema是否合法? 并且在声明xmlns:xsi属性时,使用http://www.w3.org/2001/XMLSchema-instance作为值是否合法?

I ask this because I have seen XML auto-generated by some XML editors which references these namespaces, and yet (at least on the XMLSchema-instance page) W3C says that "This schema should never be used as such: the XML Schema Recommendation forbids此命名空间中的属性声明”。

所以我很困惑; 说这样的话总是违法的:

<?xml version="1.0" encoding="UTF-8"?>
<myElement xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    [...]
</myElement>

...如果不是,为什么 W3C 似乎说永远不应该使用命名空间? 一个人会用它做什么? 我尝试阅读 XML 1.0 规范,但它非常冗长且难以理解。

该名称空间不应用于其他任何用途。 它必须用于引用在该命名空间中声明的属性。

您不得在属于其他人的命名空间中声明任何元素或属性。 这意味着您不能在“XMLSchema”命名空间中声明您的myElement


<?xml version="1.0" encoding="UTF-8"?> 
<root xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <genericElement xsi:type="xs:string">string</genericElement>
</root>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM