目錄
Xsl解析xml檔案... 1
1.XSL 指擴充樣式表語言(EXtensible Stylesheet Language);1
2.XSLT 指 XSL 轉換。在此教程中,你将學習如何使用 XSLT 将 XML 文檔轉換為其他文檔,比如 XHTML。1
3.xsl解析xml檔案——Eg1:... 2
(方法;寫一個和xsl建立起關系的xml檔案以及xsl檔案)... 2
4.xsl解析xml檔案——Eg2:... 3
5. 幾個XSLT重要要素... 3
文法... 3
Xsl解析xml檔案
1.XSL 指擴充樣式表語言(EXtensible Stylesheet Language);
網際網路聯盟 (W3C) 開始發展 XSL 的原因是:存在着對于基于 XML 的樣式表語言的需求。
2.XSLT 指 XSL 轉換。在此教程中,你将學習如何使用 XSLT 将 XML 文檔轉換為其他文檔,比如 XHTML。
CSS = HTML 樣式表
HTML 使用預先定義的标簽,标簽的意義很容易被了解。
HTML 元素中的 <table> 元素定義表格 - 并且浏覽器清楚如何顯示它。
向 HTML 元素添加樣式是很容易的。通過 CSS,很容易告知浏覽器用特定的字型或顔色顯示一個元素。
XSL = XML 樣式表
XML 不使用預先定義的标簽(我們可以使用任何喜歡的标簽名),并且這些标簽的意義并不都那麼容易被了解。
<table> 元素意味着一個 HTML 表格,一件家具,或是别的什麼東西 - 浏覽器不清楚如何顯示它。
XSL 可描述如何來顯示 XML 文檔!
XSL - 不僅僅是樣式表語言XSL 包括三部分:
XSLT
一種用于轉換 XML 文檔的語言。
XPath
一種用于在 XML 文檔中導航的語言。
XSL-FO
一種用于格式化 XML 文檔的語言。
Xsl大緻相當于html中的css,來定義xml如何顯示;
3.xsl解析xml檔案——Eg1:
我們現在要把下面這個 XML 文檔("cdcatalog.xml")轉換為 XHTML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country><?xml:namespace prefix="st1">?xml:namespace>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>
方法;寫一個和xsl建立起關系的xml檔案以及xsl檔案
檔案1:cdcatalog.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>
檔案2: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="/"> //注釋:match="/"屬性則把此模闆與 XML 源文檔的根相聯系。
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd"> //注解:利用xslt的for-each 元素将XML中元素周遊輸出
//注釋:添加查找條件一般在此位置
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
//注釋:select屬性的值是一個 XPath 表達式。此表達式的工作方式類似于定位某個檔案系統,在其中正斜杠可選擇子目錄。
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
4.xsl解析xml檔案——Eg2:
我們需要寫一個xml檔案并将其顯示出來出如下結果來:
張老師的學生 | |
小張 | 小王 |
檔案一:ts.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="ts.xsl"?>
<teacher>
<name>小張</name>
<name>小王</name>
</teacher>
檔案二:ts.xml
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table width="328" border="2" bgcolor="yellow">
<tr>
<th width="133">張老師的學生:</th>
</tr>
<xsl:for-each select="teacher/name">
<td><xsl:value-of select="."/></td>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
5. 幾個XSLT重要要素
<xsl:template> 元素
<xsl:template> 元素用于構模組化闆。XSL 樣式表由一個或多套被稱為模闆(template)的規則組成。每個模闆含有當某個指定的節點被比對時所應用的規則。
<xsl:value-of> 元素
<xsl:value-of select=” ”>元素用于提取某個標明節點的值,并把值添加到轉換的輸出流中
select屬性的值是一個 XPath 表達式。此表達式的工作方式類似于定位某個檔案系統,在其中正斜杠可選擇子目錄。
<xsl:for-each> 元素
<xsl:for-each> 元素允許您在 XSLT 中進行循環。可用于選取指定的節點集中的每個 XML 元素
通過在 <xsl:for-each> 元素中添加一個選擇屬性的判别式,我們也可以過濾從 XML 檔案輸出的結果。
<xsl:for-each select="catalog/cd [artist='Bob Dylan'] ">
合法的過濾運算符:
- = (等于)
- != (不等于)
- < (小于)
- > (大于)
<xsl:sort> 元素
<xsl:sort> 元素用于對結果進行排序。
<?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>
<h2>My CD Collection</h2>
<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>
//
注釋
:
select屬性訓示需要排序的 XML 元素。
<xsl:if> 元素
<xsl:if> 元素用于放置針對 XML 檔案内容的條件測試。
文法
<xsl:if test="expression">
...
...如果條件成立則輸出...
...
</xsl:if>
在何處放置 <xsl:if> 元素
如需添加有條件的測試,請在 XSL 檔案中的 <xsl:for-each> 元素内部添加 <xsl:if> 元素:
<?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>
<h2>My CD Collection</h2>
<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>
注釋:必選的test屬性的值包含了需要求值的表達式。
上面的代碼僅僅會輸出價格高于 10 的 CD 的 title 和 artist 元素。
<xsl:choose> 元素
<xsl:choose> 元素用于結合 <xsl:when> 和 <xsl:otherwise> 來表達多重條件測試。
<xsl:choose>
<xsl:when test="expression"> //條件表達式
... 輸出 ...
</xsl:when>
<xsl:otherwise> //條件不等輸出
... 輸出 ....
</xsl:otherwise>
</xsl:choose>
在何處放置選擇條件
要插入針對 XML 檔案的多重條件測試,請向 XSL 檔案添加 <xsl:choose>、<xsl:when> 以及 <xsl:otherwise>:
<?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>
<h2>My CD Collection</h2>
<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>
上面的代碼會在 CD 的價格高于 10 時向 "Artist" 列添加粉色的背景顔色。
<xsl:apply-templates> 元素
<xsl:apply-templates> 元素可把一個模闆應用于目前的元素或者目前元素的子節點。
<?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>
<h2>My CD Collection</h2>
<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 style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>