xmln:tns and targetNamespace

倖福魔咒の 提交于 2019-12-03 08:32:19

问题


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

回答1:


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

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.



来源:https://stackoverflow.com/questions/17295588/xmlntns-and-targetnamespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!