« | July 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | |
| 公告 |
用一句流行话说,我是外行,我怕谁!
嘿嘿!其实嘛,我是说,我什么都不懂,说错了你别见怪!
|
Blog信息 |
blog名称:X'Me, Love 日志总数:20 评论数量:40 留言数量:0 访问次数:151224 建立时间:2008年8月8日 |

| |
[xmlStudy]UnderstandingXPath02 文章收藏
半路和尚 发表于 2008/10/9 2:44:33 |
Getting the Value of a Single Element
The problem with the code shown so far is that it acts on an element or a set of elements. Within the set of elements (such as the dish elements), no one node is singled out. If you also address a dish node instead of matching it with a template, a reasonable assumption would be that you will get the value of a single dish node. Listing 3.6 tests this assumption.
Listing 3.6 Stylesheet Getting the Value of a dish Element
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: <xsl:apply-templates />7: </xsl:template>8:9: <xsl:template match="/menu/appetizers" />10:11: <xsl:template match="/menu/entrees">12: <xsl:value-of select="dish" />13: </xsl:template>14:15: <xsl:template match="/menu/desserts" />16:17: </xsl:stylesheet>
In Listing 3.6, the template on line 11 matching the entrees element selects only the value of a dish node on line 12. Note that, compared to Listing 3.4, there is no template matching the dish element. The result for Listing 3.6 is shown in Listing 3.7.
Listing 3.7 Result from Applying Listing 3.6 to Listing 3.1
<?xml version="1.0" encoding="utf-8"?>
Grilled SalmonIn
Listing 3.7, the assumption made for Listing 3.6 is correct. Getting the value of a dish node yields the value of exactly one dish node. But what happened to the other nodes? After all, the xsl:value-of element matches dish nodes, so it, in fact, matches a node-set. The fact that you get a single value is again a result of the default behavior of XSLT. If a node-set matches a xsl:value-of selection, only the value of the first element in the node-set is used. The first element is the one that comes first in the source XML, according to the selection—in this case, the Grilled Salmon.
注:在以上Listing 3.6中的
11: <xsl:template match="/menu/entrees">12: <xsl:value-of select="dish" />13: </xsl:template>
这三行获取的只是一个dish的值,即/menu/entrees的第一个子元素dish。实际上,<xsl:value-of select="dish" />选取的是dish的元素集,至于为什么只显示一个dish,这是xslt的默认规则。
(我的体会:用template来定义dish和直接用xsl::value-of select=“dish”,效果是不一样的。在UnderstandingXpath01的Listing3.4中的这三行
18: <xsl:template match="dish">
19: <xsl:value-of select="text()" />
20: </xsl:template>
就可以取得所有dish的值。感觉这一点很重要,但是初学时很容易忽略它。)
Now a new question arises: How do you specifically select another element in the node-set? Fortunately, you can just specify the number of the element you want to select. This, however, deviates from the path notation you are familiar with from Web sites. You need to place the number of the element you want to select between square brackets, [ and ]. Hence, you select the third dish element like this:
<xsl:value-of select="dish[3]" />
NOTE
In many programming languages, a list of elements is numbered from 0, so element number 3 is actually the fourth element in the list. XSLT numbers a list starting with 1, so element number 3 is the third element.
The value between the square brackets is called a predicate, which is a literal or expression that determines whether a certain node should be included in the selection.
Note that the preceding example uses relative addressing, which means that the selection is done based on the current location. If that is the entrees element, the third dish element in the entrees element is selected. If the current node has no child elements named dish, the value is empty.
Because predicates can be expressions, they can become quite complex, testing whether an element conforms to certain criteria. As I said at the beginning of this section, I'll discuss more complex expressions later in this book. The object now is to make you familiar with the building blocks, so let's proceed with an example based on what you've seen so far. The example in Listing 3.8 creates a menu of the day from the sample XML in Listing 3.1.
Listing 3.8 Stylesheet Creating "Today's Menu"
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: Today's Menu7: <xsl:value-of select="/menu/appetizers/dish[2]" />8: <xsl:value-of select="/menu/entrees/dish[3]" />9: <xsl:value-of select="/menu/desserts/dish[1]" />10: </xsl:template>11:12: </xsl:stylesheet>
Listing 3.8 has only one template on line 5, matching the root element. This template displays the values for different elements in Listing 3.1 using absolute addressing and number predicates. Line 7 selects the second dish element in the appetizers element; line 8, the third dish element in the entrees element; and line 9, the first dish element in the desserts element. Listing 3.9 shows the result.
Listing 3.9 Result from Applying Listing 3.8 to Listing 3.1
<?xml version="1.0" encoding="utf-8"?>
Today's Menu Jumbo Prawns Linguini al Pesto Dame Blanche
Listing 3.8 contains only a template matching the root of the XML source. The rest of the stylesheet's functionality utilizes absolute addressing to get the wanted values. As you can see, this yields a list of dishes that form today's menu.
Earlier, you learned about the current node. After a template is fired, a certain node is considered the current node. A path expression doesn't contain a current node, but it does consist of context nodes. A context node is a part of an expression that operates on a certain node.
The predicates used in Listing 3.8 operate on the context node—in this case, the dish node. At one point or another, each part of the path's expression is the context node. It, however, is relevant only when you're working with predicates in a path expression. You can have predicates at several stages within the path expression, in which case the context node is the node that the predicate operates on.
You must realize that if no match occurs, nothing happens. This is also the case if a predicate holds a number for which no element exists. In that case, the number is just ignored. You don't see an error message or anything telling you that an element is missing. The clue is not that an element is missing, but rather that no element matches that particular rule, so the rule is never applied.
注:以上片段所说的predicate在这个阶段暂时还可以理解,不过不敢说理解透彻。遇到问题的时候还得回过头来再学习。 |
|
|