- eXtensible Stylesheet Language Transformations
- 本身是XSL規範的一部分,專門用於對XML的樹型數據進行重組轉換,並可以以各種格式輸出
示例
//hello.xml
<?xml version="1.0">
<?xml-stylesheet type="text/xsl" href="hello.xslt"?>
<message>hello</message>
//hello.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- transform the input root -->
<xsl:template match="/">
<html>
<body>
<h1>
<xsl:value-of select="message"/>
</h1>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
//output
<html>
<body>
<h1>hello</h1>
</body>
</html>
- XML文檔中告訴解析器可以用一個XSLT來進行轉換
- XSLT文檔,本身其實就是一個XML文檔,但XSLT的根元素必須是“http://www.w3.org/1999/XSL/Transform”命名空間中的stylesheet元素
- 瀏覽器如果打開XML文件,會發現XML可以被一個XSLT規則來轉換,所以就轉換,於是顯示的就是轉換後的內容,即output
- 在XSLT中定義了一個模板,match=”/”匹配的是文檔節點,即作爲整個轉換工作的入口,類似main函數
模板
模板用來匹配XML中的節點並對匹配的節點做轉換
模板的聲明
<xsl:template
match=Pattern name=QName mode=QName priority=Number as=Sequence-type>
<!-- other xsl elements and literal result elements -->
......
</xsl:template>
- match用於選取匹配的節點,是一個相對或絕對的XPath
- name爲模板的名字,可以通過name來調用
- mode可以進一步區分有相同name的模板
- priority用於對可以用於同一個節點的模板間區分優先級,值大優先
- as爲該模板的返回類型,一般無須指定,表示可以輸出任何內容
模板的調用
1、xsl:apply-templates
<xsl:template match="/"> 模板 1
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="message"> 模板 2
.....
</xsl:template>
- xsl:apply-templates的廣度優先調用
- 在入口模板中調用了xsl:apply-templates,表面在文檔節點下廣度優先、逐層向下的遍歷節點
- 模板2採用了相對路徑,意味着一旦遍歷到一個message節點,這個模板就匹配上,就可以調用,對當前的message節點進行處理
語法
<xsl:apply-templates select=Expression mode=QName>
......
</xsl:apply-templates>
- select屬性表示下面的遍歷只遍歷我select的節點,如
<xsl:apply-templates select="message"/>
那麼就只會遍歷所有的message節點並調用匹配該節點的模板來處理
- mode屬性用於匹配模板,對於符合該節點的模板同時還需要滿足相同的mode纔會被調用
2、xsl:call-template
<xsl:call-template name = qname>
<!-- Content: xsl:with-param* -->
</xsl:call-template>
- call-template則是直接調用指定name的模板來處理
內置模板
- 內置模板的作用,就是已經實現好了所有的遍歷過程,而我們只需要寫好相應的模板,內置模板就會在遍歷的時候自動的調用我們寫的模板來處理。
- 對於我們沒有編寫模板來處理的節點,內置模板默認是輸出其內容的
- 如果不希望輸出則一般需要我們寫一個模板去覆蓋內置模板的輸出
XSLT轉換功能
- xsl:value-of 提取文本內容
- 取指爲一個XPath表達式
<xsl:value-of select = expression />
- xsl:text來輸出文本內容
//xml
<message>
<subject>I</subject>
<predicate>Love</predicate>
<object>You</object>
</message>
//xslt1
<xsl:template match="message">
<xsl:value-of select="subject"/>
<xsl:value-of select="predicate"/>
<xsl:value-of select="object"/>
</xsl:template>
//output1
ILoveYou
//xslt2
<xsl:template match="message">
<xsl:value-of select="subject"/><xsl:text> </xsl:text>
<xsl:value-of select="predicate"/><xsl:text> </xsl:text>
<xsl:value-of select="object"/>
</xsl:template>
//output2
I Love You
- xsl:for-each來批量處理
<xsl:for-each select = Expression>
...循環處理代碼...
</xsl:for-each>
- xsl:sort對批量處理的內容進行排序
- 這種批量處理可以是xsl:for-each,也可以是xsl:apply-templates
<xsl:sort select = expression data-type = { "text" | "number" | QName }
order = { "ascending" | "descending" } case-order = { "upper-first" | "lower-first" } />
- xsl:if進行判斷與分支
<xsl:if test = boolean-expression>
...條件滿足時所執行的指令...
</xsl:if>
//示例
<xsl:template match="Name">
<xsl:if test="position()!=last()">
<li><xsl:value-of select="."/></li>
</xsl:if>
</xsl:template>
- xsl:choose、xsl:when、xsl:otherwise進行多分支
<xsl:choose>
<xsl:when test=case1>
...
</xsl:when>
<xsl:when test=case2>
...
</xsl:when>
<xsl:otherwise>
...default...
</xsl:otherwise>
</xsl:choose>
- xsl:copy複製原節點,僅複製當前節點,不復制子元素和屬性
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
- xsl:copy-of複製整個節點
<xsl:template match="/">
<xsl:copy-of select="."/>
</xsl:template>
- xsl:element 生成 XML 元素
<xsl:template match="message">
<xsl:element name=“new-message">
<xsl:vlaue-of select="."/>
</xsl:element>
</xsl:template>
- xsl:attribute生成屬性
<xsl:template match="message">
<xsl:copy>
<xsl:attribute name="content">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:copy>
</xsl:template>