分布式系统-XML.ppt_第1页
分布式系统-XML.ppt_第2页
分布式系统-XML.ppt_第3页
分布式系统-XML.ppt_第4页
分布式系统-XML.ppt_第5页
已阅读5页,还剩241页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1,XML,Steven Holzner, Sams Teach Yourself XML in 21 Days, Third Edition. 2003.,2,1. XML 基础,3,Markup Languages, Hello From HTML An HTML Document This is an HTML document! ,标记是文档中数据的描述和解释,4,XML,eXtensible Markup Language 元语言,创建标记语言的语言,5, Elements are nested Root element contains all others,Element (or

2、 tag) names,Example,elements,Root element,Empty element,attributes,declaration,6,More Terminology, John is a nice fellow 21 Main St. ,Opening tag,Closing tag: What is open must be closed,Nested element, child of Person,Parent of Address, Ancestor of number,“standalone” text, not useful as data,Child

3、 of Address, Descendant of Person,Content of Person,7,IE中浏览XML文档,8,An XML Document Using a Style Sheet, John is a nice fellow 21 Main St. . . ,9,ch01_04.css,Person display: block; font-size:18pt; color: #0000ff; text-align:left,10,使用JavaScript抽取数据, Hello From XML This is an XML document! ,11,使用JavaS

4、cript抽取数据, Retrieving data from an XML document function getData() xmldoc= document.all(firstXML).XMLDocument; nodeDoc = xmldoc.documentElement; nodeHeading = nodeDoc.firstChild; outputMessage = Heading: + nodeHeading.firstChild.nodeValue; message.innerHTML=outputMessage; ,12,使用JavaScript抽取数据, Retri

5、eving data from an XML document ,13,使用JavaScript抽取数据,14,使用Java从XML文档中抽取数据,import javax.xml.parsers.*; import org.w3c.dom.*; import java.io.*; public class ch01_06 static public void main(String argv) try DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; tr

6、y db = dbf.newDocumentBuilder(); catch (ParserConfigurationException pce) Document doc = null; doc = db.parse(ch01_02.xml);,15,使用Java从XML文档中抽取数据,for (Node node = doc.getDocumentElement().getFirstChild(); node != null; node = node.getNextSibling() if (node instanceof Element) if (node.getNodeName().e

7、quals(heading) StringBuffer buffer = new StringBuffer(); for (Node subnode = node.getFirstChild(); subnode != null; subnode = subnode. getNextSibling() if (subnode instanceof Text) buffer.append(subnode.getNodeValue(); System.out.println(buffer.toString(); catch (Exception e) e.printStackTrace(); ,1

8、6,使用Java从XML文档中抽取数据,java ch01_06 Hello From XML,17,Well-formed XML Documents,Must have a root element Every opening tag must have matching closing tag Elements must be properly nested is a no-no An attribute name can occur at most once in an opening tag. It it occurs, It must have a value (boolean a

9、ttrs, like in HTML, are not allowed) The value must be quoted (with “ or ) XML processors are not supposed to try and fix ill-formed documents (unlike HTML browsers),18,Valid XML Documents, Hello From XML This is an XML document! ,Valid XML Document,A valid XML document is defined by the W3C as a we

10、ll-formed XML document which also conforms to the rules of a Document Type Definition (DTD) or an XML Schema/ski:m/ (XSD),19,20,XML应用,XML用于存储、传输、结构化数据 纯文本格式使它容易被在互联网上传输并被不同平台上的应用所处理 过去5年中,已经出现了上百种XML子语言,21,使用MathML 显示4x2 5x + 6 = 0, 4 x + 6 = 0 ,22,在Amaya 浏览器中显示MathML 文档,23,XHTML,Extensible Hypertex

11、t Markup Language 更加严格 允许增加自己的标记 HTML 4.01(当前版本),24,An XHTML Document, An XHTML Page Welcome to XHTML! This is an XHTML document. Pretty cool, eh? ,25,在IE中显示,26,An SVG Document, SVG Example ,27,练习,valid XML 文档一定是well-formed 吗? well-formed XML 文档一定是valid吗?,28,编辑XML文档,XML 编辑器 XML Spy Visual Studio XML

12、 Designer XRay XML 浏览器 IE,Most powerful general Jumbo, CML browser,29,Jumbo,30,XML Validators,Make sure it is well formed and valid Scholarly Technology Groups validator Microsofts Visual Studio .NET,31,error.xml, Hello From XML This is an XML document! ,32,33,XML validation in Visual Studio .NET,34

13、,建立一个完整的XML文档, Hello From XML This is an XML document! ,35,XML 文档,Prologs XML declarations Processing instructions Elements and attributes Comments CDATA sections Entities,36,字符编码问题,ASCII仅有256个字符 Chinese, Armenian, Hebrew, Thai, Tibetan 从数量上、通用上,ASCII不能在Web上使用 Unicode (), 65,536

14、 个字符 前256个对应ASCII 但让所有的软件转去支持Unicode太困难 UCS Transformation Format-8 (UTF-8),37,UTF-8,所有ASCII字符的编码保持不变(8bit) 其它Unicode字符使用2字节,直至6字节编码 W3C requires all XML processors to support both UTF-8, UTF-16 大部分支持UTF-8,38,字符实体引用, Hello From XML This text is inside a element. ,39,字符实体引用,40,字符实体引用, Replaced with ,

15、实体,实体是一大段文本的别名 假如你为你的信件署名定义了一个实体lettersign,它代表下面这一大段文本: 张三 某网络公司销售部门 北京市海淀区中关村88号,100000,41,实体,邮件 收件人李四/收件人 主题hello/主题 正文晚上吃饭! overshadows old default,Redeclaration of cde; overshadows old declaration,55,Namespaces (续),xmlns=“ 并不意味着此URL上有一个文档,这种格式仅仅是个约定俗成。 一个namespace 仅仅是个标识符 一些前缀已经成为 标准。 E.g., xsd

16、for /2001/XMLSchema xsl for /1999/XSL/Transform Etc.,56,Valid XML Schemas supports 44+ datatypes,122,XML Schemas特性,增强的数据类型 44+ versus 10 Can create your own datatypes Example: This is a new type based on the string type and elements of this type must follow this patte

17、rn: ddd-dddd, where d represents a digit. 与实例文档的语法相同 less syntax to remember 面向对象 Can extend or restrict a type (derive new type definitions on the basis of old ones) 可以表达集合, i.e., can define the child elements to occur in any order ,123,BookStore.dtd, ,124,ATTLIST,ELEMENT,ID,#PCDATA,NMTOKEN,ENTITY,

18、CDATA,BookStore,Book,Title,Author,Date,ISBN,Publisher,This is the vocabulary that DTDs provide to define your new vocabulary,125,element,complexType,schema,sequence,/2001/XMLSchema,string,integer,boolean,BookStore,Book,Title,Author,Date,ISBN,Publisher, (targetNames

19、pace),This is the vocabulary that XML Schemas provide to define your new vocabulary,126, ,BookStore.xsd,xsd = Xml-Schema Definition,(explanations on succeeding pages),127, , ,128, ,All XML Schemas have schema as the root element.,129, ,The elements and datatypes that are used to construct schemas -

20、schema - element - complexType - sequence - string come from the http:/XMLSchema namespace,130,element,complexType,schema,sequence,/2001/XMLSchema,XMLSchema Namespace,string,integer,boolean,131, ,Indicates that the elements defined by this schema - BookStore - Book - Title - Author -

21、 Date - ISBN - Publisher are to go in the namespace,132,BookStore,Book,Title,Author,Date,ISBN,Publisher, (targetNamespace),Book Namespace (targetNamespace),133, ,This is referencing a Book element declaration. The Book in what namespace? Since there is no namespace

22、 qualifier it is referencing the Book element in the default namespace, which is the targetNamespace! Thus, this is a reference to the Book element declaration in this schema.,The default namespace is which is the targetNamespace!,134, ,实例文档中的任何元素 必须指定名字空间,135,在 XML 实例文档中引用schema

23、, My Life and Times Paul McCartney July, 1998 94303-12021-43892 McMillin Publishing . ,1. 声明默认的名字空间,告诉 schema-validator 本实例文档中所有元素来自于 名字空间 2. schemaLocation 告诉schema-validator 名字空间 由BookStore.xsd 定义 3. 告诉schema-validator 属性 schemaLocation 在 XMLSchema-instance 名

24、字空间中,1,2,3,136,schemaLocation,type,noNamespaceSchemaLocation,/2001/XMLSchema-instance,XMLSchema-instance Namespace,nil,137,在 XML 实例文档中引用schema,BookStore.xml,BookStore.xsd,targetNamespace=,schemaLocation= BookStore.xsd,- defines elements in namesp

25、ace ,- uses elements from namespace ,A schema defines a new vocabulary. Instance documents use that new vocabulary.,138,验证,BookStore.xml,BookStore.xsd,XMLSchema.xsd (schema-for-schemas),Validate that the xml document conforms to the rules described in BookStore.

26、xsd,Validate that BookStore.xsd is a valid schema document, i.e., it conforms to the rules described in the schema-for-schemas,139, ,(see example02),Note that http:/XMLSchema is the default namespace. Consequently, there are no namespace qualifiers on - schema - element - complexType - sequence - st

27、ring,140, ,Here we are referencing a Book element. Where is that Book element defined? In what namespace? The bk: prefix indicates what namespace this element is in. bk: has been set to be the same as the targetNamespace.,141,bk: References the targetNamespace,BookStore,Book,Title,Author,Date,ISBN,P

28、ublisher, (targetNamespace),/2001/XMLSchema,bk,element,complexType,schema,sequence,string,integer,boolean,Consequently, bk:Book refers to the Book element in the targetNamespace.,142,内联元素声明,143, ,Note that we have moved all the element declarations inline, and we a

29、re no longer refing to the element declarations. This results in a much more compact schema!,This way of designing the schema - by inlining everything - is called the Russian Doll design.,144,Anonymous types (no name), ,145,Named Types,使用命名的 complexType 的等价定义.,146, ,Named type,The advantage of split

30、ting out Books element declarations and wrapping them in a named type is that now this type can be reused by other elements.,147,Please note that: is equivalent to: ,Element A references the complexType foo.,Element A has the complexType definition inlined in the element declaration.,148,定义元素小结 (两种方

31、式),A simple type (e.g., xsd:string) or the name of a complexType (e.g., BookPublication), ,1,2,A nonnegative integer,A nonnegative integer or unbounded,Note: minOccurs and maxOccurs can only be used in nested (local) element declarations.,149,问题,日期不同于字符串 ISBN 格式: d-ddddd-ddd-d or d-ddd-ddddd-d or d-

32、dd-dddddd-d,150,数据类型,151,gYear 数据类型,A built-in datatype (Gregorian calendar year) Elements declared to be of type gYear must follow this form: CCYY range for CC is: 00-99 range for YY is: 00-99 Example: 1999 indicates the gYear 1999,152, ,Here we are defining a new (user-defined) data- type, called

33、ISBNType.,Declaring Date to be of type gYear, and ISBN to be of type ISBNType (defined above),153, ,新的数据类型 ISBNType. 它是字符串的约束: - 模式1: 1 digit followed by a dash followed by 5 digits followed by another dash followed by 3 digits followed by another dash followed by 1 more digit, or - 模式2: 1 digit fol

34、lowed by a dash followed by 3 digits followed by another dash followed by 5 digits followed by another dash followed by 1 more digit, or - 模式3: 1 digit followed by a dash followed by 2 digits followed by another dash followed by 6 digits followed by another dash followed by 1 more digit.,正规表达式,154,等

35、价的表达式, , ,The vertical bar means or,155, or ?,何时用 complexType element 何时用 simpleType element? 当定义子元素时使用 complexType element 当基于一个内置类型建立新类型时,使用 simpleType element,156,内置数据类型,Primitive Datatypes string boolean decimal float double duration dateTime time date gYearMonth gYear gMonthDay,Atomic, built-in

36、 Hello World true, false, 1, 0 7.08 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN P1Y2M3DT10H30M12.3S format: CCYY-MM-DDThh:mm:ss format: hh:mm:ss.sss format: CCYY-MM-DD format: CCYY-MM format: CCYY format: -MM-DD,Note: T is the date/time separator INF = infinit

37、y NAN = not-a-number,157,内置数据类型 (续),Primitive Datatypes gDay gMonth hexBinary base64Binary anyURI QName NOTATION,Atomic, built-in format: -DD (note the 3 dashes) format: -MM- a hex string a base64 string a namespace qualified name a NOTATION from the XML spec,158,内置数据类型 (续),Derived types normalizedS

38、tring token language IDREFS ENTITIES NMTOKEN NMTOKENS Name NCName ID IDREF ENTITY integer nonPositiveInteger,Subtype of primitive datatype A string without tabs, line feeds, or carriage returns String w/o tabs, l/f, leading/trailing spaces, consecutive spaces any valid xml:lang value, e.g., EN, FR,

39、. must be used only with attributes must be used only with attributes must be used only with attributes must be used only with attributes part (no namespace qualifier) must be used only with attributes must be used only with attributes must be used only with attributes 456 negative infinity to 0,159

40、,内置数据类型 (续),Derived types negativeInteger long int short byte nonNegativeInteger unsignedLong unsignedInt unsignedShort unsignedByte positiveInteger,Subtype of primitive datatype negative infinity to -1 -9223372036854775808 to 9223372036854775807 -2147483648 to 2147483647 -32768 to 32767 -127 to 128

41、 0 to infinity 0 to 18446744073709551615 0 to 4294967295 0 to 65535 0 to 255 1 to infinity,Note: the following types can only be used with attributes (which we will discuss later): ID, IDREF, IDREFS, NMTOKEN, NMTOKENS, ENTITY, and ENTITIES.,160,从基类型创建新数据类型,facets Example. string 有6个 facets: length m

42、inLength maxLength pattern enumeration whitespace (legal values: preserve, replace, collapse),161,通过指定facets的值创建新类型, ,1. This creates a new datatype called TelephoneNumber. 2. Elements of this type can hold string values, 3. But the string length must be exactly 8 characters long and 4. The string m

43、ust follow the pattern: ddd-dddd, where d represents a digit. (Obviously, in this example the regular expression makes the length facet redundant.),1,2,3,4,162,又例, ,This creates a new type called shape. An element declared to be of this type must have either the value circle, or triangle, or square.

44、,163,integer类型的Facets,integer 数据类型有 8 facets: totalDigits pattern whitespace enumeration maxInclusive maxExclusive minInclusive minExclusive,164,Example, ,This creates a new datatype called EarthSurfaceElevation. Elements declared to be of this type can hold an integer. However, the integer is restr

45、icted to have a value between -1290 and 29035, inclusive.,165,通过指定Facet 值建立新类型的一般格式, ,Facets: - length - minlength - maxlength - pattern - enumeration - minInclusive - maxInclusive - minExclusive - maxExclusive .,Sources: - string - boolean - number - float - double - duration - dateTime - time .,16

46、6,多个 Facets - and 还是or ?,An element declared to be of type TelephoneNumber must be a string of length=8 and the string must follow the pattern: 3 digits, dash, 4 digits., , ,An element declared to be of type shape must be a string with a value of either circle, or triangle, or square.,Patterns, enum

47、erations = or them together All other facets = and them together,167,从其它simpleType 建立 simpleType,168, , ,This simpleType uses EarthSurfaceElevation as its base type.,169,固定 Facet 的值, ,simpleTypes which derive from this simpleType may not change this facet.,170, , ,Error! Cannot change the value of a

48、 fixed facet!,171,包含用户自定义简单类型的元素,Example. Create a schema element declaration for an elevation element. Declare the elevation element to be an integer with a range -1290 to 29035 5240 Heres one way of declaring the elevation element: ,172,包含用户自定义简单类型的元素,Heres an alternative method for declaring elev

49、ation: ,The simpleType definition is defined inline, it is an anonymous simpleType definition. The disadvantage of this approach is that this simpleType may not be reused by other elements.,173,定义元素小结 (3种方式), ,1,2, ,3,174,Code to check the structure and content (datatype) of the data,Code to actuall

50、y do the work,In a typical program, up to 60% of the code is spent checking the data!,- source unknown,XML Schemas节约了$,Continued -,175,Code to check the structure and content of the data,Code to actually do the work,If your data is structured as XML, and there is a schema, then you can hand the data

51、-checking task off to a schema validator. Thus, your code is reduced by up to 60%! Big $ savings!,XML Schemas节约了$,176,XML Schemas的经典使用(贸易伙伴 - B2B),Supplier,Consumer,P.O.,Schema Validator,P.O. Schema,Software to Process P.O.,P.O. is okay,P.O.,(Schema at third-party, neutral web site),177,参考资料,Elliott

52、e Rusty Harold, XML Bible, Second Edition, 2001 Roger L. Costello, XML Schema Tutorial , 2001,178,XML who is neither subject to compulsion nor to hindrance, nor to force; whose movements to action are not impeded, whose desires attain their purpose, and who does not fall into that which he would avo

53、id. ,179,Style sheets 是 style rule 的集合,title display: block; font-size: 36pt; font-weight: bold; text-align: center; text-decoration: underline philosopher display: block; font-size: 16pt; text-align: center book display: block; font-size: 28pt; text-align: center; font-style: italic paragraph displ

54、ay: block; margin-top: 10,Selector,Rule Specification,property/value pairs,180,XML who is neither subject to compulsion nor to hindrance, nor to force; whose movements to action are not impeded, whose desires attain their purpose, and who does not fall into that which he would avoid. ,181,IE中的显示结果,182,格式化表格, First Name Last Name Loan Amount Fred Turner $100,000 Bill Saunders $120,000 ,183,格式化表格,document display:table; border-style:solid headers display:table-he

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论