xml targetNamespace和没有前缀的xmlns,有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7190572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:54:14  来源:igfitidea点击:

targetNamespace and xmlns without prefix, what is the difference?

xmlxsdschemaxml-namespacesprefix

提问by Abe

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

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

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

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.

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

回答by Petru Gardea

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

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

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

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

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

根据我的经验,许多 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:typeattribute in instance XML documents. The rest (groups, attribute groups) are there to facilitate schema composition (through references).

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

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

我也认为(一般来说)人们从两个角度设计 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.

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

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

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

回答by kimbert

For those who are still confused, consider these three xsds. They all define one global type and one global element definition that references it.

对于那些仍然感到困惑的人,请考虑这三个 xsd。它们都定义了一种全局类型和一种引用它的全局元素定义。

First, an xsd like the one posted above. It uses the prefix 'xsd' for the schema namespace, and a default namespace for the targetNamespace:

首先,像上面发布的那样的 xsd。它为架构命名空间使用前缀“xsd”,为 targetNamespace 使用默认命名空间:

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

  <xsd:element name="aGlobalElement" type="aGlobalType"/>

  <xsd:simpleType name="aGlobalType">
    <xsd:restriction base="xsd:string"/>
  </xsd:simpleType>   
</xsd:schema>  

Now the same xsd, but defining and using a namespace prefix for the target namespace:

现在相同的 xsd,但为目标命名空间定义和使用命名空间前缀:

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

  <xsd:element name="aGlobalElement" type="tns:aGlobalType"/>

  <xsd:simpleType name="aGlobalType">
    <xsd:restriction base="xsd:string"/>
  </xsd:simpleType> 
</xsd:schema>  

...and finally, a version that uses a default namespace instead of 'xsd' for the XML schema namespace:

...最后,使用默认命名空间而不是“xsd”作为 XML 架构命名空间的版本:

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

  <element name="aGlobalElement" type="tns:aGlobalType"/>

  <simpleType name="aGlobalType">
    <restriction base="string"/>
  </simpleType>
</schema>

Most of the schema authors choose the first or the last, because if the default namespace facility is available then we might as well use it for something.

大多数模式作者选择第一个或最后一个,因为如果默认命名空间设施可用,那么我们不妨将它用于某些事情

回答by HakunaMatata

xmlns

xmlns

The xmlns attribute sets the default name space of the described element. The default name space is thus applied to all the elements inside the described element, which do not explicitly declare another name space for themselves.

xmlns 属性设置所描述元素的默认名称空间。因此,默认名称空间应用于所描述元素内的所有元素,这些元素没有为自己显式声明另一个名称空间。

The default name space is set to a standard value for WSDL files: http://www.w3.org/ns/wsdl

默认名称空间设置为 WSDL 文件的标准值:http: //www.w3.org/ns/wsdl

targetNameSpace

目标名称空间

This attribute contains the name space of your web service. You can choose this name space freely, but there is a convention saying that the URI should point to the WSDL of the service.

此属性包含 Web 服务的名称空间。您可以自由选择这个名称空间,但有一个约定是 URI 应该指向服务的 WSDL。

xmlns:tns

xmlns:tns

This name space should be set to the same URI as the targetNameSpace attribute. That way you can refer to the target name space via this name space prefix (tns).

此名称空间应设置为与 targetNameSpace 属性相同的 URI。这样你就可以通过这个命名空间前缀 (tns) 来引用目标命名空间。

Source : http://tutorials.jenkov.com/wsdl/description.html

来源:http: //tutorials.jenkov.com/wsdl/description.html

回答by Premraj

namespace means like scope

命名空间意味着像范围

targetNamespaceis an attribute of schemaelement defines namespace i.e. package in XSD file. By convention we use URI/URLs, but we could use any string.

targetNamespaceschema元素的一个属性,定义了命名空间,即 XSD 文件中的包。按照惯例,我们使用 URI/URL,但我们可以使用任何字符串。

xmlnsis an attribute is used to refer elements and datatypes that come from xmlns attribute value for the current element scope.

xmlns是一个属性,用于引用来自当前元素范围的 xmlns 属性值的元素和数据类型。

For Example:

例如:

  • xmlns:xsd="http://www.w3.org/2001/XMLSchema"is with prefix as xsdmeans namespace should be prefixed with xsd:
  • xmlns="http://www.w3.org/2001/XMLSchema"without prefix is default
  • xmlns:p="http://www.example.com/People" is with prefix as pmeans namespace should be prefixed with p:
  • xmlns:xsd="http://www.w3.org/2001/XMLSchema"is with prefix asxsd意味着命名空间应该以xsd:
  • xmlns="http://www.w3.org/2001/XMLSchema"没有前缀是默认的
  • xmlns:p="http://www.example.com/People" 带有前缀,p表示命名空间应该带有前缀p:

Where xmlns:xsdand xmlns:pare QNames and xmlnsis local name.

其中xmlns:xsdxmlns:p是 QNames 并且xmlns是本地名称。

The following image helps to understand XSD using Java analogy as per my knowledge:

根据我的知识,下图有助于使用 Java 类比来理解 XSD:

enter image description here

在此处输入图片说明

回答by ARK

Other answers are good here, so I will not repeat their explanations here. However, if anyone from Java background, find its simpler, here is the analogy I came up with -

其他答案在这里很好,所以我不会在这里重复他们的解释。但是,如果任何来自 Java 背景的人发现它更简单,这是我想出的类比 -

  1. .xsddocument is the artifact/.jarfile
  2. xmlnsis the

    package com.example
    

    statement, you declare at the top of your Javaclasses.

  1. .xsd文档是工件/.jar文件
  2. xmlns是个

    package com.example
    

    语句,您在Java类的顶部声明。

Consider (for analogy), if you had one single package in your Java project, and all the classes are declared and defined within a single outer class.For eg,

考虑(类比),如果您的 Java 项目中有一个包,并且所有类都在单个外部类中声明和定义例如,

    package com.furniture.models

    public class FurnitureShop {

         int noOfTables;
         int noOfChairs;
         int noOfBeds;
         List<Table> tables;
         List<Chair> chairs;
         List<Bed> beds;

         // and now instead of declaring and defining a class for table/chair/bed in a 
         // separate file, you just add it here 
         public static class Table {
             int height;
             int width;
             int length;
             ...
         }

         public static class Chair {
             String color;
             ChairType chairType;
             ...
         }

         public static class Sofa {
             int price;
             String color;
             ...
         }
    }

This is how different elements are grouped in a single .xsdfile, for a new schema.

.xsd对于新模式,这就是将不同元素分组到单个文件中的方式。

  1. targetNamespaceis the name of the artifact you create.As you can find it out yourself, targetNamespaceis used when creating a schema, in an .xsdfile.
  1. targetNamespace是您创建的工件的名称正如您自己发现的那样targetNamespace文件中创建模式时使用.xsd

Once, the artifact(or .xsdfile) is created, you'd use it in other projects as follows -

一旦.xsd创建了工件(或文件),您就可以在其他项目中使用它,如下所示 -

In a Java project, you'd import the library, using pom.xml(or build.gradle) file as follows -

在 Java 项目中,您将使用pom.xml(或build.gradle)文件导入库,如下所示 -

    <dependency>
       <groupId>com.furniture</groupId>
       <artifactId>furniture-apis</artifactId>
       <version>1.1.1</version>
    </dependency>

In XML, you'd "import" the schema using

在 XML 中,您可以使用“导入”模式

    <furniture xmlns="http://furniture.com"/>

=== APPENDIX ===

=== 附录 ===

Clarification -

澄清 -

  1. xmlnsis both used as a packagestatement, as well as the importstatement in Java. In .xsdfile, xmlnsacts as the "package" statement, whereas in .xmlfiles, it acts as the "import" statement.
  1. xmlns既用作package语句,也用作importJava 中的语句。在.xsd文件中,xmlns充当“ package”语句,而在.xml文件中,它充当“ import”语句。

回答by typelogic

After some thorough testing using xmllintI think I found the definite explanation here. Consider the below schema:

在使用xmllint进行了一些彻底的测试后,我想我在这里找到了明确的解释。考虑以下架构:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema
version="1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://yyyzzz.com"
xmlns:p="http://abced.com"
xmlns:q="http://pqr.com"
xmlns="http://yyyzzz.com">

<xsd:element name="recipe" type="recipeType" />

<xsd:complexType name="recipeType">
    <xsd:simpleContent>
        <xsd:extension base="xsd:string">
        <xsd:attribute name="desc" type="xsd:string"  />
        <xsd:attribute name="archetype" type="xsd:string" />
        </xsd:extension>
    </xsd:simpleContent>
</xsd:complexType>
</xsd:schema>

The above schema validates to the below document:

上面的模式验证到下面的文档:

<?xml version="1.0"?>

<recipe xmlns="http://yyyzzz.com">
    Deciphering the purpose of targetNamespace
</recipe>

The reason that works is because xmlns="http://yyyzzz.com"automatically binds to the element being defined by the schema too! That means, it binds also to the recipeTypeelement.

有效的原因是因为xmlns="http://yyyzzz.com"也会自动绑定到模式定义的元素!这意味着,它还绑定到recipeType元素。

Now, with same xml document but with slightly modified schema as below also validates and take a close look at the difference:

现在,使用相同的 xml 文档但稍微修改架构,如下所示也验证并仔细查看差异:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema
version="1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://yyyzzz.com"
xmlns="http://eigenfield.aparicio.com"
xmlns:EGboy="http://yyyzzz.com">

<xsd:element name="recipe" type="EGboy:recipeType" />

<xsd:complexType name="recipeType">
    <xsd:simpleContent>
        <xsd:extension base="xsd:string">
        <xsd:attribute name="desc" type="xsd:string"  />
        <xsd:attribute name="archetype" type="xsd:string" />
        </xsd:extension>
    </xsd:simpleContent>
</xsd:complexType>

</xsd:schema> 

Ignore if the other xmlnsgone missing, but instead look closely at type="EGboy:recipeType". We can no longer rely on the xmlnsbecause it has different value therefore, we must put the prefix EGboyin front of recipeType.

忽略其他xmlns是否丢失,而是仔细查看type="EGboy:recipeType"。我们不能再依赖xmlns,因为它具有不同的值,因此我们必须在recipeType前面放置前缀EGboy

The xml document does not even care of the EGboyprefix this prefix is only for the schema to refer to the proper xmlnsin case there are many.

xml 文档甚至不关心EGboy前缀,这个前缀只是为了模式引用正确的xmlns,以防万一。