這篇“XML基本概念XPath、XSLT與XQuery函數怎么使用”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“XML基本概念XPath、XSLT與XQuery函數怎么使用”文章吧。
XSL指擴展樣式表語言(EXtensible Stylesheet Language)。
官方網站:https://www.w3.org/TR/xpath/
XSL - 不僅僅是樣式表語言,包括三部分:
XSLT :一種用于轉換 XML 文檔的語言。
XPath :一種用于在 XML 文檔中導航的語言。
XSL-FO :一種用于格式化 XML 文檔的語言。
XPath
XML DOM使用XPath解析HTML。
HTMLAgilityPack使用XPath解析HTML (JumonyHTML采用CSS3方式解析HTML)。
XPath 使用路徑表達式在 XML 文檔中選取節點。節點是通過沿著路徑或者 step 來選取的。
下面列出了最有用的路徑表達式:
nodename:選取此節點的所有子節點。
/:從根節點選取。
//:選擇文檔中的節點,而不考慮它們的位置。
. :選取當前節點。
.. :選取當前節點的父節點。
@ :選取屬性。
實例:
bookstore : 選取 bookstore 元素的所有子節點。
/bookstore:選取根元素 bookstore。
注釋:假如路徑起始于正斜杠( / ),則此路徑始終代表到某元素的絕對路徑!
bookstore/book:選取屬于 bookstore 的子元素的所有 book 元素。
//book:選取所有 book 子元素,而不管它們在文檔中的位置。
bookstore//book:選擇屬于 bookstore 元素的后代的所有 book 元素,而不管它們位于 bookstore 之下的什么位置。
//@lang:選取名為 lang 的所有屬性。
謂語用來查找某個特定的節點或者包含某個指定的值的節點。謂語被嵌在方括號中。
實例:
/bookstore/book[1] : 選取屬于 bookstore 子元素的第一個 book 元素。
/bookstore/book[last()] : 選取屬于 bookstore 子元素的最后一個 book 元素。
/bookstore/book[last()-1] : 選取屬于 bookstore 子元素的倒數第二個 book 元素。
/bookstore/book[position()<3] : 選取最前面的兩個屬于 bookstore 元素的子元素的 book 元素。
//title[@lang] : 選取所有擁有名為 lang 的屬性的 title 元素。
//title[@lang='eng'] : 選取所有 title 元素,且這些元素擁有值為 eng 的 lang 屬性。
/bookstore/book[price>35.00] : 選取 bookstore 元素的所有 book 元素,且其中的 price 元素的值須大于 35.00。
/bookstore/book[price>35.00] /title: 選取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值須大于 35.00。
book[count(author)=2] : 有兩個author元素的Book
book/*[starts-with(name(),’B’)] : 以B開頭的并在book下的元素。
book/*[contains(name(),’B’)] : 包含’B’名稱的元素
book/*[string-length(name())==3] : 元素的名稱長度為3
選擇含有某子元素的元素://table[.//img]
XPath 通配符可用來選取未知的 XML 元素。
* : 匹配任何元素節點。
@* : 匹配任何屬性節點。
node() : 匹配任何類型的節點。
實例:
/bookstore/*: 選取 bookstore 元素的所有子元素。
//*: 選取文檔中的所有元素。
//title[@*]: 選取所有帶有屬性的 title 元素。
通過在路徑表達式中使用 "|" 運算符,您可以選取若干個路徑。
實例:
//book/title | //book/price : 選取 book 元素的所有 title 和 price 元素。
//title | //price : 選取文檔中的所有 title 和 price 元素。
/bookstore/book/title | //price : 選取屬于 bookstore 元素的 book 元素的所有 title 元素,以及文檔中所有的 price 元素。
軸可定義相對于當前節點的節點集。
ancestor : 選取當前節點的所有先輩(父、祖父等)。
ancestor-or-self: 選取當前節點的所有先輩(父、祖父等)以及當前節點本身。
attribute : 選取當前節點的所有屬性。
child: 選取當前節點的所有子元素。
descendant: 選取當前節點的所有后代元素(子、孫等)。
descendant-or-self: 選取當前節點的所有后代元素(子、孫等)以及當前節點本身。
following: 選取文檔中當前節點的結束標簽之后的所有節點。
namespace: 選取當前節點的所有命名空間節點。
parent: 選取當前節點的父節點。
preceding: 選取文檔中當前節點的開始標簽之前的所有節點。
preceding-sibling: 選取當前節點之前的所有同級節點。
self: 選取當前節點。
步的語法:軸名稱::節點測試[謂語]
實例:
child::book: 選取所有屬于當前節點的子元素的 book 節點。
attribute::lang: 選取當前節點的 lang 屬性。
child::*: 選取當前節點的所有子元素。
attribute::*: 選取當前節點的所有屬性。
child::text(): 選取當前節點的所有文本子節點。
child::node(): 選取當前節點的所有子節點。
descendant::book: 選取當前節點的所有 book 后代。
ancestor::book: 選擇當前節點的所有 book 先輩。
ancestor-or-self::book: 選取當前節點的所有 book 先輩以及當前節點(如果此節點是 book 節點)
child:: * /child::price: 選取當前節點的所有 price 孫節點。
下面列出了可用在 XPath 表達式中的運算符:
| : 計算兩個節點集
+: 加法
-: 減法
*: 乘法
div: 除法
=: 等于
!=: 不等于
< : 小于
<=: 小于或等于
> : 大于
>=: 大于或等于
or: 或
and: 與
mod: 計算除法的余數
指 XSL 轉換。在此教程中,你將學習如何使用 XSLT 將 XML 文檔轉換為其他文檔,比如 XHTML。
XSLT 使用 XPath 在 XML 文檔中進行導航。
把文檔聲明為 XSL 樣式表的根元素是 或 。
注釋: 和 是完全同義的,均可被使用!
根據 W3C 的 XSLT 標準,聲明 XSL 樣式表的正確方法是:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
或者:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
如需訪問 XSLT 的元素、屬性以及特性,我們必須在文檔頂端聲明 XSLT 命名空間。
然后創建一個帶有轉換模板的 XSL 樣式表("cdcatalog.xsl"):
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h3>My CD Collection</h3> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th align="left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
向 XML 文檔("cdcatalog.xml")添加 XSL 樣式表引用:
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl" rel="external nofollow" ?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> </cd> </catalog>
如果您使用的瀏覽器兼容 XSLT,它會很順利地把您的 XML 轉換為 XHTML。(注意本地文件沒效果,要放到Web服務器上才能轉換)
1、 元素
XSL 樣式表由一個或多套被稱為模板(template)的規則組成。
每個模板含有當某個指定的節點被匹配時所應用的規則。
XSL 樣式表由一個或多套被稱為模板(template)的規則組成。
每個模板含有當某個指定的節點被匹配時所應用的規則。
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h3>My CD Collection</h3> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <tr> <td>.</td> <td>.</td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet>
元素定義了一個模板。而 match="/" 屬性則把此模板與 XML 源文檔的根相聯系。
元素內部的內容定義了寫到輸出結果的 HTML 代碼。
2、 元素
元素用于提取某個選定節點的值,并把值添加到轉換的輸出流中:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h3>My CD Collection</h3> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <tr> <td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet>
注釋:select 屬性的值是一個 XPath 表達式。此表達式的工作方式類似于定位某個文件系統,在其中正斜杠可選擇子目錄。
這個例子的結果有一點缺陷:僅有一行數據從 XML 文檔被拷貝到輸出結果。
可以使用 元素來循環遍歷 XML 元素,并顯示所有的記錄。
3、 元素
元素可用于選取指定的節點集中的每個 XML 元素。
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h3>My CD Collection</h3> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
結果過濾
通過在 元素中添加一個選擇屬性的判別式,我們也可以過濾從 XML 文件輸出的結果。
<xsl:for-each select="catalog/cd[artist='Bob Dylan']">
合法的過濾運算符:
= (等于)
!= (不等于)
< (小于)
> (大于)
4、 元素
元素用于對結果進行排序。
如需對結果進行排序,只要簡單地在 XSL 文件中的 元素內部添加一個 元素:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h3>My CD Collection</h3> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:sort select="artist"/> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
5、 元素
元素用于放置針對 XML 文件內容的條件測試。
如需添加有條件的測試,請在 XSL 文件中的 元素內部添加 元素:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h3>My CD Collection</h3> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:if test="price > 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
6、 元素
元素用于結合 和 來表達多重條件測試??梢园鄠€ 元素
要插入針對 XML 文件的多重條件測試,請向 XSL 文件添加 、 以及 :
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h3>My CD Collection</h3> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price > 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="artist"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
7、 元素
元素可把一個模板應用于當前的元素或者當前元素的子節點。
假如我們向 元素添加一個 select 屬性,此元素就會僅僅處理與屬性值匹配的子元素。我們可以使用 select 屬性來規定子節點被處理的順序。
請看下面的 XSL 樣式表:
<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h3>My CD Collection</h3> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="cd"> <p> <xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/> </p> </xsl:template> <xsl:template match="title"> Title: <span > <xsl:value-of select="."/> </span> <br /> </xsl:template> <xsl:template match="artist"> Artist: <span > <xsl:value-of select="."/> </span> <br /> </xsl:template> </xsl:stylesheet>
8、 元素
元素用于聲明局部或全局的變量。
注釋:如果被聲明為頂層元素,則該變量是全局的,而如果在模板內聲明,則變量是本地的。 一旦您設置了變量的值,就無法改變或修改該值!
提示:您可以通過 元素的內容或通過 select 屬性,向變量添加值!
下面的例子通過 元素的內容為變量 "header" 賦值:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:variable name="header"> <tr> <th>Element</th> <th>Description</th> </tr> </xsl:variable> <xsl:template match="/"> <html> <body> <table> <xsl:copy-of select="$header" /> <xsl:for-each select="reference/record"> <tr> <xsl:if category="XML"> <td><xsl:value-of select="element"/></td> <td><xsl:value-of select="description"/></td> </xsl:if> </tr> </xsl:for-each> </table> <br /> <table> <xsl:copy-of select="$header" /> <xsl:for-each select="table/record"> <tr> <xsl:if category="XSL"> <td><xsl:value-of select="element"/></td> <td><xsl:value-of select="description"/></td> </xsl:if> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
XQuery 相對于 XML的作用,等同于 SQL 相對于數據庫。
XQuery 被設計用來查詢 XML 數據。 不僅僅限于 XML 文件,還包括任何可以 XML 形態呈現的數據,包括數據庫。
SQLServer XML類型使用基于XPath的XQuery。
XQuery 也被稱為 XML Query。
XQuery 對大小寫敏感
XQuery 的元素、屬性以及變量必須是合法的 XML 名稱。
XQuery 字符串值可使用單引號或雙引號。
XQuery 變量由 “$” 并跟隨一個名稱來進行定義,舉例,$bookstore
XQuery 注釋被 (: 和 :) 分割,例如,(: XQuery 注釋 :)
FLWOR 是 "For, Let, Where, Order by, Return" 的只取首字母縮寫。
for 語句把 bookstore 元素下的所有 book 元素提取到名為 $x 的變量中。
where 語句選取了 price 元素值大于 30 的 book 元素。
order by 語句定義了排序次序。將根據 title 元素進行排序。
return 語句規定返回什么內容。在此返回的是 title 元素。
下面這個路徑表達式可選取 bookstore 元素下的 book 元素下所有的 title 元素,并且其中的 price 元素的值必須大于 30。:
doc("books.xml")/bookstore/book[price>30]/title --doc() 用于打開 "books.xml" 文件:
下面這個 FLWOR 表達式所選取的數據和上面的路徑表達式是相同的:
for $x in doc("books.xml")/bookstore/book where $x/price>30 return $x/title
上面的 XQuery 表達式結果是:
<title lang="en">XQuery Kick Start</title> <title lang="en">Learning XML</title>
通過 FLWOR,您可以對結果進行排序:
for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title
上面的 XQuery 表達式的結果:
<title lang="en">Learning XML</title> <title lang="en">XQuery Kick Start</title>
以上就是關于“XML基本概念XPath、XSLT與XQuery函數怎么使用”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。