XSLT学习笔记
先看一下w3school上的定義,如下(自己翻譯的,可能會有一點有妥):
XPath是一種可以在XML文檔中尋找信息的語言
(XPath is a language for finding information in an XML document. )
XPath是可以查找XML文檔中的元素和屬性
(XPath is used to navigate through elements and attributes in an XML document.)
XPath是W3C的XSLT的標準的一個主要組成部分(XPath is a major element in the W3C's XSLT standard )
并且XQuery和XPointer都是基于XPath而建立起來的(and XQuery and XPointer are both built on XPath expressions. )
參見:http://www.w3schools.com/xpath/default.asp
學習所需的基礎知識:
- HTML / XHTML
- XML / XML Namespaces
什么是XPath?
-
- XPath is a syntax(語法) for defining parts of an XML document
- XPath uses path expressions(路徑表達式) to navigate(導航,定位) in XML documents
- XPath contains a library of standard functions (包含一些標準函數庫)
- XPath is a major element in XSLT (XSLT的主要構成部份)
- XPath is a W3C Standard (W3C的一個標準)
XPath Path Expressions(路徑表達式)
XPath uses path expressions to select nodes(節點) or node-sets(節點集) in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system.
XPath通過路徑表達式來選擇XML文檔中的節點或節點集,這些表達式很像那些傳統的電腦文件系統的表達式.
XPath Standard Functions(標準函數)
XPath includes over 100 built-in functions. There are functions for string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, and more.
XPath 包含100多個內建函數,這些函數可以返回字符串值,數值,日期和時間的比較值,節點和QName操作,因果關系操作,布爾值,還有其它等.
XPath is Used in XSLT
XPath is a major element in the XSLT standard. Without XPath knowledge you will not be able to create XSLT documents.
沒有XPath知識你將不能創建有效的,靈活的XSLT文檔
XQuery and XPointer are both built on XPath expressions. XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators.
XQuery 1.0?和 XPath 2.0有共同的數據模式,支持相同的函數和操作XPath is a W3C Standard
XPath became a W3C Recommendation(推薦,提議) 16. November 1999.
XPath was designed to be used by XSLT, XPointer and other XML parsing(分析,解析) software.
XPath Terminology術語
In XPath, there are seven kinds of nodes(七種節點): element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes.
在 XPath中,共有七種節點:元素,屬性,文本,名字空間,處理指令,注釋和文檔根節點
看下面這個例子:
| <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> ??<title lang="en">Harry Potter</title> ??<author>J K. Rowling</author> ??<year>2005</year> ??<price>29.99</price> </book> </bookstore> |
上例中的節點
| <bookstore>??(document node) <author>J K. Rowling</author>??(element node) lang="en"??(attribute node) |
Atomic values(原子值)
Atomic values are nodes with no children or parent.
原子值是沒有子節點和父節點的節點
如上例中的:
| J K. Rowling "en" |
Items
Items are atomic values or nodes.
Relationship of Nodes節點間關系
Parent父
Each element and attribute has one parent.每個元素和屬性只有一個父節點
In the following example; the book element is the parent of the title, author, year, and price:
下例中book元素是title, author, year,?和 price的父節點:
| <book> ??<title>Harry Potter</title> ??<author>J K. Rowling</author> ??<year>2005</year> ??<price>29.99</price> </book> |
Children子
Element nodes may have zero, one or more children.元素的節點可以是零個,一個或多個
In the following example; the title, author, year, and price elements are all children of the book element:
| <book> ??<title>Harry Potter</title> ??<author>J K. Rowling</author> ??<year>2005</year> ??<price>29.99</price> </book> |
Siblings同胞,同科,平行
Nodes that have the same parent.具有相同父節點的節點
In the following example; the title, author, year, and price elements are all siblings:下例中,title, author, year,?和 price 元素都是同科
| <book> ??<title>Harry Potter</title> ??<author>J K. Rowling</author> ??<year>2005</year> ??<price>29.99</price> </book> |
Ancestors祖先,根
A node's parent, parent's parent, etc.一個節點的父節點,父節點的父節點
In the following example; the ancestors of the title element are the book element and the bookstore element:
下例中,title元素的根是book和bookstore元素
| <bookstore> <book> ??<title>Harry Potter</title> ??<author>J K. Rowling</author> ??<year>2005</year> ??<price>29.99</price> </book> </bookstore> |
Descendants胄
A node's children, children's children, etc.一個節點的子節點,子節點的子節點
In the following example; descendants of the bookstore element are the book, title, author, year, and price elements:
下例中,bookstore元素的胄是book,title,author,year和price元素
| <bookstore> <book> ??<title>Harry Potter</title> ??<author>J K. Rowling</author> ??<year>2005</year> ??<price>29.99</price> </book> </bookstore> |
XPath Syntax 語法
XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps.
XPath使用路徑表達式在XML文檔中選擇節點或節點套,節點的選擇通過以下方式
The XML Example Document
XML例子
We will use the following XML document in the examples below.我們用下面的XML文檔來做為范例
| <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> ??<title lang="eng">Harry Potter</title> ??<price>29.99</price> </book> <book> ??<title lang="eng">Learning XML</title> ??<price>39.95</price> </book> </bookstore> |
Selecting Nodes選擇節點
XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps.
XPath使用路徑表達式在XML文檔中選擇節點或節點套,節點的選擇通過以下方式
The most useful path expressions are listed below:
最常用的路徑表達式如下:
| Expression | Description |
| nodename節點名 | Selects all child nodes of the node 選擇指定節點的所有子節點 |
| / | Selects from the root node 從根節點來始選擇 |
| // | Selects nodes in the document from the current node that match the selection no matter where they are 在文檔中從當前節點開始選擇,不管它們在哪里,只要它們符合條件 |
| . | Selects the current node 選擇當前的節點 |
| .. | Selects the parent of the current node 選擇當前節點的父節點 |
| @ | Selects attributes 選擇屬性 |
Examples
In the table below we have listed some path expressions and the result of the expressions:
在下面的表格里,我們例舉了一些路徑表達式和它們的結果
| Path Expression | Result |
| bookstore | Selects all the child nodes of the bookstore element 選擇bookstore元素的所有子節點 |
| /bookstore | Selects the root element bookstore 選擇根節點bookstore Note: If the path starts with a slash ( / ) it always represents an absolute path to an element! 注:如果路徑以"/"開始,它通常表示一絕對路徑 |
| bookstore/book | Selects all? book確良elements that are children of bookstore 選擇bookstore所有的book元素 |
| //book | Selects all book elements no matter where they are in the document 選擇所有的book元素,無論它們在文檔的什么地方 |
| bookstore//book | Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element 選擇所有的bookstore的胄book 元素,不管它們在哪,只要在bookstore元素內 |
| //@lang | Selects all attributes that are named lang 選擇所有的名字為lang的屬性 |
Predicates謂詞
?
Predicates are used to find a specific node or a node that contains a specific value.
謂詞用于查找一個指定的節點或一個包含一個指定值的節點
Predicates are always embedded in square brackets.
謂詞通常嵌寫在一對方括號內
Examples例
In the table below we have listed some path expressions with predicates and the result of the expressions:
在下面的表格中我們例舉了一些使用了謂詞的路徑表達式和它們的結果
| Path Expression | Result |
| /bookstore/book[0] | Selects the first book element that is the child of the bookstore element. 選擇bookstore元素的第一個book 元素 Note: IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!! 注: IE5 和更高的版本指明[0]表示第一個節點,但是根據W3C的標準,應該是 [1]表示第一個節點 |
| /bookstore/book[last()] | Selects the last book element that is the child of the bookstore element 選擇bookstore元素的最后一個book 元素 |
| /bookstore/book[last()-1] | Selects the last but one book element that is the child of the bookstore element 選擇bookstore元素的倒數第二個book 元素 |
| /bookstore/book[position()<3] | Selects the first two book elements that are children of the bookstore element 選擇bookstore元素的前兩個book 元素 |
| //title[@lang] | Selects all the title elements that have an attribute named lang 選擇所有具有lang屬性的title元素 |
| //title[@lang='eng'] | Selects all the title elements that have an attribute named lang with a value of 'eng' 選擇所有的,lang屬性值為'eng'的 title 元素 |
| /bookstore/book[price>35.00] | Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00 選擇所有了bookstore?的 book 元素,且book 元素的price 元素的值大于35.00 |
| /bookstore/book[price>35.00]/title | Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00 選擇所有的bookstore元素的 book 的 title元素,且book 元素的price 元素的值大于35.00 |
Selecting Unknown Nodes
XPath wildcards can be used to select unknown XML elements.
| Wildcard | Description |
| * | Matches any element node 匹配任何元素節點 |
| @* | Matches any attribute node 匹配任何屬性節點 |
| node() | Matches any node of any kind 匹配任何種類的節點 |
Examples
In the table below we have listed some path expressions and the result of the expressions:
| Path Expression | Result |
| /bookstore/* | Selects all the child nodes of the bookstore element 選擇所有的bookstore元素的子節點 |
| //* | Selects all elements in the document 選擇文檔中所有的元素 |
| //title[@*] | Selects all title elements which have any attribute 選擇的有屬性的title元素 |
Selecting Several Paths選擇幾條路徑
By using the | operator in an XPath expression you can select several paths.
在XPath表達式中通過使用"|"操作符可以來選擇幾條路徑
Examples
In the table below we have listed some path expressions and the result of the expressions:
| Path Expression | Result |
| //book/title | //book/price | Selects all the title AND price elements of all book elements 選擇book元素的所有title 和price 元素 |
| //title | //price | Selects all the title AND price elements in the document 選擇文檔中所有的title 和price 元素 |
| /bookstore/book/title | //price | Selects all the title elements of the book element of the bookstore element AND all the price elements in the document 選擇bookstore元素的book元素的所有title和 文檔中所有的price 元素 |
XPath Axes軸,軸線,軸心
XPath的主線
The XML Example Document
We will use the following XML document in the examples below.
| <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> ??<title lang="eng">Harry Potter</title> ??<price>29.99</price> </book> <book> ??<title lang="eng">Learning XML</title> ??<price>39.95</price> </book> </bookstore> |
XPath Axes
An axis defines a node-set relative to the current node.
一條主線指定了一系列的節點和當前節點的關系
| AxisName | Result |
| ancestor | Selects all ancestors (parent, grandparent, etc.) of the current node 選擇當前節點的所有的祖先,根,(父,父之父) |
| ancestor-or-self | Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself 選擇當前節點所有的祖節點和自己 |
| attribute | Selects all attributes of the current node 選擇當前節點的所有屬性 |
| child | Selects all children of the current node 選擇當前節點的所有子節點 |
| descendant | Selects all descendants (children, grandchildren, etc.) of the current node 選擇當前節點的所有胄(子,子之子等) |
| descendant-or-self | Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself 選擇當前節點的所有子節點和自己 |
| following | Selects everything in the document after the closing tag of the current node 選擇當前節點結束后的文檔中的所有節點 |
| following-sibling | Selects all siblings after the current node 選擇當前節點后的所有同科節點 |
| namespace | Selects all namespace nodes of the current node 選擇當前節點的所有的名字空間 |
| parent | Selects the parent of the current node 選擇當前節點的所有父節點 |
| preceding | Selects everything in the document that is before the start tag of the current node 選擇文檔中當前節點前的所有節點 |
| preceding-sibling | Selects all siblings before the current node 選擇當前節點前的所有同科節點 |
| self | Selects the current node 選擇當前節點 |
Location Path Expression位置表達式
A location path can be absolute or relative.
可以是絕對的也可以是相對的確良
An absolute location path starts with a slash ( / ) and a relative location path does not. In both cases the location path consists of one or more steps, each separated by a slash:
一個絕對的路徑以"/"開始,兩種表示式都有一級或多級構成,級用"/"分開
| An absolute location path: /step/step/... A relative location path: step/step/... |
Each step is evaluated against the nodes in the current node-set.
每一級都是根據當前節點的在節點集的位置來執行的
A step consists of:級構成:
- an axis (defines the tree-relationship between the selected nodes and the current node) 主線(規定一個當前節點和所先節點的關系樹)
- a node-test (identifies a node within an axis) 一個節點測試(在主線中辨認一個節點)
- zero or more predicates (to further refine the selected node-set) 一個或多個謂詞(進一步挑選選定的節點集)
The syntax for a location step is:語法
| axisname::nodetest[predicate] |
Examples
| Example | Result |
| child::book | Selects all book nodes that are children of the current node 選擇當前節點的所有的book節點 |
| attribute::lang | Selects the lang attribute of the current node 選擇當前節點的lang 屬性 |
| child::* | Selects all children of the current node 選擇當前節點的所有子節點 |
| attribute::* | Selects all attributes of the current node 選擇當前節點的所有屬性 |
| child::text() | Selects all text child nodes of the current node 選擇當前節點的所有文本子節點 |
| child::node() | Selects all child nodes of the current node 選擇當前節點的所有子節點 |
| descendant::book | Selects all book descendants of the current node 選擇當節點的所有book胄 |
| ancestor::book | Selects all book ancestors of the current node 選擇當前節點的所有book節點的祖,根 |
| ancestor-or-self::book | Selects all book ancestors of the current node - and the current as well if it is a book node 選擇當前節點的book元素的根節點或它自己(如果它也是一個book節點的話) |
| child::*/child::price | Selects all price grandchildren of the current node |
XPath Operators操作符
An XPath expression returns either a node-set, a string, a Boolean, or a number.
一個 XPath 表達式可以返回一個節點集,一個字符串,一個布爾值,或一個數值
XPath Operators
Below is a list of the operators that can be used in XPath expressions:
下面是一些可以用在XPath表達式中的操作符的清單:
| Operator | Description | Example | Return value |
| | | Computes two node-sets 返回兩個節點集 | //book | //cd | Returns a node-set with all book and cd elements 返回一個所有book 元素和cd元素的節點集 |
| + | Addition加 | 6 + 4 | 10 |
| - | Subtraction減 | 6 - 4 | 2 |
| * | Multiplication乘 | 6 * 4 | 24 |
| div | Division除 | 8 div 4 | 2 |
| = | Equal等于 | price=9.80 | true if price is 9.80 false if price is 9.90 |
| != | Not equal不等于 | price!=9.80 | true if price is 9.90 false if price is 9.80 |
| < | Less than小于 | price<9.80 | true if price is 9.00 false if price is 9.80 |
| <= | Less than or equal to小于等于 | price<=9.80 | true if price is 9.00 false if price is 9.90 |
| > | Greater than大于 | price>9.80 | true if price is 9.90 false if price is 9.80 |
| >= | Greater than or equal to大于等于 | price>=9.80 | true if price is 9.90 false if price is 9.70 |
| or | or或 | price=9.80 or price=9.70 | true if price is 9.80 false if price is 9.50 |
| and | and 且 | price>9.00 and price<9.90 | true if price is 9.80 false if price is 8.50 |
| mod | Modulus (division remainder)取余 | 5 mod 2 | 1 |
XPath Examples
實例
Let's try to learn some basic XPath syntax by looking at some examples.
通過實例學習基本的xpath語法
The XML Example Document
We will use the following XML document in the examples below.
使用下面的xml文檔來做為范例
"books.xml":
| <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> |
View the "books.xml" file in your browser 點擊查看 .
Selecting Nodes 選擇節點
We will use the Microsoft XMLDOM object to load the XML document and the selectNodes() function to select nodes from the XML document:
我們通過使用Microsoft? 的XMLDOM對象來加載XML文檔和selectNodes() 函數來從XML文檔中選擇節點
| set xmlDoc=CreateObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load("books.xml") xmlDoc.selectNodes(path expression) |
Select all book Nodes選擇所有的book節點
The following example selects all the book nodes under the bookstore element:
下例選擇bookstore元素的所有的book 節點
| xmlDoc.selectNodes("/bookstore/book") |
If you have IE 5 or higher you can try it yourself.
Select the First book Node選擇第一個book節點
The following example selects only the first book node under the bookstore element:
| xmlDoc.selectNodes("/bookstore/book[0]") |
If you have IE 5 or higher you can try it yourself
Note: IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!!
注:W3C標準為使用[1]來選擇第一個
A Workaround!
To solve the [0] and [1] problem in IE5+, you can set the SelectionLanguage to XPath.
為了解決這個問題,你可以通過為XPath設置SelectionLanguage
The following example selects only the first book node under the bookstore element:
| xmlDoc.setProperty "SelectionLanguage", "XPath" xmlDoc.selectNodes("/bookstore/book[1]") |
Try it yourself
Select the prices
選擇price元素
The following example selects the text from all the price nodes:
| xmlDoc.selectNodes("/bookstore/book/price/text()") |
If you have IE 5 or higher you can try it yourself.
Selecting price Nodes with Price>35
選擇price值大于35的元素
The following example selects all the price nodes with a price higher than 35:
| xmlDoc.selectNodes("/bookstore/book[price>35]/price") |
If you have IE 5 or higher you can try it yourself.
Selecting title Nodes with Price>35
選擇值price大于35的title元素
The following example selects all the title nodes?with a price higher than 35:
xmlDoc.selectNodes("/bookstore/book[price>35]/title")XSLT全稱eXtended Stylesheet Language Transformation
xslt文件頭
<?xml?version="1.0"?encoding="utf-8"?>
<xsl:stylesheet?version="1.0"?xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
重要標簽解析
<xsl:template match="xpath">?? ?? ?? ?? 該標簽用于定義模版,同時分配給指定結點
<xsl:apply-templates select="xpath">??? 該標簽用于指定要應用模版的結點
提示: xsl:template中可以再次使用xsl:apply-templates,用于樣式的多級嵌套
實例1:
planets.xml
<?xml?version="1.0"?encoding="utf-8"?>
<?xml-stylesheet?type="text/xsl"?href="planets.xslt"?>
<planets>
????<planet?color="red">
????????<name>Mercury</name>
????????<mass?units="(Earth=1)">.0553</mass>
????????<day?units="days">58.65</day>
????????<radius?units="miles">1516</radius>
????????<density?units="(Earth=1)">.983</density>
????????<distance?units="million?miles">43.4</distance>
????</planet>
????<planet?color="yellow">
????????<name>Venus</name>
????????<mass?units="(Earth=1)">.815</mass>
????????<day?units="days">116.75</day>
????????<radius?units="miles">3716</radius>
????????<density?units="(Earth=1)">.943</density>
????????<distance?units="million?miles">66.8</distance>
????</planet>
</planets>
planets.xslt
<?xml?version="1.0"?encoding="utf-8"?>
<xsl:stylesheet?version="1.0"?xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template?match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template?match="planet">
<p><xsl:value-of?select="name"/></p>
</xsl:template> 以上例子中,先對所有結點使用<xsl:apply-templates>,然后再使用<xsl:template>對planet結點作處理
<xsl:attribute>??? 可為html標簽添加屬性
實 例2:<a><xsl:attribute name="href" select="http://www.cnblogs.com"></a>該語句生成的結果為<a href="http://www.cnblogs.com"></a>
<xsl:value-of select="xpath">?? 獲得結點的值
語法結構的使用
1. 類似于if(){...}else{}的語法
<xsl:if?test="expression/condition">
</xsl:if>
?
2. 類似于switch(){case n: ...}的語法<xsl:choose>
<xsl:when?test="condition"></xsl:when>
<xsl:when?test="condition"></xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose> 3.foreach語法
<xsl:for-each?select="node1">
</xsl:for-each> 4.模版函數定義
<xsl:template?name=”template?name”>
<xsl:param?name=”parameter1”/>
<xsl:param?name="parameter2"?select="defaultvalue"/>
</xsl:template> 其中,parameter2使用select屬性指定了默認值defaultvalue。
對于模版函數中的參數可以用$variable來引用
實例3
<xsl:call-template?name="template1">
<xsl:with-param?name="parameter1"/>
<xsl:value-of?select="$parameter1"/>
</xsl:call-template> 5.模版函數調用
<xsl:call-template?name="template?name">
<xsl:with-param?name="parameter1"?select="parameter?value"/>
...
</xsl:call-template>
在IE中使用xslt的注意點
- 在xml中引用xslt時,必須把type=”text/xml”改為type=”text/xsl”
- 必須先匹配根結點后,再匹配其他結點,否則可能無法顯示,即match=”/”
- IE瀏覽器不支持任何XSLT默認規則,因此必須自己寫
如果要在<script language="javascript"></script>中添加代碼,必須使用<![CDATA[...]]>,因為添加的代碼沒有人任何標記,會使xslt文件不符合xml格式規范
轉載于:https://www.cnblogs.com/Dragon-China/archive/2007/05/29/764102.html
總結
- 上一篇: 个人的web开发心得(八)-------
- 下一篇: 用户 'sa' 登录失败。原因: 未与信