XML 属性

XML elements can have attributes in the start tag, just like HTML.
XML 元素可以像HTML那样在起始标签内书写属性。

Attributes are used to provide additional information about elements.
属性是用来提供与元素相关的附加信息的。


XML Attributes
XML属性

XML elements can have attributes.
XML 元素可以有属性。

From HTML you will remember this: <IMG SRC="computer.gif">. The SRC attribute provides additional information about the IMG element.
在HTML教程中,你一定还记得这个:<IMG SRC="computer.gif">, SRC 属性提供了与 IMG 元素相关的附加信息。

In HTML (and in XML) attributes provide additional information about elements:
在HTML(以及XML)中,属性是提供与元素相关的附加信息的。

<img src="computer.gif">
<a href="demo.asp">

Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but important to the software that wants to manipulate the element:
属性通常提供除数据部分以外的信息,在下面的案例中,文件类型与数据不相关,但对处理元素的软件却很重要:

<file type="gif">computer.gif</file>

 


 

Quote Styles, "female" or 'female'?
引号类型,"female" 还是 'female'?

Attribute values must always be enclosed in quotes, but either single or double quotes can be used. For a person's sex, the person tag can be written like this:
属性值必须书写在引号里,单引和双引都可以用。以某人性别为例,人物标签可以这样书写:

<person sex="female">

or like this:
或者这样

<person sex='female'>

Note: If the attribute value itself contains double quotes it is necessary to use single quotes, like in this example:
注意:
如果属性值本身包含双引号,那引用整句时就必须使用单引号,见下面的案例:

<gangster name='George "Shotgun" Ziegler'>

Note: If the attribute value itself contains single quotes it is necessary to use double quotes, like in this example:
注意:
如果属性值本身包含单引号,那引用整句时就必须使用双引号,见下面的案例:

<gangster name="George 'Shotgun' Ziegler">

 


Use of Elements vs. Attributes
比较元素和属性的使用方法

Data can be stored in child elements or in attributes.
数据可以存储在子元素或属性中。

Take a look at these examples:
请看下面这些案例:

<person sex="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>

 

<person>
<sex>female</sex>
<firstname>Anna</firstname>
<lastname>Smith</lastname>

</person>

In the first example sex is an attribute. In the last, sex is a child element. Both examples provide the same information.
在第一个案例中,性别是一个属性。在最后的案例中,性别是一个子元素。两个案例提供的信息一样。

There are no rules about when to use attributes, and when to use child elements. My experience is that attributes are handy in HTML, but in XML you should try to avoid them. Use child elements if the information feels like data.
至于何时使用属性或何时使用子元素并没有具体的规定。我的经验是:对于HTML中的属性来说,操作起来比较容易,但在XML中,你应该尽量避免用属性。如果信息中包含大量的数据,那么就尽量使用子元素。


My Favorite Way
我最喜欢的方法

I like to store data in child elements.
我喜欢将数据存储在子元素中。

The following three XML documents contain exactly the same information:
以下三份XML文档都包含相同的信息:

A date attribute is used in the first example:
第一个案例中使用了日期属性:

<note date="12/11/2002">
<to>Tove</to>
<from>Jani</from>

<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

A date element is used in the second example:
第二个案例中使用了日期元素:

<note>
<date>12/11/2002</date>

<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

An expanded date element is used in the third: (THIS IS MY FAVORITE):
第三个案例中使用了扩展日期元素(这是我们常用的方法):

<note>
<date>
<day>12</day>
<month>11</month>

<year>2002</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>
</note>

 


Avoid using attributes?
避免使用属性?

Should you avoid using attributes?
你真的应该避免使用属性吗?

Some of the problems with using attributes are:
使用属性会带来的一些问题:

  • attributes cannot contain multiple values (child elements can)
    属性不能含有多重值(子元素可以)
  • attributes are not easily expandable (for future changes)
    属性不易扩展(这里说的可扩展性是针对将来的属性变化来讲的)
  • attributes cannot describe structures (child elements can)
    属性不能描述结构(如果是子元素的话可以描述结构)
  • attributes are more difficult to manipulate by program code
    属性不易被程序代码处理
  • attribute values are not easy to test against a Document Type Definition (DTD) - which is used to define the legal elements of an XML document
    属性值不易被文本类型定义(DTD)测试,DTD是用来定义XML文档中的合法元素的

If you use attributes as containers for data, you end up with documents that are difficult to read and maintain. Try to use elements to describe data. Use attributes only to provide information that is not relevant to the data.
如果你使用属性作为储存数据的容器,那么最终生成的文件将不易于阅读和长时间持续地使用。试着使用元素对数据进行描述;仅在提供一些与数据无关的信息是才使用属性。

Don't end up like this (this is not how XML should be used):
不要这样结尾(XML不应该这样使用):

<note day="12" month="11" year="2002"

to="Tove" from="Jani" heading="Reminder"
body="Don't forget me this weekend!">
</note>

 


An Exception to my Attribute Rule
我的属性使用规则之例外

 

Rules always have exceptions.
规则常有例外。

My rule about attributes has one exception:
我的属性使用规则有一个例外。

Sometimes I assign ID references to elements. These ID references can be used to access XML elements in much the same way as the NAME or ID attributes in HTML. This example demonstrates this:
有时我把ID参数赋值给元素。这些ID参数可用来访问XML元素,这与HTML中的 NAME 或 ID  属性存取XML元素的方式大致相同。以下的案例可以说明问题:

<messages>

<note id="p501">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>
</note>

<note id="p502">
<to>Jani</to>

<from>Tove</from>
<heading>Re: Reminder</heading>
<body>I will not!</body>
</note>

</messages>

The ID in these examples is just a counter, or a unique identifier, to identify the different notes in the XML file, and not a part of the note data.
这些例子中ID只是一个“计数器”,或者可以称之为一个特别的“识别器”,用于识别XML文件中不同的note信息,但它们不是note数据的一部分。

What I am trying to say here is that metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.
我想着重强调的是,metadata(元数据:与数据相关的数据)应该以属性方式储存,而数据本身应该以元素方式储存。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值