設定作業 (XPath)
XML 路徑語言 (XPath) 支援設定作業|.
聯集 (|) 運算子
此 | 或聯集,運算子可傳回其兩個運算元的聯集 (必須是節點集)。 例如,//author | //publisher 可傳回節點集,其中結合了所有 //author 節點和所有 //publisher 節點。 可一起鏈結多個聯集運算子以結合多個節點集。 例如,//author | //publisher | //editor | //book-seller 傳回包含所有 //author、//publisher,//editor 和 //book-seller elements 的節點集。 聯集運算子可保留文件順序且不會傳回重複項。
範例
運算式 |
代表意義 |
---|---|
first-name | last-name |
包含目前內容中之 <first-name> 和 <last-name> 項目的節點集。 |
(bookstore/book | bookstore/magazine) |
包含 <bookstore> 項目內之 <book> 或 <magazine> 項目的節點集。 |
book | book/author |
包含 <book> 項目內中之所有 <book> 項目和所有 <author> 項目的節點集。 |
(book | magazine)/price |
包含 <book> 或 <magazine> 項目之所有 <price> 項目的節點集。 |
範例
下列範例說明聯集運算子的效果。
XML 檔 (test.xml)
<?xml version="1.0"?>
<test>
<x a="1">
<x a="2" b="B">
<x>
<y>y31</y>
<y>y32</y>
</x>
</x>
</x>
</test>
XSLT 檔 (test.xsl)
下列 XSLT 樣式表可選取其 a 屬性等於 2 的所有 <x> 項目,以及沒有屬性的 <x> 項目。
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<!-- Suppress text nodes not covered in subsequent template rule. -->
<xsl:template match="text()"/>
<!-- Handles a generic element node. -->
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:apply-templates select="*|@*" />
<xsl:if test="text()">
<xsl:value-of select="."/>
</xsl:if>
</xsl:element>
</xsl:template>
<!-- Handles a generic attribute node. -->
<xsl:template match="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="/test">
<xsl:apply-templates select="//x[@a=2] | //x[not(@*)]"/>
</xsl:template>
</xsl:stylesheet>
轉換會產生下列結果:
<x a="2" b="B">
<x>
<y>31</y>
<y>y32</y>
</x>
</x>
<x>
<y>y31</y>
<y>y32</y>
</x>
優先順序
下表顯示了布林值運算子與比較運算子的優先順序 (從最高優先順序到最低優先順序)。
優先順序 |
運算子 |
描述 |
---|---|---|
1 |
( ) |
群組 |
2 |
[ ] |
篩選條件 |
3 |
/ // |
路徑運算 |
4 |
< <= > >= |
比較 |
5 |
= != |
比較 |
6 |
| |
聯集 |
7 |
not() |
布林值 not |
8 |
and |
布林值 and |
9 |
or |
布林值 or |
範例
下列範例說明以上列示的運算子優先順序效果。
XML 檔 (test.xml)
<?xml version="1.0"?>
<test>
<x a="1">
<x a="2" b="B">
<x>
<y>y31</y>
<y>y32</y>
</x>
</x>
</x>
<x a="1">
<x a="2">
<y>y21</y>
<y>y22</y>
</x>
</x>
<x a="1">
<y>y11</y>
<y>y12</y>
</x>
<x>
<y>y03</y>
<y>y04</y>
</x>
</test>
基本 XSLT 檔 (test.xsl)
我們將從使用這個基本 XSLT 檔開始,說明接下來所有的案例。
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<!-- Suppress text nodes not covered in subsequent template rule. -->
<xsl:template match="text()"/>
<!-- Handles a generic element node. -->
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:apply-templates select="*|@*" />
<xsl:if test="text()">
<xsl:value-of select="."/>
</xsl:if>
</xsl:element>
</xsl:template>
<!-- Handles a generic attribute node. -->
<xsl:template match="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
案例 0。測試回合
您可以將下列範本規則加入 XSLT 樣式表。
<xsl:template match="/test">
<xsl:apply-templates select="*|@*/>
</xsl:template>
這個動作會產生與原始文件相同的 XML 文件,而不必使用 <?xml version="1.0"?> 處理指令。
下列案例顯示這個範本規則的不同撰寫方法。 重點是顯示 XPath 運算子繫結至項目的順序。
案例 1:() 繫結緊密度高於 []
下列範本規則會從來源文件的所有 <y> 項目中,依照文件的順序選取第一個 <y> 項目。
<xsl:template match="/test">
<xsl:apply-templates select="(//y)[1]"/>
</xsl:template>
結果如下所示:
<y>y31</y>
案例 2: [] 繫結緊密度高於 / 或 //
下列範本規則會選取位於其同層級之第一個項目的所有 <y> 項目。
<xsl:template match="/test">
<xsl:apply-templates select="//y[1]"/>
</xsl:template>
結果如下所示:
<y>y31</y>
<y>y21</y>
<y>y11</y>
<y>y03</y>
案例 3:and、not
下列範本規則會選取沒有 <x> 子項目、具有 <x> 父項目和沒有任何屬性的所有 <x> 項目。
<xsl:template match="/test">
<xsl:apply-templates select=
"//x[./ancestor::*[name()='x'] and *[name()!='x'] and not(@*)]"/>
</xsl:template>
結果是含有子系的單一 <x> 項目,如下所示:
<x>
<y>y31</y>
<y>y32</y>
</x>
案例 4:or、and、not
下列範本規則會選取做為 <x> 項目子系,或者不是 <x> 項目之父代且沒有屬性的每一個 <x> 項目。
<xsl:template match="/test">
<xsl:apply-templates select=
"//x[./ancestor::*[name()='x'] or *[name()!='x'] and not(@*)]"/>
</xsl:template>
結果是含有下列 <x> 項目及其子系的節點集,如下所示:
<x a="2" b="B">
<x>
<y>y31</y>
<y>y32</y>
</x>
</x>
<x>
<y>y31</y>
<y>y32</y>
</x>
<x a="2">
<y>y21</y>
<y>y22</y>
</x>
<x>
<y>y03</y>
<y>y04</y>
</x>
案例 5:and、or、not
下列範本規則會選取做為 <x> 項目子系,但不是 <x> 項目之父代,或者沒有屬性的每個 <x> 項目。
<xsl:template match="/test">
<xsl:apply-templates select=
"//x[./ancestor::*[name()='x'] and *[name()!='x'] or not(@*)]"/>
</xsl:template>
結果是含有下列 <x> 項目及其子系的節點集,如下所示:
<x>
<y>y31</y>
<y>y32</y>
</x>
<x a="2">
<y>y21</y>
<y>y22</y>
</x>
<x>
<y>y03</y>
<y>y04</y>
</x>