« | 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 访问次数:151354 建立时间:2008年8月8日 |

| |
[xmlStudy]UnderstandingXPath04 文章收藏
半路和尚 发表于 2008/10/10 12:58:03 |
Beyond the Basics
Until now, the discussion has targeted single nodes wherever possible. In fact, the focus has been on how to avoid selections that yield more than one node. Although this information is very useful to get you started, it really limits your capabilities. Without creating complex expressions, you can already perform many tasks with some of the basic functionality XPath provides.
Using a Wildcard
Wildcard characters are common in most search-oriented functions and languages. XPath has only one wildcard character: *. You can use it only to match entire names of elements or attributes, so the expression a* does not match all elements starting with the letter a. This expression generates an error instead. Wildcards are useful when you want to drill deeper into the source XML, and the names of certain nodes (particularly parent nodes) don't matter. Listing 3.14 shows how to use a wildcard.
Listing 3.14 Stylesheet Using a Wildcard
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="/menu/*/dish">6: -<xsl:value-of select="text()" />7: </xsl:template>8:9: </xsl:stylesheet>
Line 5 in Listing 3.14 uses a wildcard character, so it doesn't matter whether the matched dish element is a child element of the appetizers, entrees, or desserts element. Line 6 just shows the value of the matched dish element. Listing 3.15 shows the result.
Listing 3.15 Result from Applying Listing 3.14 to Listing 3.1
<?xml version="1.0" encoding="utf-8"?>
Crab Cakes
Jumbo Prawns
Smoked Salmon and Avocado Quesadilla
Caesar Salad
Grilled Salmon
Seafood Pasta
Linguini al Pesto
Rack of Lamb
Ribs and Wings
Dame Blanche
Chocolat Mousse
Banana Split
In Listing 3.15, the result yields all the dish nodes, not just those that are child nodes of a particular node.
You can use this technique in all kinds of situations. Say that you've created a whitepaper or book using an XML document. Using a wildcard, you can select all the chapter and section headers to create a table of contents. If you don't want to get all the nodes, using just the wildcard doesn't solve your problem. However, just as with the path expressions you saw earlier, you can use predicates to refine the expression. That way, you have more control over what the wildcard actually matches. A simple example is shown in Listing 3.16.
Listing 3.16 Stylesheet Using Wildcards and Predicates
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/*[3]/dish[2]" />8: Price: <xsl:value-of select="/menu/*[3]/dish[2]/@*[2]" />9: </xsl:template>10:11: </xsl:stylesheet>
Listing 3.16 yields the same result as Listing 3.12; the result is shown in Listing 3.13. Instead of addressing nodes by name, line 8 in Listing 3.16 uses several wildcards aided by position predicates. The desserts element is the third child element of the menu element. The /menu/*[3] section of the path expression tells the processor to take the third child element of the menu element, with no regard to the name of that element. That expression yields the desserts element, just as if it had been named. The attribute chosen is also based on a wildcard. In this case, the expression tells the processor to take the second attribute of the dish element, which is, of course, the price attribute.
注:Listing 3.16中的以下两行
7: <xsl:value-of select="/menu/*[3]/dish[2]" />8: Price: <xsl:value-of select="/menu/*[3]/dish[2]/@*[2]" />
和Listing 3.12中的以下两行
7: <xsl:value-of select="/menu/desserts/dish[2]" />8: Price: <xsl:value-of select="/menu/desserts/dish[2]/@price" />
所产生的实际效果一样。
结论:|直接用名称标示一个节点的路径| 和 |采用通配符加谓词说明的方式来标示一个节点的路径| , 效果是一样的。
|
|
|