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

发布于 2024-12-01 03:52:35 字数 344 浏览 1 评论 0原文

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

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

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

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/">

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

棒棒糖 2024-12-08 03:52:35

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

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

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

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

我还认为,(一般来说)人们从两个角度来设计 XSD:

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

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

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.

幻想少年梦 2024-12-08 03:52:35

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

首先,像上面发布的那样的 xsd。它使用前缀“xsd”作为模式命名空间,使用默认命名空间作为目标命名空间:

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

现在是相同的 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>  

...最后,使用默认命名空间的版本而不是 XML 模式命名空间的“xsd”:

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

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

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

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: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: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:

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

彼岸花ソ最美的依靠 2024-12-08 03:52:35

xmlns

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

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

targetNameSpace

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

xmlns:tns

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

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

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.

The default name space is set to a standard value for WSDL files: 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.

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

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

浅笑依然 2024-12-08 03:52:35

命名空间意味着范围

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

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

例如:

  • xmlns:xsd="http://www.w3.org/2001/XMLSchema" 的前缀为 xsd 表示命名空间应以 为前缀xsd:
  • xmlns="http://www.w3.org/2001/XMLSchema" 不带前缀是默认的
  • xmlns:p="http://www.example.com/People " 的前缀为p 表示命名空间应以 p: 为前缀

,其中 xmlns:xsdxmlns:p 是 QNames 和 >xmlns 是本地名称。

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

在此处输入图像描述

namespace means like scope

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

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

For Example:

  • xmlns:xsd="http://www.w3.org/2001/XMLSchema" is with prefix as xsd means 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 p means namespace should be prefixed with p:

Where xmlns:xsd and xmlns:p are QNames and xmlns is local name.

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

enter image description here

不气馁 2024-12-08 03:52:35

其他答案都说得很好了,我就不再重复他们的解释了。但是,如果有 Java 背景的人会发现它更简单,这是我想出的类比 -

  1. .xsd 文档是artifact/.jar 文件
  2. xmlns

    包 com.example
    

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

考虑一下(类比),如果您的 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;
             ...
         }
    }

新架构,这就是如何将不同元素分组到单个 .xsd 文件中。

  1. targetNamespace 是您创建的工件的名称。您可以自己发现,targetNamespace 在创建架构时使用,.xsd 文件中。

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

在Java 项目,您可以使用以下命令导入库pom.xml (或 build.gradle)文件如下 -

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

说明来“导入”架构

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

在 XML 中,您可以使用=== 附录 ===

-

  1. xmlns 既用作 package 语句,也用作 Java 中的 import 语句。在 .xsd 文件中,xmlns 充当“package”语句,而在 .xml 文件中,它充当作为“import”语句。

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 -

  1. .xsd document is the artifact/.jar file
  2. xmlns is the

    package com.example
    

    statement, you declare at the top of your Java classes.

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,

    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 .xsd file, for a new schema.

  1. targetNamespace is the name of the artifact you create. As you can find it out yourself, targetNamespace is used when creating a schema, in an .xsd file.

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

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

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

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

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

=== APPENDIX ===

Clarification -

  1. xmlns is both used as a package statement, as well as the import statement in Java. In .xsd file, xmlns acts as the "package" statement, whereas in .xml files, it acts as the "import" statement.
囚我心虐我身 2024-12-08 03:52:35

经过使用 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>

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

<?xml version="1.0"?>

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

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

现在,使用相同的 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> 

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

xml 文档甚至不关心 EGboy 前缀,此前缀仅用于架构在存在多个时引用正确的 xmlns

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

<?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 recipeType element.

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

<?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 xmlns gone missing, but instead look closely at type="EGboy:recipeType". We can no longer rely on the xmlns because it has different value therefore, we must put the prefix EGboy in front of recipeType.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文