XML - 資料交換管理/C.6
外觀
現在您已經知道如何建立模式來定義概念上的主鍵/外部索引鍵關係,您需要知道如何訪問和顯示這些資料。檢視下面的樣式表以獲取示例。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<!-- See note below about new elements added -->
<xsl:key name="predecessor" match="president" use="@primaryKey"/>
<!--xsl:key name="name referenced from within XSL file" match="element in XML file containing key"
use="attribute or sub-element containing actual value of key "-->
<!--key() function use: key('predecessor',2) will return the nodes contained
by the president element with attribute: primaryKey="2"
Since a node list is returned, <xsl:value-of select="key('predecessor',2)/firstName"/>
will output John -->
<xsl:output method="html"/>
<xsl:template match="/">
<table style="font-family:arial">
<tr style="background-color:#6495ED;font-weight:bold">
<td>President First Name</td>
<td>President Last Name</td>
<td>Predecessor First Name</td>
<td>Predecessor Last Name</td>
</tr>
<xsl:for-each select="//president">
<tr style="background-color:#3CB371">
<td>
<xsl:value-of select="firstName"/>
</td>
<td>
<xsl:value-of select="lastName"/>
</td>
<xsl:choose>
<!--XSL equivolent to a programmatic if-else structure-->
<xsl:when test="predecessor">
<!--Test to see if there is a predecessor-->
<td>
<xsl:value-of select="key('predecessor',predecessor/@foreignKey)/firstName"/>
</td>
<td>
<xsl:value-of select="key('predecessor',predecessor/@foreignKey)/lastName"/>
</td>
</xsl:when>
<xsl:otherwise>
<!--Equivolent to else-->
<td colspan="2" style="text-align:center">
This is the first president!
</td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
|
表 6-3:總統實體的 XML 樣式表 - president.xsl
- <xsl:key> 此元素是必要的宣告,以便使用下面描述的 key() 函式。此元素需要三個必要的屬性
- name: 此屬性包含在 XSL 檔案中的 key() 函式中引用的名稱。name 屬性中的值可以在程式設計術語中被認為是包含物件例項的變數,例如:在 Java 中的“String stringVariable”。
- match: 此屬性類似於上面描述的 .<xsd:selector> 元素。這是 XML 檔案中包含父鍵(關係的主鍵)的元素。
- use: 此屬性類似於上面描述的 <xsd:field> 元素。這是包含父鍵值的欄位。
- key(string,object) 函式:此函式根據傳遞給它的引數返回節點集。string 引數是在 <xsl:key> 標籤的 name 屬性中定義的值。object 引數是您希望獲取節點的鍵值的主鍵。key('predecessor',2) 將返回包含主鍵為“2”的總統元素的節點。由於返回的是節點集,因此您可以使用 XPath 表示式訪問返回集的子節點,就像您訪問任何元素一樣。因此:<xsl:value-of select="key('predecessor',2)/firstName "/> 將輸出“John”。有關 key 函式的更多資訊,請訪問:http://www.w3.org/TR/xslt#key
- <xsl:choose> 此 XSL 元素提供了一種在 XSL 樣式表中建立 if-else 型別結構的方法。這在我們的示例中是必要的,因為第一個總統將沒有前任。我們只在存在前任的情況下顯示前任!<xsl:choose> 元素中有兩個元素可以執行此操作
- <xsl:when> 此元素相當於程式設計中的 'if' 語句。test 屬性包含第 5 章中描述的測試語句。在上面的示例中,它只是測試前任元素是否存在(如果總統有前任)。
- <xsl:otherwise> 此元素相當於程式設計中的 'else' 語句。如果 <xsl:when> 語句中的測試條件失敗,則顯示其內容。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:key name="squadCaptain" match="player" use="@playerId"/>
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:for-each select="//team">
<table>
<tr style="background-color:#6495ED;font-weight:bold">
<td colspan="3">
Team Id: <xsl:value-of select="@teamId"/><br />
Team Name: <xsl:value-of select="teamName"/><br />
Team Type: <xsl:value-of select="teamType"/>
</td>
</tr>
<tr style="background-color:#F0E68C;font-weight:bold">
<td>Player Id</td>
<td>Player Name</td>
<td>Player Captain</td>
</tr>
<xsl:for-each select="player">
<tr style="background-color:#D3D3D3">
<td><xsl:value-of select="@playerId"/></td>
<td><xsl:value-of select="firstName"/>&space;
<xsl:value-of select="lastName"/></td>
<xsl:choose>
<xsl:when test="squadCaptain">
<td>
<xsl:value-of select="key('squadCaptain',squadCaptain/@fk_playerId)/firstName"/>
&space;
<xsl:value-of select="key ('squadCaptain',squadCaptain/@fk_playerId)/lastName"/> </td>
</xsl:when>
<xsl:otherwise>
<td style="background-color:#FF0000;font-weight:bold">Squad Captain</td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
<br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
|
表 6-4:團隊實體的 XML 樣式表 - team.xsl
- <!doctype stylesheet[….]> - 此宣告是宣告實體所必需的,必須出現在 <xsl:stylesheet> 標籤之前
- <!ENTITY space "<xsl:text xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> </xsl:text>"> {2}- 此宣告必須出現在上面顯示的 doctype 宣告中的方括號“[]”之間。“space” 是您要定義的實體的名稱。雙引號之間的值是實體所代表的內容。在上面的示例中,實體 space 代表一個空格,用 <xsl:text> </xsl:text> 語句表示。位於 <xsl:text> 元素中的 xmlns 屬性僅適用於使用 Microsoft XML 解析器的解析器,但應為了跨平臺相容性而包含在內。使用此實體的語法與在 XSL 樣式表中使用任何其他實體的語法相同,即一個“&”符號,後跟實體名稱和一個分號。在上面的示例中,這將是 &space;,如上所示。現在,只要樣式表中需要空格,就可以使用“&space;”代替 <xsl:text> </xsl:text>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:key name="subProduct" match="accessory" use="@accessoryId"/>
<xsl:output method="html"/>
<xsl:template match="/">
<table>
<tr style="background-color:#6495ED;font-weight:bold">
<td>Accessory/Package</td>
<td>Accessory/Package Price</td>
<td>Subproduct(s)</td>
</tr>
<xsl:for-each select="//accessory">
<tr style="background-color:#D3D3D3;vertical-align:top;">
<td>
<xsl:value-of select="accessoryName"/>
</td>
<td>
<xsl:value-of select="format-number(accessoryPrice,'$###,###.00')"/>
</td>
<xsl:choose>
<xsl:when test="subProduct ">
<td>
<table>
<tr style="background-color:#F0E68C;font-weight:bold">
<td>Accessory Name</td>
<td>Accessory Price</td>
</tr>
<xsl:for-each select="subProduct">
<tr style="background-color:#7B68EE">
<td><xsl:value-of select="key('subProduct',@fk_SubProductId)/accessoryName"/></td>
<td><xsl:value-of select="format-number_
(key ('subProduct',@fk_SubProductId)/accessoryPrice,'$###,###.00')"/></td>
</tr>
</xsl:for-each>
</table>
</td>
</xsl:when>
<xsl:otherwise>
<td>N/A</td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
|
表 6-9:附件實體的 XML 樣式表 - accessory.xsl