上文把XML和Schema的命名空间的一些相关内容进行了详细介绍,下面通过例子来具体了解:
例一:重点理解名称空间的相关概念。
下面的例子是一个XML Schema文件,名为"note.xsd"
- <?xml version="1.0"?>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- targetNamespace="http://www.w3schools.com"
- xmlns="http://www.w3schools.com"
- elementFormDefault="qualified">
- <xsd:element name="note">
- <xsd:complexType>
- <xsd:sequence>
- <xsd:element name="to" type="xs:string"/>
- <xsd:element name="from" type="xs:string"/>
- <xsd:element name="heading" type="xs:string"/>
- <xsd:element name="body" type="xs:string"/>
- </xsd:sequence>
- </xsd:complexType>
- </xsd:element>
- </xsd:schema>
下面的XML文档和上文给出的XML Schema相关联,名为"note.xml"。并且下文的讨论将围绕这两个文档展开。
- <?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>
此片段:xmlns:xsd="http://www.w3.org/2001/XMLSchema",表明此schema中使用的元素和数据类型来自于"http://www.w3.org/2001/XMLSchema"名称空间(namespace)。它同样指出来自于"http://www.w3.org/2001/XMLSchema"名称空间的元素和数据类型必须使用带"xsd: "前缀。作为名称空间的标识符(在声明中作为元素或属性的前缀),你也可以不使用xsd或xsi。这个 xmlns属性包含了基本的XML schema元素,比如element, attribute, complexType, group, simpleType等。
此片段:targetNamespace="http://www.w3schools.com",表明此schema (note, to, from, heading, body)定义的元素来自于"http://www.w3schools.com"名称空间。这个targetNamespace属性表示了该schema所对应的名称空间的URI。也就是说在引用该Schema的其它文档(包括自身文档)中要声明名称空间,其URI应该是targetNamespace的属性值。例如在这里因为要用到note.xsd自己定义的扩展数据类型(note, to, from, heading, body),所以也声明了名称空间xmlns="http://www.w3schools.com"。而且该名称空间是默认名称空间(没有前缀)。targetNamespace属性为在模式中显式创建的所有新类型均声明了XML名称空间。
我们再来看由该schema规定的XML文档note.xml的开头将是什么样子:
- <note xmlns="http://www.w3schools.com"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.w3schools.com note.xsd">
其中缺省名称空间声明xmlns="http://www.w3schools.com"就是和刚刚声明的XML Schema的名称空间相结合来规定该XML文档。(即该文档用到了此名称空间中定义的数据) xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 是任何XML实例文档固有的XML模式实例名称空间,它由XML模式规范定义。而xsi:schemaLocation="http://www.w3schools.com note.xsd"则规定了该名称空间所对应的schema的位置,即在相同路径的note.xsd文件。
例二:重点理解Schema文档使用自身定义类型
xsd文件中定义了一个targetNameSpace后,其内部定义的元素,属性,类型等都属于该targetNameSpace,其自身或外部xsd文件使用这些元素,属性等都必须从定义的targetNameSpace中找。修改一下note.xsd,去除默认名称空间的声明,并添加一个复杂类型:
- <?xml version="1.0"?>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- targetNamespace="http://www.w3schools.com"
- elementFormDefault="qualified">
- <xsd:element name="note">
- <xsd:complexType>
- <xsd:sequence>
- <xsd:element name="to" type="xs:string"/>
- <xsd:element name="from" type="xs:string"/>
- <xsd:element name="heading" type="xs:string"/>
- <xsd:element name="body" type="xs:string"/>
- </xsd:sequence>
- </xsd:complexType>
- </xsd:element>
- <xsd:element name="Student" type="stu"/>
- <xsd:complexType name="stu">
- <xsd:sequence>
- <xsd:element name="Name" type="xs:string"/>
- <xsd:element name="Class" type="xs:string"/>
- </xsd:sequence>
- </xsd:complexType>
- </xsd:schema>
上述代码中,复杂类型stu是找不到的,因为你定义了一个名称空间"http://www.w3schools.com",该复杂类型存在于"http://www.w3schools.com"中,因此应该修改代码如下:
- <?xml version="1.0"?>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- targetNamespace="http://www.w3schools.com"
- xmlns:student="http://www.w3schools.com"
- elementFormDefault="qualified">
- <xsd:element name="note">
- <xsd:complexType>
- <xsd:sequence>
- <xsd:element name="to" type="xs:string"/>
- <xsd:element name="from" type="xs:string"/>
- <xsd:element name="heading" type="xs:string"/>
- <xsd:element name="body" type="xs:string"/>
- </xsd:sequence>
- </xsd:complexType>
- </xsd:element>
- <xsd:element name="Student" type="student:stu"/>
- <xsd:complexType name="stu">
- <xsd:sequence>
- <xsd:element name="Name" type="xs:string"/>
- <xsd:element name="Class" type="xs:string"/>
- </xsd:sequence>
- </xsd:complexType>
- </xsd:schema>
若自身并不使用重用组件,仅供外部使用的话,则只定义targetNameSpace就可以,不用指定别名。
通过上面的例子,我们可以很深刻的理解targetNameSpace。targetNamespace定义了Schema定义的新元素与属性的名称空间。而"http://www.w3.org/2001/XMLSchema"名称空间则定义了element, attribute, complexType, group, simpleType等元素。
理解了上面的两个例子,Schema的命名空间的内容应该就明了了。
相关推荐
xml中xmlns和targetNamespace的相关信息。用来描述xml。
@WebService 注解用于标注一个类为WebService,targetNamespace 属性指定了该WebService 的命名空间。在上面的代码中,我们可以看到: ```xml @WebService(targetNamespace="http://ws.pay.dsp.org/") ``` 这个...
整合webserviec到项目,适用于目前没有接口的项目。 通过service接口进行数据的传递。 步骤清晰,代码截屏齐全。
3. 定义某个节点的都有什么样的子节点,可以有多少个子节点,子节点出现的顺序 4. 定义元素或者属性的数据类型 5. 定义元素或者属性的默认值或者固定值 Xml Schema的根元素: 表示数据类型等定义来自w3 ...
call.setOperationName(new QName(targetNamespace, "targetNamespace")); //设置参数名: call.addParameter("arg0", //参数名 XMLType.XSD_STRING,//参数类型:String ParameterMode.IN); //参数模式:'IN...
<?xml version="1.0" encoding="UTF-8" standalone="no"?... targetNamespace="http://code.alibabatech.com/schema/dubbo"> <xsd:import namespace="http://www.springframework.org/schema/tool"/>
一、java端首先我使用的是java自带的对webservice的支持包来编写的服务端和发布程序,代码... * User: Administrator * Date: 14-3-5 * Time: 下午3:11 */@WebService(targetNamespace = “http://xxx.com/wsdl”
targetNamespace="http://code.alibabatech.com/schema/dubbo"> <xsd:documentation><![CDATA[ Namespace support for the dubbo services provided by dubbo framework. ]]> ...
利用注解修改webservice生成wsdl的服务名称,命名空间,返回值,调用方法。加强安全性。代码完整,包括服务端和客户端两个项目
由于DOM4J在解析XML时只能一层一层解析,所以当XML文件层数过多时使用会很不方便, 结合XPATH就可以直接获取到某个元素 使用dom4j支持xpath的操作的几种主要形式 第一种形式 /a/b/c: 表示一层...
targetNamespace="http://www.springframework.org/schema/mvc"
...targetNamespace="http://www.smsgateway.ca/sendsms.asmx" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
3. RuntimeService:在Activiti中,每当一个流程定义被启动一次之后,都会生成一个相应的流程对象实例。RuntimeService提供了启动流程、查询流程实例、设置获取流程实例变量等功能。此外它还提供了对流程部署,流程...
因为网上关于Eclipse XSD的中文资料比较少,而且关于Eclipse XSD的范例代码也凤毛麟角,但是有的时候我们需要生成一个带枚举限定的简单类型的XSD Schema,比如下面的格式, <?xml version=1.0 encoding=UTF-8?&...
tutorial-soap-spring-boot-cxf 教程如何使用 , 和创建,... 完全合同优先,使用生成的JAX-WS服务类不包装WSDL并使用原始的WSDL-包括正确的URL和TargetNamespace(推荐) 步骤4:随博客文章一起发布: (或德语版本:
PHP WSDL创建器支持的... WSDL Creator 2 PHP 7.1,PHP 7.2,PHP 7.3 WSDL Creator 3 PHP 7.3,PHP 7.4类注释@网络服务参数: name ( string "WebServiceAnnotations" )默认:服务名称targetNamespace ( string ...
统一待办中心集成数据接口说明
3) XML是一种元语言,可以用来定义其它标签语言; 4) XML没有定义任何标记,它提供了一种工具定义标记以及它们之间的结构关系; 5) XML是一种用于结构化文本交换的标记语言; 6) XML代表了内容的结构也代表了内容...
围棋xgf文件格式解析. schema location: C:\Source\misc\xgf\xml-spec\xgf.xsd targetNamespace: http://xmp.net/xgf/namespaces/xgf/20020407 Elements Complex types GameCollection XGFGameType XGFGame
步骤如下: 下载微软HTC组件:...definitions name=”WS_SYSTEM_LOGIN_SELECT” targetNamespace=”SCRM4U_SERVICE” xmlns:tns=”SCRM4U_SERVICE” xmlns:ws