xmlns与targetNamespace

xmlns与targetNamespace

xmlns与targetNamespace
http://blog.sina.com.cn/weatry
在使用XML Schema生成XML文件时,我们常常会在一个元素中看到xmlns和targetNamespace这两个属性,它们可以在任何一个XML元素上使用,它们究竟是什么含义呢?
1、xmlns使用方法
xmlns的使用格式是xmlns:namespace-prefix="namespace"
xmlns可以使用在任何元素上,表明该元素及其子元素,可以通过相应的前缀名引用定义在名字空间内的元素,可以引入多个名字空间。
比如:
<wsdl:definitions
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
</wsdl:definitions>
上述代表的含义是在definitions元素下,可以通过xsd前缀名和wsdl前缀分别引用名字空间"http://www.w3.org/2001/XMLSchema"和"http://schemas.xmlsoap.org/wsdl/"中定义的元素。
2、targetNamespace
由于在XML中并不一定都只是引用一个名字空间中的元素,有时也可能是要向一个名字空间中加入新的元素,那么应该如何向一个名字空间中加入元素呢?这时就要使用到targetNamespace。
targetNamespace也可以用在任何元素上,它代表在这个元素及其子元素下定义的任何元素、数据类型等都会被定义在指定的名字空间上。例如:
<wsdl:definitions
       targetNamespace="http://com.liuxiang.xfireDemo/HelloService"
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
    <wsdl:message name="xx">.....</wsdl:message>

</wsdl:definitions>


http://stackoverflow.com/questions/4126919/what-is-the-difference-between-targetnamespace-and-xmlnstarget

Answered quite well over here: targetNamespace and xmlns without prefix, what is the difference?

To restate:targetNamespace="" - As the current XML document is a schema this attribute defines the namespace that this schema is intended to target, or validate.

xmlns="" - Defines the default namespace within the current document for all non-prefixed elements (i.e no yada: in <yada:elementName>)

原文地址:https://www.cnblogs.com/daishuguang/p/4234547.html