xmlns、xmlns:xsi、xsi:schemaLocation 和 targetNamespace? [英] xmlns, xmlns:xsi, xsi:schemaLocation, and targetNamespace?

查看:50
本文介绍了xmlns、xmlns:xsi、xsi:schemaLocation 和 targetNamespace?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下 XML 片段:

For the following XML fragment:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

xmlnsxmlns:xsixsi:schemaLocation 属性究竟是什么意思?它们有什么关系?: 有什么用?

What do the xmlns, xmlns:xsi, and xsi:schemaLocation attributes exactly mean? How are they related? What's the : for?

xsi:schemaLocation=

  1. http://maven.apache.org/POM/4.0.0 (它恰好与 xmlns 相同,但在访问时会出现 404 错误.)
  2. http://maven.apache.org/xsd/maven-4.0.0.xsd(这是一个实际的 XSD 文档)
  1. http://maven.apache.org/POM/4.0.0 (it happens to be the same as xmlns but it gives a 404 error when visiting.)
  2. http://maven.apache.org/xsd/maven-4.0.0.xsd (this is an actual XSD doc)

如果 1 不存在,为什么还要把它放在那里?

If 1 doesn't exist, why still put it there?

推荐答案

XML 和 XML Schema (XSD) 中的命名空间相关属性

  • xmlnsW3C Namespaces in XML Recommendation的一部分:

    前缀 xmlns 仅用于声明命名空间绑定和根据定义绑定到命名空间名称http://www.w3.org/2000/xmlns/.

    The prefix xmlns is used only to declare namespace bindings and is by definition bound to the namespace name http://www.w3.org/2000/xmlns/.

    在您的示例中,它声明 http://maven.apache.org/POM/4.0.0 是 Maven 项目中元素的默认命名空间.

    In your example, it declares that http://maven.apache.org/POM/4.0.0 is the default namespace for the elements in your Maven project.

    xmlns:xsi 为 XSD 中使用的核心命名空间声明标准命名空间前缀 (xsi):http://www.w3.org/2001/XMLSchema-instance

    xmlns:xsi declares a standard namespace prefix (xsi) for a core namespace used in XSD: http://www.w3.org/2001/XMLSchema-instance

    XML Schema: Structures 还定义了几个直接使用的属性在任何 XML 文档中.这些属性在不同的命名空间中,它具有命名空间名称http://www.w3.org/2001/XMLSchema-instance.为简洁起见,文本和本规范中的示例使用前缀 xsi: 代表 this后一个命名空间;实际上,可以使用任何前缀.

    XML Schema: Structures also defines several attributes for direct use in any XML documents. These attributes are in a different namespace, which has the namespace name http://www.w3.org/2001/XMLSchema-instance. For brevity, the text and examples in this specification use the prefix xsi: to stand for this latter namespace; in practice, any prefix can be used.

    在您的示例中,它声明了 xsi 命名空间前缀到 http://www.w3.org/2001/XMLSchema-instance 的常规绑定,正确设置以下属性的使用:

    In your example, it declares the conventional binding of the xsi namespace prefix to http://www.w3.org/2001/XMLSchema-instance, which properly sets up the use of the following attributes:

    • xsi:type allows an XML instance to associate element type information directly rather than through an XSD. See How to restrict the value of an XML element using xsi:type in XSD?

    在您的示例中,未使用 xsi:type;包括在此处以确保 xsi 的完整性.

    In your example, xsi:type is not used; included here for completeness regarding xsi.

    xsi:nil 允许在 XSD 可能不允许的情况下将空元素视为有效.

    xsi:nil allows an empty element to be considered to be valid when the XSD might not otherwise have allowed it.

    在您的示例中,未使用 xsi:nil;包括在此处以确保 xsi 的完整性.

    In your example, xsi:nil is not used; included here for completeness regarding xsi.

    xsi:schemaLocationxsi:noNamespaceSchemaLocation 向 XML 处理器提供有关如何将 XSD 与 XML 文档相关联的提示.有命名空间时使用xsi:schemaLocation;没有命名空间时使用 xsi:noNamespaceSchemaLocation.

    xsi:schemaLocation and xsi:noNamespaceSchemaLocation provide hints to the XML processor as to how to associate an XSD with an XML document. Use xsi:schemaLocation when there is a namespace; use xsi:noNamespaceSchemaLocation when there is no namespace.

    在您的示例中,有一个命名空间,因此您可以正确使用 xsi:schemaLocation,其值是空格分隔的 <强>命名空间XSD-location-URI.您的示例使用名称空间 http://maven.apache.org/POM/4.0.0,名称空间是词法命名结构,不需要可检索.您的示例还使用了 XSD-location-URI,http://maven.apache.org/xsd/maven-4.0.0.xsd,它应该是可检索的.

    In your example, there is a namespace, so you properly use xsi:schemaLocation, whose values are space-separated pairs of namespace and XSD-location-URI. Your example uses the namespace, http://maven.apache.org/POM/4.0.0, and namespaces are lexical naming constructs that need not be retrivable. Your example also uses the XSD-location-URI, http://maven.apache.org/xsd/maven-4.0.0.xsd, which is retrivable as it should be.

    如果您的示例没有使用命名空间,您将使用 xsi:noNamespaceSchemaLocation,其值为单个 XSD-location-URI这暗示了预期 XSD 的位置并且应该是可检索的.

    If your example did not use a namespace, you would use xsi:noNamespaceSchemaLocation, whose value is a single XSD-location-URI that hints to the location of the intended XSD and which should be retrievable.

    targetNamespacexs:schema 根上的一个属性XSD 的元素,它指定根元素的命名空间XSD 旨在管理的 XML 文档实例的数量.它必须匹配这些 XML 文档根的默认或显式命名空间元素.

    targetNamespace is an attribute on the xs:schema root element of an XSD which specifies the namespace of the root element of the XML document instances the XSD is intended to govern. It must match the default or explicit namespace of those XML documents' root elements.

    这篇关于xmlns、xmlns:xsi、xsi:schemaLocation 和 targetNamespace?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆