XPath/CSS 等效
:disabled 等效
//*[@disabled] 代表 :disabled
:checked 等效
//*[@checked] 代表 :checked
:selected 等效
//*[@selected] 代表 :selected
:text 等效
//*[@type="text"] 代表 :text
:contains 等效
//*[contains(text(),"you")] 代表 :contains("you")
:only-of-type 查詢
//p[contains(@me,"you")] 代表 p[me*="you"]
以...開頭等效
//p[starts-with(@me,"you")] 代表 p[me^="you"]
包含等效
//p[starts-with(@me,concat("you",'-'))] 代表 p[me|="you"]
以...結尾等效
//p[substring(@me,string-length(@me)-2)="you"] 代表 p[me$="you"]
像等效
//p[contains(concat(" ",@me, " ")," you ")] 代表 p[me~="you"]
否定屬性等效
//p[@me!="you"] 代表 p[me!="you"]
ID 等效
//p[@id="me"] 代表 p#me
:not 等效
//p[not(@id="me")] 代表 p:not(#me)
類等效
//p[contains(concat(" ", @class, " "), " me ")] 代表 p.me
後代等效
//div//p 代表 div p
子節點等效
//div/p 代表 div > p
相鄰兄弟節點等效
//h1/following-sibling::div 代表 h1 + div
一般兄弟節點等效
//h1/following-sibling::*[count(div)] 代表 h1 ~ div
:nth-last-child(n) 查詢
//*[count(child::node() ) > 0] 代表 hasChildNodes
:root 查詢
/*[1] 代表 :root
:first-child 查詢
descendant::*[1] 代表 :first-child
:last-child 查詢
//*[last()] 代表 :last-child
:nth-last-child(n) 查詢
//*[count(*)=1] 代表 :only-child
:empty 查詢
//*[count(*) = 0] 代表 :empty
:nth-child(n) 查詢
//*[position() mod n = 1] 代表 :nth-child(n)
:nth-child(odd) 查詢
//*[(position() mod 2)=1] 代表 :nth-child(odd)
:nth-child(even) 查詢
//*[(position() mod 2)=0] 代表 :nth-child(even)
:nth-last-child(n) 查詢
//*[(count() - position()) mod n = 1] 代表 :nth-last-child(n)
:nth-of-type(n) 查詢
//p[n] 代表 :nth-of-type(n)
:nth-last-of-type(n) 查詢
//p[(count() - position()) mod n = 1] 代表 :nth-last-of-type(n)
:first-of-type 查詢
descendant::p[1] 代表 :first-of-type
:last-of-type 查詢
//p[last()] 代表 :last-of-type
:only-of-type 查詢
//p[count(*)=1] 代表 :only-of-type
-moz-any/-webkit-any 查詢
[local-name()='h1' or local-name()='h4']/node() 代表 -moz-any(h1,h4) *
//mytag 代表 getElementsByTagName
//*[@class=$myclass] 代表 getElementsByClassName
//* 代表 childNodes
preceding-sibling::*[1] 代表 previousSibling
following-sibling::*[1] 代表 nextSibling
./following-sibling::* 代表 generalSibling
- 謂詞就像 SQL 的 where 子句
- 管道就像 SQL 的 union 子句
- 軸就像 SQL 的 t1.col = t2.col
- 使用帶有布林變數檢查的謂詞作為 if 語句
- 使用帶有標籤名搜尋的管道作為範圍檢查器
//h2 | //h3 | //h4
- 使用帶有否定謂詞變數檢查的管道作為 else 語句
//var[1] | //var[not //var]
- 使用重複軸跳過樹中的級別,以檢索每隔分支的節點
child::*/child::*
- 使用變數在複雜和/或條件測試中儲存單個檢查
- 在迴圈中使用變數來儲存依賴於迭代的變數,並將邏輯與輸出分離
- 使用 string-length 測試函式的存在
- 使用單獨的測試,例如 (a and c) 或 (b and c),而不是巢狀條件,例如 ((a or b) and c)
- 使用 string(.), local-name(.),string-length(concat(., '') ), number(.), 和 boolean(.)[boolean(.)] 測試節點值、名稱和存在
- 使用 //node()[local-name(.) = $myvar] 測試表單值是否存在
- 使用 //node()[local-name(.) = $myvar][boolean(node())] 跳過空表單值
- 除錯時始終跳過空節點
- 使用 local-name(.)[boolean(.)] 測試上下文節點中是否存在空標籤
- 使用 boolean(@*[not aa or not bb]) 過濾已知屬性
- 使用 {boolean((string$myvar))} 測試插值變數
- 使用 boolean(following::*[1] or following::. or following::self::*) 測試結束標籤失敗
- 使用 count(//*[1 | last() = 1]) 統計只有一個子節點的節點
- 使用 (table1 | table2)[col=val]/* 進行連線
- 使用 *[not(@*)] 返回沒有屬性的節點
- 使用 number(.) - number(.) 抑制數值
- 使用 substring(., 0, string-length(.) ) 抑制字串值
- 使用 substring('0', 1, not($myvar) ) 將未定義的變數設定為零
- 使用 normalize-whitespace($myvar) 刪除列表中的製表符
- 使用 translate($myvar, $ABCvar, $abcvar) 以及儲存 A-Z 和 a-z 的變數,以忽略節點查詢的大小寫
- 在檢查列表位置時使用 string-length 而不是 contains,使邏輯資料獨立
- 使用 use-attribute-sets 與多個元素共享樣板引數,例如表格或列表行
- 在選擇時使用 * 或 self::* 以返回節點集或單個節點
- 使用 string(.) 從使用 document 函式呼叫的 XML 檔案中獲取節點
- 在每個列表項之間使用不同的分隔符,使 substring-before/after 邏輯更易讀
- 使用節點/類名與計數器/節點值的字串連線來生成 ID 屬性
- 使用變數儲存上一個索引值,並使用逗號使 substring-after 能夠與遞迴一起使用
- 使用帶有節點布林值的謂詞作為保護運算子
smyvar[$myvar]
- 使用 XSL 節點儲存本地值,並使用管道在謂詞中測試 XML 節點,以模擬預設運算子 $dynamic[$var] | $default[not($var)]
- 使用子節點軸代替斜槓來對子節點進行分組
child::(boy | girl)
- 避免使用 (*),因為它在測試子節點之前會遍歷樹
- 使用 node() 而不是 .,當使用 // 搜尋所有元素時
- 使用節點本身的 generate-id 的謂詞檢查和節點變數執行交集和排除集運算
- 使用 concat 連線節點和虛擬引數,以檢查節點是否存在
- 使用 string(number(.))=NaN 檢查數值節點值是否存在
- 在比較包含多個節點的變數時,使用 not($a=$b) 而不是 $a !=$b
- 在括號之後使用換行符,避免留下一個未結束的括號
- 使用謂詞檢查表單欄位名是否在 XML 文件中存在
- 使用 qname 獲取標籤的名稱空間繫結
- 使用 id() 獲取 generate-id 值,而不是變數插值