XSLT学习笔记
先看一下w3school上的定義,如下(自己翻譯的,可能會(huì)有一點(diǎn)有妥):
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的標(biāo)準(zhǔn)的一個(gè)主要組成部分(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
學(xué)習(xí)所需的基礎(chǔ)知識(shí):
- HTML / XHTML
- XML / XML Namespaces
什么是XPath?
-
- XPath is a syntax(語法) for defining parts of an XML document
- XPath uses path expressions(路徑表達(dá)式) to navigate(導(dǎo)航,定位) in XML documents
- XPath contains a library of standard functions (包含一些標(biāo)準(zhǔn)函數(shù)庫)
- XPath is a major element in XSLT (XSLT的主要構(gòu)成部份)
- XPath is a W3C Standard (W3C的一個(gè)標(biāo)準(zhǔn))
XPath Path Expressions(路徑表達(dá)式)
XPath uses path expressions to select nodes(節(jié)點(diǎn)) or node-sets(節(jié)點(diǎn)集) 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通過路徑表達(dá)式來選擇XML文檔中的節(jié)點(diǎn)或節(jié)點(diǎn)集,這些表達(dá)式很像那些傳統(tǒng)的電腦文件系統(tǒng)的表達(dá)式.
XPath Standard Functions(標(biāo)準(zhǔn)函數(shù))
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多個(gè)內(nèi)建函數(shù),這些函數(shù)可以返回字符串值,數(shù)值,日期和時(shí)間的比較值,節(jié)點(diǎn)和QName操作,因果關(guān)系操作,布爾值,還有其它等.
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知識(shí)你將不能創(chuàng)建有效的,靈活的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有共同的數(shù)據(jù)模式,支持相同的函數(shù)和操作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術(shù)語
In XPath, there are seven kinds of nodes(七種節(jié)點(diǎn)): element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes.
在 XPath中,共有七種節(jié)點(diǎn):元素,屬性,文本,名字空間,處理指令,注釋和文檔根節(jié)點(diǎn)
看下面這個(gè)例子:
| <?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> |
上例中的節(jié)點(diǎn)
| <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.
原子值是沒有子節(jié)點(diǎn)和父節(jié)點(diǎn)的節(jié)點(diǎn)
如上例中的:
| J K. Rowling "en" |
Items
Items are atomic values or nodes.
Relationship of Nodes節(jié)點(diǎn)間關(guān)系
Parent父
Each element and attribute has one parent.每個(gè)元素和屬性只有一個(gè)父節(jié)點(diǎn)
In the following example; the book element is the parent of the title, author, year, and price:
下例中book元素是title, author, year,?和 price的父節(jié)點(diǎn):
| <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.元素的節(jié)點(diǎn)可以是零個(gè),一個(gè)或多個(gè)
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.具有相同父節(jié)點(diǎn)的節(jié)點(diǎn)
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.一個(gè)節(jié)點(diǎn)的父節(jié)點(diǎn),父節(jié)點(diǎn)的父節(jié)點(diǎn)
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.一個(gè)節(jié)點(diǎn)的子節(jié)點(diǎn),子節(jié)點(diǎn)的子節(jié)點(diǎn)
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使用路徑表達(dá)式在XML文檔中選擇節(jié)點(diǎn)或節(jié)點(diǎn)套,節(jié)點(diǎn)的選擇通過以下方式
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選擇節(jié)點(diǎn)
XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps.
XPath使用路徑表達(dá)式在XML文檔中選擇節(jié)點(diǎn)或節(jié)點(diǎn)套,節(jié)點(diǎn)的選擇通過以下方式
The most useful path expressions are listed below:
最常用的路徑表達(dá)式如下:
| Expression | Description |
| nodename節(jié)點(diǎn)名 | Selects all child nodes of the node 選擇指定節(jié)點(diǎn)的所有子節(jié)點(diǎn) |
| / | Selects from the root node 從根節(jié)點(diǎn)來始選擇 |
| // | Selects nodes in the document from the current node that match the selection no matter where they are 在文檔中從當(dāng)前節(jié)點(diǎn)開始選擇,不管它們?cè)谀睦?#xff0c;只要它們符合條件 |
| . | Selects the current node 選擇當(dāng)前的節(jié)點(diǎn) |
| .. | Selects the parent of the current node 選擇當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn) |
| @ | Selects attributes 選擇屬性 |
Examples
In the table below we have listed some path expressions and the result of the expressions:
在下面的表格里,我們例舉了一些路徑表達(dá)式和它們的結(jié)果
| Path Expression | Result |
| bookstore | Selects all the child nodes of the bookstore element 選擇bookstore元素的所有子節(jié)點(diǎn) |
| /bookstore | Selects the root element bookstore 選擇根節(jié)點(diǎn)bookstore Note: If the path starts with a slash ( / ) it always represents an absolute path to an element! 注:如果路徑以"/"開始,它通常表示一絕對(duì)路徑 |
| 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元素,無論它們?cè)谖臋n的什么地方 |
| bookstore//book | Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element 選擇所有的bookstore的胄book 元素,不管它們?cè)谀?#xff0c;只要在bookstore元素內(nèi) |
| //@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.
謂詞用于查找一個(gè)指定的節(jié)點(diǎn)或一個(gè)包含一個(gè)指定值的節(jié)點(diǎn)
Predicates are always embedded in square brackets.
謂詞通常嵌寫在一對(duì)方括號(hào)內(nèi)
Examples例
In the table below we have listed some path expressions with predicates and the result of the expressions:
在下面的表格中我們例舉了一些使用了謂詞的路徑表達(dá)式和它們的結(jié)果
| Path Expression | Result |
| /bookstore/book[0] | Selects the first book element that is the child of the bookstore element. 選擇bookstore元素的第一個(gè)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]表示第一個(gè)節(jié)點(diǎn),但是根據(jù)W3C的標(biāo)準(zhǔn),應(yīng)該是 [1]表示第一個(gè)節(jié)點(diǎn) |
| /bookstore/book[last()] | Selects the last book element that is the child of the bookstore element 選擇bookstore元素的最后一個(gè)book 元素 |
| /bookstore/book[last()-1] | Selects the last but one book element that is the child of the bookstore element 選擇bookstore元素的倒數(shù)第二個(gè)book 元素 |
| /bookstore/book[position()<3] | Selects the first two book elements that are children of the bookstore element 選擇bookstore元素的前兩個(gè)book 元素 |
| //title[@lang] | Selects all the title elements that have an attribute named lang 選擇所有具有l(wèi)ang屬性的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 匹配任何元素節(jié)點(diǎn) |
| @* | Matches any attribute node 匹配任何屬性節(jié)點(diǎn) |
| node() | Matches any node of any kind 匹配任何種類的節(jié)點(diǎn) |
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元素的子節(jié)點(diǎn) |
| //* | 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表達(dá)式中通過使用"|"操作符可以來選擇幾條路徑
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.
一條主線指定了一系列的節(jié)點(diǎn)和當(dāng)前節(jié)點(diǎn)的關(guān)系
| AxisName | Result |
| ancestor | Selects all ancestors (parent, grandparent, etc.) of the current node 選擇當(dāng)前節(jié)點(diǎn)的所有的祖先,根,(父,父之父) |
| ancestor-or-self | Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself 選擇當(dāng)前節(jié)點(diǎn)所有的祖節(jié)點(diǎn)和自己 |
| attribute | Selects all attributes of the current node 選擇當(dāng)前節(jié)點(diǎn)的所有屬性 |
| child | Selects all children of the current node 選擇當(dāng)前節(jié)點(diǎn)的所有子節(jié)點(diǎn) |
| descendant | Selects all descendants (children, grandchildren, etc.) of the current node 選擇當(dāng)前節(jié)點(diǎn)的所有胄(子,子之子等) |
| descendant-or-self | Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself 選擇當(dāng)前節(jié)點(diǎn)的所有子節(jié)點(diǎn)和自己 |
| following | Selects everything in the document after the closing tag of the current node 選擇當(dāng)前節(jié)點(diǎn)結(jié)束后的文檔中的所有節(jié)點(diǎn) |
| following-sibling | Selects all siblings after the current node 選擇當(dāng)前節(jié)點(diǎn)后的所有同科節(jié)點(diǎn) |
| namespace | Selects all namespace nodes of the current node 選擇當(dāng)前節(jié)點(diǎn)的所有的名字空間 |
| parent | Selects the parent of the current node 選擇當(dāng)前節(jié)點(diǎn)的所有父節(jié)點(diǎn) |
| preceding | Selects everything in the document that is before the start tag of the current node 選擇文檔中當(dāng)前節(jié)點(diǎn)前的所有節(jié)點(diǎn) |
| preceding-sibling | Selects all siblings before the current node 選擇當(dāng)前節(jié)點(diǎn)前的所有同科節(jié)點(diǎn) |
| self | Selects the current node 選擇當(dāng)前節(jié)點(diǎn) |
Location Path Expression位置表達(dá)式
A location path can be absolute or relative.
可以是絕對(duì)的也可以是相對(duì)的確良
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:
一個(gè)絕對(duì)的路徑以"/"開始,兩種表示式都有一級(jí)或多級(jí)構(gòu)成,級(jí)用"/"分開
| An absolute location path: /step/step/... A relative location path: step/step/... |
Each step is evaluated against the nodes in the current node-set.
每一級(jí)都是根據(jù)當(dāng)前節(jié)點(diǎn)的在節(jié)點(diǎn)集的位置來執(zhí)行的
A step consists of:級(jí)構(gòu)成:
- an axis (defines the tree-relationship between the selected nodes and the current node) 主線(規(guī)定一個(gè)當(dāng)前節(jié)點(diǎn)和所先節(jié)點(diǎn)的關(guān)系樹)
- a node-test (identifies a node within an axis) 一個(gè)節(jié)點(diǎn)測(cè)試(在主線中辨認(rèn)一個(gè)節(jié)點(diǎn))
- zero or more predicates (to further refine the selected node-set) 一個(gè)或多個(gè)謂詞(進(jìn)一步挑選選定的節(jié)點(diǎn)集)
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 選擇當(dāng)前節(jié)點(diǎn)的所有的book節(jié)點(diǎn) |
| attribute::lang | Selects the lang attribute of the current node 選擇當(dāng)前節(jié)點(diǎn)的lang 屬性 |
| child::* | Selects all children of the current node 選擇當(dāng)前節(jié)點(diǎn)的所有子節(jié)點(diǎn) |
| attribute::* | Selects all attributes of the current node 選擇當(dāng)前節(jié)點(diǎn)的所有屬性 |
| child::text() | Selects all text child nodes of the current node 選擇當(dāng)前節(jié)點(diǎn)的所有文本子節(jié)點(diǎn) |
| child::node() | Selects all child nodes of the current node 選擇當(dāng)前節(jié)點(diǎn)的所有子節(jié)點(diǎn) |
| descendant::book | Selects all book descendants of the current node 選擇當(dāng)節(jié)點(diǎn)的所有book胄 |
| ancestor::book | Selects all book ancestors of the current node 選擇當(dāng)前節(jié)點(diǎn)的所有book節(jié)點(diǎn)的祖,根 |
| ancestor-or-self::book | Selects all book ancestors of the current node - and the current as well if it is a book node 選擇當(dāng)前節(jié)點(diǎn)的book元素的根節(jié)點(diǎn)或它自己(如果它也是一個(gè)book節(jié)點(diǎn)的話) |
| 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.
一個(gè) XPath 表達(dá)式可以返回一個(gè)節(jié)點(diǎn)集,一個(gè)字符串,一個(gè)布爾值,或一個(gè)數(shù)值
XPath Operators
Below is a list of the operators that can be used in XPath expressions:
下面是一些可以用在XPath表達(dá)式中的操作符的清單:
| Operator | Description | Example | Return value |
| | | Computes two node-sets 返回兩個(gè)節(jié)點(diǎn)集 | //book | //cd | Returns a node-set with all book and cd elements 返回一個(gè)所有book 元素和cd元素的節(jié)點(diǎn)集 |
| + | 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
實(shí)例
Let's try to learn some basic XPath syntax by looking at some examples.
通過實(shí)例學(xué)習(xí)基本的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 點(diǎn)擊查看 .
Selecting Nodes 選擇節(jié)點(diǎn)
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對(duì)象來加載XML文檔和selectNodes() 函數(shù)來從XML文檔中選擇節(jié)點(diǎn)
| set xmlDoc=CreateObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load("books.xml") xmlDoc.selectNodes(path expression) |
Select all book Nodes選擇所有的book節(jié)點(diǎn)
The following example selects all the book nodes under the bookstore element:
下例選擇bookstore元素的所有的book 節(jié)點(diǎn)
| xmlDoc.selectNodes("/bookstore/book") |
If you have IE 5 or higher you can try it yourself.
Select the First book Node選擇第一個(gè)book節(jié)點(diǎn)
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標(biāo)準(zhǔn)為使用[1]來選擇第一個(gè)
A Workaround!
To solve the [0] and [1] problem in IE5+, you can set the SelectionLanguage to XPath.
為了解決這個(gè)問題,你可以通過為XPath設(shè)置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">
重要標(biāo)簽解析
<xsl:template match="xpath">?? ?? ?? ?? 該標(biāo)簽用于定義模版,同時(shí)分配給指定結(jié)點(diǎn)
<xsl:apply-templates select="xpath">??? 該標(biāo)簽用于指定要應(yīng)用模版的結(jié)點(diǎn)
提示: xsl:template中可以再次使用xsl:apply-templates,用于樣式的多級(jí)嵌套
實(shí)例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> 以上例子中,先對(duì)所有結(jié)點(diǎn)使用<xsl:apply-templates>,然后再使用<xsl:template>對(duì)planet結(jié)點(diǎn)作處理
<xsl:attribute>??? 可為html標(biāo)簽添加屬性
實(shí) 例2:<a><xsl:attribute name="href" select="http://www.cnblogs.com"></a>該語句生成的結(jié)果為<a href="http://www.cnblogs.com"></a>
<xsl:value-of select="xpath">?? 獲得結(jié)點(diǎn)的值
語法結(jié)構(gòu)的使用
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.模版函數(shù)定義
<xsl:template?name=”template?name”>
<xsl:param?name=”parameter1”/>
<xsl:param?name="parameter2"?select="defaultvalue"/>
</xsl:template> 其中,parameter2使用select屬性指定了默認(rèn)值defaultvalue。
對(duì)于模版函數(shù)中的參數(shù)可以用$variable來引用
實(shí)例3
<xsl:call-template?name="template1">
<xsl:with-param?name="parameter1"/>
<xsl:value-of?select="$parameter1"/>
</xsl:call-template> 5.模版函數(shù)調(diào)用
<xsl:call-template?name="template?name">
<xsl:with-param?name="parameter1"?select="parameter?value"/>
...
</xsl:call-template>
在IE中使用xslt的注意點(diǎn)
- 在xml中引用xslt時(shí),必須把type=”text/xml”改為type=”text/xsl”
- 必須先匹配根結(jié)點(diǎn)后,再匹配其他結(jié)點(diǎn),否則可能無法顯示,即match=”/”
- IE瀏覽器不支持任何XSLT默認(rèn)規(guī)則,因此必須自己寫
如果要在<script language="javascript"></script>中添加代碼,必須使用<![CDATA[...]]>,因?yàn)樘砑拥拇a沒有人任何標(biāo)記,會(huì)使xslt文件不符合xml格式規(guī)范
轉(zhuǎn)載于:https://www.cnblogs.com/Dragon-China/archive/2007/05/29/764102.html
總結(jié)
- 上一篇: 个人的web开发心得(八)-------
- 下一篇: web标准不标准