xml基础

郁闷啊。。。刚才写了一份,但是提交的时候出了错,又没有保存。白写了。。。现在就接着刚才的那份向下写吧。

现在就编写一个xml文件,顺便来说下xml的特性。

<?xml version='1.0' encoding='utf-8'?>

<!-- A SAMPLE set of slides --> 
 这两行代码不用多说了,第一行是xml文件序言,声明这是一个xml文件,编码用的是utf-8,序言内还可以包含其他的声明:

version: Identifies the version of the XML markup language used in the data. This attribute is not optional.
encoding: Identifies the character set used to encode the data. ISO-8859-1 is Latin-1, the Western European and English language character set. (The default is 8-bit Unicode: UTF-8.)
standalone: Tells whether or not this document references an external entity or an external data type specification. If there are no external references, then "yes" is appropriate.
 <slideshow
    title="Sample Slide Show"
    date="Date of publication"
    author="Yours Truly"
    >
  </slideshow>

这段代码定义了这个xml文件的root element,没个xml文件只能有一个root element。slideshow tag里面包含了title,date,author属性。注意多个属性之间不像html文件一样用逗号隔开,而是用空格,而且要用引号括起来。

xml允许一种具有层次的数据结构,所以,tag可以包含tag。如:

<slideshow
  ...
  >

   <!-- TITLE SLIDE -->
  <slide type="all">
    <title>Wake up to WonderWidgets!</title>
  </slide>

</slideshow>

属性和子elements都能用来描述这个element,但是确实有区别的,我就引用j2eetutorial里面的一段话来说明二者的区别:More importantly, this example illustrates the difference between things that are more usefully defined as elements (the title element) and things that are more suitable as attributes (the type attribute). The visibility heuristic is primarily at work here. The title is something the audience will see, so it is an element. The type, on the other hand, is something that never gets presented, so it is an attribute. Another way to think about that distinction is that an element is a container, like a bottle. The type is a characteristic of the container (tall or short, wide or narrow). The title is a characteristic of the contents (water, milk, or tea). These are not hard-and-fast rules, of course, but they can help when you design your own XML structures.

 

下面就说下另一种标签process instructions,它的格式是<?target data?>.j2eetutorial里面是这样定义它的:give commands or information to an application that is processing the XML data

target:the application that is expected to do the processing

data:the instruction or information for it to process

举个例子:

<slideshow
  ...
  >
  <!-- PROCESSING INSTRUCTION -->
  <?my.presentation.Program QUERY="exec, tech, all"?>

  <!-- TITLE SLIDE -->


下面再来说下entity,entity就像是c语言里面的宏。格式: &entityName;

默认定义的entity有:

Character
Name
Reference
&
ampersand
&amp;
<
less than
&lt;
>
greater than
&gt;
"
quote
&quot;
'
apostrophe
&apos;

你也可以自己定义一个entity,关于如何定义一个entity放在DTD里面说。

 

现在在说下CDATA标签,格式<![CDATA[。。。。。。。。。]]>. 这个标签里面的内容将原封不动的被提取出来,不会被xml parsers所解析。

   ...
  <slide type="tech">
    <title>How it Works</title>
    <item>First we fozzle the frobmorten</item>
    <item>Then we framboze the staten</item>
    <item>Finally, we frenzle the fuznaten</item>
    <item><![CDATA[Diagram:
      frobmorten <--------------- fuznaten
        |            <3>             ^
        | <1>                        | <1> = fozzle
        V                            | <2> = framboze
      staten-------------------------+ <3> = frenzle
               <2>
    ]]></item>
  </slide>
</slideshow>
 就像上面的例子,像<的符号将不会被xml parsers解析的。

下面说下xml里面比较重要的一个东西DTD。在j2eetutorial 里面是这样被定义的:After the XML declaration, the document prolog can include a DTD, which lets you specify the kinds of tags that can be included in your XML document. In addition to telling a validating parser which tags are valid and in what arrangements, a DTD tells both validating and nonvalidating parsers where text is expected, which lets the parser determine whether the whitespace it sees is significant or ignorable.

像xml文件一样,一个dtd文件也要有它自己的声明:

<?xml version='1.0' encoding='utf-8'?>
<!--
  DTD for a simple "slide show"
-->

声明element是这样的:

<!-- DTD for a simple "slide show" -->
<!ELEMENT slideshow (slide+)>

它定义了一个slideshow标签,并且slideshow标签可以包含1个或多个slide标签。slide后面的+表示一个多者多个,还有其他的符号(qualifier):

Qualifier
Name
Meaning
?
Question mark
Optional (zero or one)
*
Asterisk
Zero or more
+
Plus sign
One or more

注意这样的定义将不被允许

<!ELEMENT item (#PCDATA | (#PCDATA, item+)) >

只能<!ELEMENT item (#PCDATA, item+) >

定义一个可以为空标签<!ELEMENT flag EMPTY> ,这就表示<flag/>是被允许的

说了这些DTD的内容,那么怎么在一个xml文件里面引用DTD呢?

<!--  A SAMPLE set of slides  -->
<!DOCTYPE slideshow SYSTEM "slideshow.dtd">
<slideshow


DOCTYPE标签在xml声明后面,root element前面。System标识符指定了DTD的位置,上面的例子中是一个相对路径,因为它没有http://或者file://的前缀。

DOCTYPE标签内也可以定义DTD定义,它们被括在方括号内,for example,

<!DOCTYPE slideshow SYSTEM "slideshow1.dtd" [
  ...local subset definitions here...
]>

上面说了如何在DTD里面定义标签,也就是element,那么如何在DTD里面定义属性(attribute)呢?

<!ELEMENT slideshow (slide+)>
<!ATTLIST slideshow
    title    CDATA    #REQUIRED
    date     CDATA    #IMPLIED
    author   CDATA    "unknown"
>

<!ELEMENT slide (title, item*)>
黑体部分就定义了属性。

Specification
Specifies...
#REQUIRED
The attribute value must be specified in the document.
#IMPLIED
The value need not be specified in the document. If it isn't, the application will have a default value it uses.
"defaultValue"
The default value to use if a value is not specified in the document.
#FIXED "fixedValue"
The value to use. If the document specifies any value at all, it must be the same.


定义一个entity:

<!DOCTYPE slideshow SYSTEM "slideshow.dtd" [
  <!ENTITY product  "WonderWidget">
  <!ENTITY products "WonderWidgets">

]>
引用的时候就用&product;就可以了。。。。&tagName; 

为了防止xml标签出现名字冲突,xml用名字空间(Namespace)来解决这个问题。

在DTD里面定义一个名字空间是这样做的,在需要名字空间的标签上定义一个叫xmlns的属性。

<!ELEMENT title (%inline;)*>
<!ATTLIST title
  xmlns CDATA #FIXED "http://www.example.com/slideshow"
>
想象一下,如果你只在一处会用到这个名字空间,当然用element属性没什么大碍,但是,要是会在多处用到这个名字空间的话,用属性并不是一个明智的选择。

所以这个时候就最好定义Namesapce predix。

<SL:slideshow xmlns:SL='http:/www.example.com/slideshow'
    ...>
  ...
</SL:slideshow>

这个就是定义一个SL的Namespace prefix。引用的时候也很简单

<SL:slideshow xmlns:SL='http:/www.example.com/slideshow'
      ...>
  ...
  <slide>
    <SL:title>Overview</SL:title>
  </slide>
  ...
</SL:slideshow>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值