搬自我的博客:https://blog.cybertaotao.com/?p=27
XML是个什么?
简单的说是一种documentation language。全名Extensible Markup Language。text-oriented & data-oriented。包含元素(element)属性(attribute)文字(text),xml文件对应一个树,json文件对应一个哈希表
SGML->XML(子集)
XML->xhtml(应用):语法严格的html,从xml语法过来的,<p>必须</p>
SGML->html:一种应用,html是一种直接定义在SGML上的标记语言,允许<p>这种不关闭的标签
html->xhtml:xhtml定义更严格,固定了html词汇
<Katalog /> 这个叫empty element 相当于<Katalog></Katalog>,因为有的框架里需要这种空标签,可以获取相关的参数比如<Katalog value="value1" />或者xslt那种foreach标签(后面说。。。)
引用自:TU Munich.moodle::Elektronisches Publizieren / Document Engineering und das World-Wide Web (IN2032): Folie C –Prof.Anne Brüggemann-Klein
引用自:TU Munich.moodle::Elektronisches Publizieren / Document Engineering und das World-Wide Web (IN2032): Folie C –Prof.Anne Brüggemann-Klein
Namespace(命名空间)
理解为java的package(包) 或者c++的 “::” 命名空间(namespace),主要为了解决冲突。因为后面说到schema会定义结构,所以xml就有了意义,用A命名空间的schema去解析B命名空间的xml文件,参数不一样,但是tag一样,就解析不开,冲突了。所以有用。
正常有个默认的命名空间(default-namespace),默认命名空间用 xmlns=‘xxx’来声明,比如下面Katalog子标签不写前缀的都是xxx命名空间的,如果连这个默认的标签都懒得写,那叫 “anonymous or universal namespace” 匿名空间,这段文字 Titel属于默认命名空间也就是xxx,如果把xmlns=´xxx´去掉,Titel就属于匿名空间。
<Katalog nm:AutorIn=´ABK´
xmlns=´xxx´
xmlns:bk=´http://www.BVDtBuchhandel.de´
xmlns:nm=´http://www.LinkedIn.com´>
<Titel>Neuanschaffungen Mai 2000</Titel>
<bk:Buch>
<bk:Titel>Green Eggs and Ham</bk:Titel>
<bk:AutorIn>
<nm:Titel>Dr.</nm:Titel><nm:NName>Seuss</nm:NName>
</bk:AutorIn>
</bk:Buch>
</Katalog>
有一些傻子题专门考这样的:注意区分
<nm:Katalog xmlns:nm=´http://www.LinkedIn.com´ /> 这个属于nm
<Katalog xmlns:nm=´http://www.LinkedIn.com´ nm:AutorIn=´ABK´ /> 这个属于匿名的
XQuery是个啥?
XQuery vs XML 约等于 SQL vs 数据库,也就是一种查符合你要求的数据的这样一种语言。
讲XQuery之前必须讲XPath,XPath很像css选择器或者js的jquery选择器,很简单的选取xml的元素或者属性。
Xpath写法:Achse :: Knotentest [ Prädikat ] [ Prädikat ] …
Achse就是树轴,轴的几种关系:
关系 缩写
self: 自己本身 .
ancestor:祖先
descendent:后辈 //
attribute:属性关系 @
child:子关系 /
parent:父母关系 ..
Knotentest 可以是Name, 任意的Name(*), type(text(), node())
Prädikat 就是谓语 比如[@price>30]
组合起来:
self:node()[] 也就是.[]
parent::node()[] 也就是..[]
descendant::node() [contains(., "Fun")]也就是//[contains(.,"Fun")]
缩写之后例子:
//book/author [position() = 1] 也就是 //book/author[1]
相当于descendant::author[position() = 1]
注意// 等价于 /descendant-or-self::node()/
所以:
<card xmlns="http://businesscard.org">
<card> mycard </card>
</card>
xpath= //card 将会得到两个元素
而/card是一个元素,也就是最外层整个文档的元素,
/card相当于 doc("xxx.xml")/card
两种形式都可以正常在xpath中使用,省略写法比较便捷,用得多
xquery 几种等价写法:
xquery除了xpath之外还有FLWOR:for, let, where, order by, return
xquery1:向一个node里写入@attribute信息
<ingredients>
{for $i in (//ingredient/@name)
return
<ingredient>{$i}<!--(测试发现这里写{$i/@name}也可以,不知道为啥呢?--> {for $j in (//recipe)
where $j//ingredient[@name=$i]
return <recipe>{$j/@title}</recipe>
}
</ingredient>
}
</ingredients>
xquery2:向一个node的attribute里写入信息
<ingredients>
{for $i in (//ingredient/@name)
return
<ingredient name="{$i}">
{for $j in (//recipe)
where $j//ingredient[@name=$i]
return <recipe>{$j/@title}</recipe>
}
</ingredient>
}
</ingredients>
xquery3(只要洋葱): @attribute之后where self::[$i = ‘onion’] self 应该是@name attribute
<ingredients>
{for $i in (//ingredient/@name) where .[$i="onion"] return
<ingredient name="{$i}">
{for $j in (//recipe)
where $j//ingredient[@name=$i]
return <recipe>{$j/@title}</recipe>
}
</ingredient>
}
</ingredients>
变量在“[@name=$i]”里或者where $j//ingredient[@name=$i]不用{} 而在别的地方需要加{}
xpath等价写法的xquery FlWER:
xpath:
doc(“books.xml”)/bookstore/book[price>30]/title
xquery:
for $x in doc(“books.xml”)/bookstore/book
where $x/price>30
return $x/title
xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<collection>
<recipe title="Beef Parmesan">
<ingredient name="onion"/>
<ingredient name="eggs"/>
</recipe>
<recipe title="Ricotta Pie">
<ingredient name="filling">
<ingredient name="ricotta cheese"/>
<ingredient name="eggs"/>
</ingredient>
<ingredient name="dough">
<ingredient name="eggs"/>
</ingredient>
</recipe>
</collection>
Xml Schema 设计模版
xml schema 定义一个xml的结构,是dtd的代替品,完全是xml实现的,可扩展。XML Schema里面有2种type: structure type & data type
dtd,xml 两种语言比较:
原xml: 描述了个autor对象
<?xml version="1.0" encoding="UTF-8"?>
<gedicht xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="gedicht.xsd">
<autor>
<vorname>Rainer Maria</vorname>
<nachname>Rilke</nachname>
</autor>
<titel>Als ich die Universität bezog</titel>
</gedicht>
dtd: 描述原xml的结构,一目了然
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT gedicht (autor, titel) >
<!ELEMENT autor (vorname, nachname) >
<!ELEMENT titel (#PCDATA)* >
<!ELEMENT vorname (#PCDATA)* >
<!ELEMENT nachname (#PCDATA)* >
xml schema: Garden of Eden: 定义的type 是全局可见的, 定义的element也是全局可见的,global & global模型,就是全部打散,元素(element)全部使用引用方式(ref)
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="gedicht" type="gedichtTyp"/>
<xs:element name="autor" type="autorTyp"/>
<xs:element name="titel" type="xs:string"/>
<xs:element name="vorname" type="xs:string"/>
<xs:element name="nachname" type="xs:string"/>
<xs:complexType name="gedichtTyp">
<xs:sequence>
<xs:element ref="autor"/>
<xs:element ref="titel"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="autorTyp">
<xs:sequence>
<xs:element ref="vorname"/>
<xs:element ref="nachname"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
xml schema: Russian Doll: 套娃模型,定义的type 是局部可见的, 定义的element也是局部可见的,local & local模型, 遇到complexType(用来描述structure type的)就写sequence,seq里面包括很多个基本elements,element里面还有structure type就再解析,再seq 再element….直到全部完成。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="gedicht">
<xs:complexType>
<xs:sequence>
<xs:element name="autor">
<xs:complexType>
<xs:sequence>
<xs:element
name="vorname" type="xs:string"/>
<xs:element
name="nachname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="titel" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
xml schema: Salami Slice,定义的type 是局部可见的, 定义的element也是全局可见的,local & global模型, 把整体拆解成n个元素(element)的引用(ref),相当于先描述复杂的结构(structure type),后把所有元素(element)写在最后,通过引用相连。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="gedicht">
<xs:complexType>
<xs:sequence>
<xs:element ref="autor"/>
<xs:element ref="titel"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="autor">
<xs:complexType>
<xs:sequence>
<xs:element ref="vorname"/>
<xs:element ref="nachname"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="titel" type="xs:string"/>
<xs:element name="vorname" type="xs:string"/>
<xs:element name="nachname" type="xs:string"/>
</xs:schema>
xml schema: Venetian Blind威尼斯的瞎子,也就是百叶窗,定义的type 是全局部可见的, 定义的element也是局部可见,global & local模型, 先把所有复杂的类型定义出来(structure type),所有元素(element)直接写在structure type定义的里面。无ref
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="gedicht" type="gedichtTyp"/>
<xs:complexType name="gedichtTyp">
<xs:sequence>
<xs:element name="autor" type="autorTyp"/>
<xs:element name="titel" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="autorTyp">
<xs:sequence>
<xs:element name="vorname" type="xs:string"/>
<xs:element name="nachname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
以上是几种schema pattern,同时也是如何定义structure type。
Xml Schema 里面如何定义一个新的data type?
union restriction(enumeration, pattern…..) and list
<!-- this is a type define union of "golfAndWw" and "porsche" -->
<xs:simpleType name="jj">
<xs:union memberTypes="golfAndWw porsche" />
</xs:simpleType>
<!-- this is a type define using the restriction based on string -->
<xs:simpleType name="golfAndWw">
<xs:restriction base="xs:string">
<xs:enumeration value="golf" />
<xs:enumeration value="ww" />
</xs:restriction>
</xs:simpleType>
<!-- this is a type define using the restriction "xs:enumeration"-->
<xs:simpleType name="porsche">
<xs:restriction base="xs:string">
<xs:enumeration value="porsche" />
</xs:restriction>
</xs:simpleType>
本文所有代码例子来自于”TU Munich.moodle::Elektronisches Publizieren / Document Engineering und das World-Wide Web (IN2032): Skript und Materialien/schemaBeispieleGedichte –Prof.Anne Brüggemann-Klein”
xml快速入门
最新推荐文章于 2024-04-29 02:02:51 发布