targetNamespace和没有前缀的xmlns,有什么区别? [英] targetNamespace and xmlns without prefix, what is the difference?

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

问题描述

在 xml 架构文档中,如果我同时拥有 targetNamespace 和 xmlns 没有前缀.

In an xml schema document, if I have both the targetNamespace and the xmlns without a prefix.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            targetNamespace="http://example.com/" xmlns="http://example.com/">

它们之间的确切区别是什么?我的理解是,如果您有一个没有前缀的 xmlns,则所有没有前缀的元素都会获得该命名空间,并且……令人困惑的是 targetNamespace 也是如此.

What is the exact difference between them? My comprehension is that if you have an xmlns without a prefix, all elements without prefix gets that namespace and...confusingly the same goes for targetNamespace.

推荐答案

targetNamespace 是一个 XML Schema神器";其目的:指示模式文件描述的特定 XML 命名空间.

targetNamespace is an XML Schema "artifact"; its purpose: to indicate what particular XML namespace the schema file describes.

xmlns - 因为 XML Schema 是一个 XML 文档,所以可以为 XML 文件本身定义一个默认的 XML 命名空间(这就是 xmlns 属性所做的);影响是多重的:创作和组合.例如,不必为模式中定义的项目使用前缀,这些项目稍后会在同一文件中的其他地方引用(例如,用作属性或元素类型的全局 simpleType).

xmlns - because the XML Schema is an XML document, it is then possible to define a default XML namespace for the XML file itself (this is what xmlns attribute does); the implications are multiple: authoring, and composition. For example, one does not have to use a prefix for the items defined in the schema, that are later on referenced elsewhere in the same file (e.g. a global simpleType used as a type for an attribute or element).

根据我的经验,许多 XML 模式作者认为这是最佳实践"……所以您走在正确的轨道上.

From my experience, many XML Schema authors consider this as a "best practice"... so you're on the right track.

在 XSD 方面,targetNamespace 规定了架构组件的限定名称的命名空间部分,包括元素、属性、组和属性组以及简单和复杂类型.XSD 中定义的某些限定名称(元素和属性)由 XML 实例文档直接"使用.其他的,例如类型,可以通过 xsi:type 属性引用在实例 XML 文档中.其余的(组、属性组)用于促进模式组合(通过引用).

In terms of XSD, the targetNamespace prescribes the namespace part of a qualified name of a schema component, which includes elements, attributes, groups and attribute groups, and simple and complex types. Some of the qualified names defined in an XSD (elements and attributes) are "directly" used by an XML instance document. Others, such as for types, can be referenced through the xsi:type attribute in instance XML documents. The rest (groups, attribute groups) are there to facilitate schema composition (through references).

我也认为(通常)人们从两个角度设计 XSD:

I'm also of opinion that (in general) people come at designing XSD from two angles:

  • 匹配现有的 XML.在这种情况下,如果您的 XML 使用命名空间,那么对于所使用的每个命名空间,您最终都会得到一个具有匹配 targetNamespace 属性的 XSD 架构元素.

  • to match an existing XML. In this case, if your XML uses namespaces, for each of the namespaces used, you'll end up with an XSD schema element with a matching targetNamespace attribute.

纯建模.然后,您可以将 targetNamespace 视为类似于 UML 包、数据库模式、Java 包或 .NET 名称空间,以及它在本例中的全部含义.从根本上说,它是一种避免命名冲突的机制;尽管如此,它也是一种在主题领域等中划分模型的机制.

pure modeling. You then think of targetNamespace similar to an UML package, or database schema, or a Java package, or a .NET namespace, and all it means in this case. Fundamentally it is a mechanism to avoid naming collisions; nonetheless, it is also a mechanism to partition models in subject areas, etc.

这篇关于targetNamespace和没有前缀的xmlns,有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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