`
足至迹留
  • 浏览: 492761 次
  • 性别: Icon_minigender_1
  • 来自: OnePiece
社区版块
存档分类
最新评论

xml之 xmlns, xmlns:xsi, targetNamespace,xsd

    博客分类:
  • xml
阅读更多
Xml,html等标记语言一般开头都会有命名空间的定义。这里主要说明几个常见到的xmlns,targetNamespace, xmlns:ss等。

1. XML 命名空间提供避免元素命名冲突的方法。
当不同的xml文件里包含相同的标签但不同含义,又恰恰在同一个文件里被引用时就分辨不出这个标签的含义了,使用命名空间来区分就不会存在歧义了,w3school里有详细的举例说明,可以参考:http://www.w3school.com.cn/xml/xml_namespaces.asp

2.xmlns, xmlns:xsi, targetNamespace

先看一个例子:
<?xml version="1.0"?>

<note xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">

<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
这里的根元素里引用了几个命名空间。

1)xmlns  (xmlns="http://www.w3schools.com")
-- specifies the default namespace declaration. This declaration tells the schema-validator that all the elements used in this XML document are declared in the "http://www.w3schools.com" namespace.
这个是定义了默认命名空间,所有不带前缀的元素都是使用这个默认命名空间里的元素。


2)xmlns:xsi  (xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance")
-- indicates that the elements and data types used in the schema come from the " http://www.w3.org/2001/XMLSchema-instance " namespace. It also specifies that the elements and data types that come from the " http://www.w3.org/2001/XMLSchema-instance " namespace should be prefixed with xsi.
这个同样声明了可以使用的命名空间,但是使用时需要指明xsi前缀,比如:
<xsi:ss>使用xsi命名空间</xsi:ss> (ss应该是xsi里真实定义的schema).

注意:我们看到标识命名空间的一般都是一个url,而且可以访问,但其实不是必须为url的。用来标识命名空间的网络地址并不被XML解析器调用,XML解析器不需要从这个网络地址中查找信息,该网络地址的作用仅仅是给命名空间一个唯一的名字,因此这个网络地址也可以是虚拟的,然而又很多公司经常把这个网络地址值象一个真实的Web页面,这个地址包含了关于当前命名空间更详细的信息。
xml元素校验的xsd文件才必须是真实的,而且会拿来校验xml定义的是否合法。


补充xsd:
XSD是指XML结构定义 ( XML Schemas Definition )
XML Schema 是DTD的替代品。XML Schema语言也就是XSD。
XML Schema描述了XML文档的结构。可以用一个指定的XML Schema来验证某个XML文档,以检查该XML文档是否符合其要求。文档设计者可以通过XML Schema指定一个XML文档所允许的结构和内容,并可据此检查一个XML文档是否是有效的。XML Schema本身是一个XML文档,它符合XML语法结构。可以用通用的XML解析器解析它。
  一个XML Schema会定义:文档中出现的元素、文档中出现的属性、子元素、子元素的数量、子元素的顺序、元素是否为空、元素和属性的数据类型、元素或属性的默认和固定值。
  XSD是DTD替代者的原因,一是据将来的条件可扩展,二是比DTD丰富和有用,三是用XML书写,四是支持数据类型,五是支持命名空间。
  XSD文件的后缀名为.xsd。
  XML Schema的优点:
  1) XML Schema基于XML,没有专门的语法
  2) XML可以象其他XML文件一样解析和处理
  3) XML Schema支持一系列的数据类型(int、float、Boolean、date等)
  4) XML Schema提供可扩充的数据模型。
  5) XML Schema支持综合命名空间
  6) XML Schema支持属性组。
详细可以参考:http://wenku.baidu.com/link?url=DkLPOQNoOBybLALkh-v-6N9k-vOc4gpftKDRXYryzX0XZrduZz1Y3lh4Ejf07qFQRMxErGMT330OyxNd__U2zg9m-UoYU_0heEdcP6uwopu


再看一个例子:
<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
...
...
</xs:schema>

3) targetNamespace  (targetNamespace="http://www.w3schools.com")
这个是把当前文档声明成一个命名空间,类似java文件中的package。但这只是声明而没有使用。如果使用xmlns:target定义跟targetNamespace一样的值,则是定义了target前缀的命名空间就是使用当前文档定义的命名空间。如:
<schema xmlns="http://www.w3.org/2001/SchemaXML
        targetNamespace="http://www.example.com/name"
        xmlns:target="http://www.example.com/name">

还可以参考: http://blog.csdn.net/markho365/article/details/8295918

2
0
分享到:
评论

相关推荐

    spring 自定义xsd

    XSD(XML Schema Definition)是XML文档的结构定义语言,用于描述XML文档的元素、属性和数据类型,确保XML文件的格式正确性。Spring框架通过提供自己的XSD,为开发者提供了声明式配置的方式。 在Spring中自定义XSD...

    Java通过XML Schema校验XML

    XML Schema(XSD)作为一种用于描述XML文档结构和数据类型的标准,提供了更为强大的数据类型支持和约束定义能力,相较于早期的DTD(Document Type Definition),XSD被更多地应用于XML文档的校验中。 然而,Java中...

    XML_SCHEMA教程

    2. **数据类型系统**:XML Schema提供了丰富的内置数据类型,如`xs:string`、`xs:int`、`xs:date`等,同时允许自定义数据类型。 3. **复杂类型定义**:可以定义复杂的元素结构,包括嵌套元素、选择和组。 4. **...

    spring自定义标签

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:custom="http://www.springframework.org/schema/custom" xsi:schemaLocation="http://www.springframework.org/schema/beans ...

    XML_Schema 教程 详细版

    2. **Schema关联**:在XML文档的根元素中,通过`xmlns:xsi`和`xsi:schemaLocation`属性指定Schema文件的位置,将文档与Schema关联起来。 3. **数据验证**:使用XML解析器或专门的工具对XML文档进行验证,检查其是否...

    Spring源码解密之自定义标签与解析

    &lt;xsd:schema xmlns="http://www.battcn.com/schema/battcn" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" targetNamespace=...

    cxf-2.7.3与spring3整合开发步骤.

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context=...

    基于spring注解的cxf实现webservice

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"&gt; ...

    cxf+spring实现webservice例子

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans ...

    Spring + CXF + 注解方式(webService)

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ...

    webservice参数的获取.docx

    &lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;soap:Header/&gt; &lt;soap:Body&gt; ...

    CXF 框架实战代码---服务器端WebServices接口

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans ...

    javascrip调webservice

    "&lt;soap:envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" + " xmlns:xsd='http://www.w3.org/2001/XMLSchema'" + " xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'&gt;" + "&lt;soap:body&gt;...

    CXF配置.doc

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans ...

    JAVA CXF webservice的示例代码

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"&gt; &lt;jaxws:endpoint...

    spring整合cxf 实现webservice

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"&gt; &lt;!-- CXF ...

Global site tag (gtag.js) - Google Analytics