首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>Xml编程>资讯:您应该知道的十种 XML 模式

您应该知道的十种 XML 模式

www.jz123.cn  2008-11-29   来源:   中国建站    编辑整理    我要投递新闻

本文考察了一些顶尖的 XML 模式,这些模式为各种各样的问题提供了解决方案,从基本的 Web 服务到数据描述等等。其中包括涉及到通讯录和发票的类似数据库的解决方案。本文选择模式的标准是根据其实用性和用途,及其在 XML 信息共享和交换方面对 XML 社区的影响。

 

本文考察了一些顶尖的 XML 模式,这些模式为各种各样的问题提供了解决方案,从基本的 Web 服务到数据描述等等。其中包括涉及到通讯录和发票的类似数据库的解决方案。本文选择模式的标准是根据其实用性和用途,及其在 XML 信息共享和交换方面对 XML 社区的影响。

SOAP

常用的缩写词

Ajax:异步 Javascript + XML(Asynchronous JavaScript + XML)

CPU:中央处理单元(Central Processing Unit)

CSS:级联样式表(Cascading Stylesheets)

HTML:超文本标记语言(Hypertext Markup Language)

OASIS:结构化信息标准推进组织(Organization for the Advancement of Structured Information Standards)

PDF:可移植的文档格式(Portable Document Format)

W3C:万维网联盟(World Wide Web Consortium)

XHTML:可扩展超本文标记语言(Extensible HyperText Markup Language)

XML:可扩展标记语言

XSLT:可扩展样式表语言转换(Extensible Stylesheet Language Transformations)

简单对象访问协议(Simple Object Access Protocol,SOAP)实际上是一种 Web 服务技术,但 Web 服务中客户机和服务器之间的数据交换格式是通过灵活的 XML 模式实现的。

Web 服务的主要优点是客户机和服务器通过网络进行信息和数据交换的互操作性的层次。SOAP 标准使用 XML 以一种体系结构中立的格式来构造数据,定义数据类型和信息。

对于编程语言来说,只需要提供数据类型和需要在远程服务器上调用的函数名称即可。SOAP 库将用主机语言编写的信息和格式转化成 XML 格式的消息,其中包括调用的函数和提供的参数。

通过 W3C 的例子就可以了解 SOAP 的结构。调用远程 SOAP 函数 GetEndorsingBoarder() 的时候,客户机上的调用程序生成清单 1 所示的 XML 消息。

 

清单 1. 调用远程 SOAP 函数 GetEndorsingBoarder()

<SOAP-ENV:Envelope

xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

<SOAP-ENV:Body>

<m:GetEndorsingBoarder xmlns:m="http://namespaces.snowboard-info.com">

<manufacturer>K2</manufacturer>

<model>Fatbob</model>

</m:GetEndorsingBoarder>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

SOAP 客户机发送的整个消息都放在 SOAP 信封中。信封的内容就是消息的详细内容。

被调用的函数显然是 GetEndorsingBoarder,它包括两个参数:manufacturer 和 model。由此可见,它把本地的可能采用二进制编码的字符串转化成了 XML 字符串。由于 XML 是平台独立的,主机使用 SOAP 系统不需要复杂的二进制编码和解码就可以交换消息。

服务器通过另一个 XML 编码的 SOAP 信封返回响应,这一次是函数的返回值。SOAP 请求的响应格式与函数相同,只不过在信封内容的后面加上了 Response,如清单 2 所示。

清单 2. SOAP 请求的响应

<SOAP-ENV:Envelope

xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

<SOAP-ENV:Body>

<m:GetEndorsingBoarderResponse xmlns:m="http://namespaces.snowboard-info.com">

<endorsingBoarder>Chris Englesmann</endorsingBoarder>

</m:GetEndorsingBoarderResponse>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

通常不需要自己编写 SOAP 消息,SOAP 库会自动生成。不过 SOAP 信封的结构和简单性表明使用 SOAP 标准共享信息很简单。

SOAP 大大简化了交换消息和调用远程函数的工作。远程过程调用(Remote Procedure Call,RPC)标准需要复杂的方法来处理二进制数据的序列化,发送结构化更高的信息需要详细的声明和双向的信息转换。

使用 SOAP,XML 序列化大大降低了这种复杂性,使得跨平台、跨语言集成和数据交换更加简单。

 

WSDL

Web 服务描述语言(Web Services Description Language,WSDL)提供了一种描述 Web 服务(大多使用 SOAP)的简单方法。WSDL 允许您描述利用 SOAP 标准所提供的服务和接口。

比方说,可以创建描述某台服务器上提供的服务的 WSDL 文件,然后把该文件分发给需要这些服务的 Web 服务消费者。通过阅读和解析 WSDL 文件,消费者能够了解到使用这些 Web 服务需要知道的所有信息,包括可以交换的数据类型、参数以及返回的各种错误和其他信息。

再次使用来自 W3C 的例子,可以看到不同远程函数的声明和交换的数据都是通过结构的 XML 定义处理的,如清单 3 所示。

清单 3. 不同远程函数和交换数据的 XML 定义

<?xml version="1.0"?>

<!-- root element wsdl:definitions defines set of related services -->

<wsdl:definitions name="EndorsementSearch"

targetNamespace="http://namespaces.snowboard-info.com"

xmlns:es="http://www.snowboard-info.com/EndorsementSearch.wsdl"

xmlns:esxsd="http://schemas.snowboard-info.com/EndorsementSearch.xsd"

xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"

xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

<!-- wsdl:types encapsulates schema definitions of communication types;

here using xsd -->

<wsdl:types>

<!-- all type declarations are in a chunk of xsd -->

<xsd:schema targetNamespace="http://namespaces.snowboard-info.com"

xmlns:xsd="http://www.w3.org/1999/XMLSchema">

<!-- xsd definition: GetEndorsingBoarder [manufacturer string,

model string] -->

<xsd:element name="GetEndorsingBoarder">

<xsd:complexType>

<xsd:sequence>

<xsd:element name="manufacturer" type="string"/>

<xsd:element name="model" type="string"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<!-- xsd definition: GetEndorsingBoarderResponse

[... endorsingBoarder string ...] -->

<xsd:element name="GetEndorsingBoarderResponse">

<xsd:complexType>

<xsd:all>

<xsd:element name="endorsingBoarder" type="string"/>

</xsd:all>

</xsd:complexType>

</xsd:element>

<!-- xsd definition: GetEndorsingBoarderFault

[... errorMessage string ...] -->

<xsd:element name="GetEndorsingBoarderFault">

<xsd:complexType>

<xsd:all>

<xsd:element name="errorMessage" type="string"/>

</xsd:all>

</xsd:complexType>

</xsd:element>

</xsd:schema>

</wsdl:types>

<!-- wsdl:message elements describe potential transactions -->

<!-- request GetEndorsingBoarderRequest is of type GetEndorsingBoarder -->

<wsdl:message name="GetEndorsingBoarderRequest">

<wsdl:part name="body" element="esxsd:GetEndorsingBoarder"/>

</wsdl:message>

<!-- response GetEndorsingBoarderResponse is of type

GetEndorsingBoarderResponse -->

<wsdl:message name="GetEndorsingBoarderResponse">

<wsdl:part name="body" element="esxsd:GetEndorsingBoarderResponse"/>

</wsdl:message>

<!-- wsdl:portType describes messages in an operation -->

<wsdl:portType name="GetEndorsingBoarderPortType">

<!-- the value of wsdl:operation eludes me -->

<wsdl:operation name="GetEndorsingBoarder">

<wsdl:input message="es:GetEndorsingBoarderRequest"/>

<wsdl:output message="es:GetEndorsingBoarderResponse"/>

<wsdl:fault message="es:GetEndorsingBoarderFault"/>

</wsdl:operation>

</wsdl:portType>

<!-- wsdl:binding states a serialization protocol for this service -->

<wsdl:binding name="EndorsementSearchSoapBinding"

type="es:GetEndorsingBoarderPortType">

<!-- leverage off soap:binding document style ...(no wsdl:foo pointing at

the soap binding) -->

<soap:binding style="document"

transport="http://schemas.xmlsoap.org/soap/http"/>

<!-- semi-opaque container of network transport details classed by

soap:binding above ... -->

<wsdl:operation name="GetEndorsingBoarder">

<!-- again bind to SOAP? ... -->

<soap:operation soapAction="http://www.snowboard-info.com/

EndorsementSearch"/>

<!-- further specify that the messages in the wsdl:operation

"GetEndorsingBoarder" use SOAP? ... -->

<wsdl:input>

<soap:body use="literal"

namespace="http://schemas.snowboard-info.com/EndorsementSearch.xsd"/>

</wsdl:input>

<wsdl:output>

<soap:body use="literal"

namespace="http://schemas.snowboard-info.com/EndorsementSearch.xsd"/>

</wsdl:output>

<wsdl:fault>

<soap:body use="literal"

namespace="http://schemas.snowboard-info.com/EndorsementSearch.xsd"/>

</wsdl:fault>

</wsdl:operation>

</wsdl:binding>

<!-- wsdl:service names a new service "EndorsementSearchService" -->

<wsdl:service name="EndorsementSearchService">

<wsdl:documentation>snowboarding-info.com Endorsement Service</

wsdl:documentation>

<!-- connect it to the binding "EndorsementSearchSoapBinding" above -->

<wsdl:port name="GetEndorsingBoarderPort"

binding="es:EndorsementSearchSoapBinding">

<!-- give the binding an network address -->

<soap:address location="http://www.snowboard-info.com/EndorsementSearch"/>

</wsdl:port>

</wsdl:service>

</wsdl:definitions>

WSDL 声明了消息类型、默认数据类型和内容以及交换的数据结构。

访问服务器上 SOAP 结构需要使用的一切信息都可以在这个 WSDL 中找到。大多数语言和环境都提供一种阅读和解析 WSDL 的机制,以确定可用的函数和数据交换。

WSDL 不仅定义了用于交换信息的 SOAP 接口,通过适当的 WSDL 生成程序,还可用于创建发送请求、生成并格式化响应所需要的代码。

WSDL 和 SOAP 组成了一个强大的远程过程调用系统。

上一篇:如何让WebServer返回指定XML内容 下一篇:XMLHTTP对象封装技术

评论总数:1 [ 查看全部 ] 网友评论


关于我们隐私版权广告服务友情链接联系我们网站地图