targetNamespace 和 xmlns 不带前缀,有什么区别?

编程入门 行业动态 更新时间:2024-10-26 14:31:13
本文介绍了targetNamespace 和 xmlns 不带前缀,有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

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 is an XML Schema "artifact"; its purpose: to indicate what particular XML namespace the schema file describes.

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).

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

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).

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

  • 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.

  • 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 不带前缀,有什么区别?

本文发布于:2023-07-28 14:05:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1231613.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:前缀   不带   有什么区别   targetNamespace   xmlns

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!