本站首页    管理页面    写新日志    退出


«July 2025»
12345
6789101112
13141516171819
20212223242526
2728293031


公告

 

用一句流行话说,我是外行,我怕谁!

嘿嘿!其实嘛,我是说,我什么都不懂,说错了你别见怪!

 


我的分类(专题)

日志更新

最新评论

留言板

链接


Blog信息
blog名称:X'Me, Love
日志总数:20
评论数量:40
留言数量:0
访问次数:151116
建立时间:2008年8月8日




[xmlStudy]UnderstandingXPath03
文章收藏

半路和尚 发表于 2008/10/9 12:02:48

Selecting Attributes So far, you've learned only about elements. But what about attributes? Earlier, I said that elements and attributes don't differ very much, so you might expect that you can address them in the same way, as Listing 3.10 tries to do. Listing 3.10 Stylesheet Trying to Select Attributes 1: <?xml version="1.0" encoding="UTF-8"?>2: <xsl:stylesheet version="1.0"3:  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">4:5:  <xsl:template match="/">6:   Dessert of the Day:7:   <xsl:value-of select="/menu/desserts/dish[2]" />8:   Price: <xsl:value-of select="/menu/desserts/dish[2]/price" />9:  </xsl:template>10:11: </xsl:stylesheet> Line 8 in Listing 3.10 tries to get the value of the price attribute of a dish element. That this approach doesn't work is obvious from the result in Listing 3.11. Listing 3.11 Result from Applying Listing 3.10 to Listing 3.1 <?xml version="1.0" encoding="utf-8"?>   Dessert of the Day:   Chocolat Mousse   Price: The value-of element in Listing 3.10 doesn't yield a result. A gap appears in the result because no element matches the select expression. That is as it should be, because no price element exists. The dish element does, however, have a price attribute, which is what Listing 3.10 is supposed to select. What's wrong? Nothing is wrong. You just need to tell the processor that it needs to match an attribute, not an element. You can tell the processor that you're looking for an attribute by adding the @ character in front of the name. So, if line 8 in Listing 3.10 is supposed to point to an attribute, the path expression should be /menu/desserts/dish[2]/@price, as shown in Listing 3.12. The result in Listing 3.13 is now correct. Listing 3.12 Stylesheet Correctly Selecting an Attribute 1: <?xml version="1.0" encoding="UTF-8"?>2: <xsl:stylesheet version="1.0"3:  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">4:5:  <xsl:template match="/">6:   Dessert of the Day:7:   <xsl:value-of select="/menu/desserts/dish[2]" />8:   Price: <xsl:value-of select="/menu/desserts/dish[2]/@price" />9:  </xsl:template>10:11: </xsl:stylesheet> Listing 3.13 Result from Applying Listing 3.12 to Listing 3.1 <?xml version="1.0" encoding="utf-8"?>   Dessert of the Day:   Chocolat Mousse   Price: 5.95 Listing 3.12 produces the desired result. Because attributes don't have any child elements, you also know that no side effects will occur. The value of the attribute is always text. NOTE Attributes can have data types, but all are based on the text value. Data types will be thoroughly discussed on Day 10, "Working with Data Types." Because attributes don't have side effects, they are much easier to work with. Also, because all attributes of an element need to have a different name, you have no trouble with matching multiple attributes (and getting only the value of the first). The only way you can get multiple attributes is to start working with selections based on a wildcard character. Another point to consider is that attributes take less space in a document than elements. Elements have begin and end tags; an attribute doesn't need these tags. Because attributes have only a text value, a parser or processor can deal with them more quickly because it doesn't have to check for child elements. This is likely to have a positive effect on performance. 注:一个元素所有的属性名称都不同,所以匹配属性时不存在像匹配元素时出现的问题,即,仅获得第一个元素的值。    元素有开始和结束标签,而属性没有,即,属性占有的文件空间小。因为属性只有一个值,解析器不需要检查它是否含有子元素,因此解析时更快。这好像是实际效应方面的一个优点。 (我的体会:以前在什么地方看到的资料说,能处理成元素的尽量少用属性。这是以方便数据的自述性为目的的,因为,元素可以随意扩展,而属性不可以。现在看来,有些本来无需扩展的信息,没有必要做成元素,用属性标示可能更好。应该说,做程序的人更有体会:处理属性值比处理元素的数据速度要快一点。)


阅读全文(2760) | 回复(1) | 编辑 | 精华
 


回复:UnderstandingXPath03
文章收藏

烟雨朦胧发表评论于2008/10/9 16:08:04

看不明白。 以下为blog主人的回复:年轻时要恋爱,中年要养家,老年需要人照顾。实际上都是一回事。    


个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 


» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.031 second(s), page refreshed 144767814 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号