xmln:tns 和 targetNamespace [英] xmln:tns and targetNamespace

查看:58
本文介绍了xmln:tns 和 targetNamespace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一些 XSD 架构文档在其顶部 schema 元素中声明了 targetNamespacexmlns:tns 属性.例如.以下是从这里.它们似乎也具有相同的字符串值.我了解 targetNamespace 的作用,但是 xmlns:tns 在此基础上还能做什么?

I am seeing some XSD schema documents that declare both a targetNamespace and an xmlns:tns attribute in their top schema element. E.g. the following one taken from here. They also seem to have the same string value. I understand the role of targetNamespace but what does xmlns:tns do on top of that?

<?xml version="1.0" encoding="UTF-8"?>
<schema
    xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/Product"
    xmlns:tns="http://www.example.org/Product"
    elementFormDefault="qualified">
...

推荐答案

它允许您稍后在架构中引用命名空间.例如,如果您声明了一个命名类型,然后还想声明该类型的元素

It lets you refer to the namespace later in the schema. For example, if you declare a named type and then want to also declare an element of that type

<complexType name="someType">
  <!-- ... -->
</complexType>

<element name="someElement" type="tns:someType" />

简单地说 type="someType" 是行不通的,因为那将指的是 http://中的(不存在的)someTypewww.w3.org/2001/XMLSchema 命名空间(架构文件的 xmlns="...")而不是 http://www 中的命名空间.example.org/Product 命名空间.

Simply saying type="someType" wouldn't work because that would be referring to the (non-existent) someType in the http://www.w3.org/2001/XMLSchema namespace (the xmlns="..." of the schema file) rather than the one in the http://www.example.org/Product namespace.

这篇关于xmln:tns 和 targetNamespace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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