gpt4 book ai didi

xmlns、xmlns :xsi, xsi :schemaLocation, 和 targetNamespace?

转载 作者:数据小太阳 更新时间:2023-10-29 01:37:28 27 4
gpt4 key购买 nike

对于以下 XML 片段:

<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 属性的确切含义是什么?它们有什么关系? : 的作用是什么?

xsi:schemaLocation=

中有 2 个 URL
  1. http://maven.apache.org/POM/4.0.0 (刚好和xmlns一样但是访问的时候报404错误。)
  2. http://maven.apache.org/xsd/maven-4.0.0.xsd (这是一个实际的 XSD 文档)

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

最佳答案

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

  • xmlnsW3C Namespaces in XML Recommendation 的一部分:

    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 项目中元素的默认 namespace .

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

    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 的常规绑定(bind),它正确地设置了以下属性的使用:

    • xsi:type允许 XML 实例直接关联元素类型信息,而不是通过 XSD。参见 How to restrict the value of an XML element using xsi:type in XSD?

      在您的示例中,未使用xsi:type;包含在此处是为了有关 xsi 的完整性。

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

      在您的示例中,未使用xsi:nil;包含在此处是为了有关 xsi 的完整性。

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

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

      如果您的示例未使用 namespace ,您将使用 xsi:noNamespaceSchemaLocation,其值为单个 XSD-location-URI这暗示了预期 XSD 的位置并且应该是可检索的。

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

关于xmlns、xmlns :xsi, xsi :schemaLocation, 和 targetNamespace?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34202967/

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