XSL - 銘傳大學

Download Report

Transcript XSL - 銘傳大學

了解XSL與XML的關係
 了解XSL基本語法與要素


什麼是XSL?
› XSL「可延伸排版樣本語言」(eXtensible
Stylesheet Language)
 作為定義XML文件轉換樣式的一種語言
 利用XSL轉換格式的能力可以將XML專換成各種開放
的格式(如XML, HTML等)

在XML文件中宣告使用XSL
› <?xml-stylesheet type="text/xsl" href=“XSL檔案位址" ?>

XSL是由一組轉換模板(Transformation
Template)所組成
<?xml version=“1.0?>
<?xml-stylesheet type="text/xsl"
href ="test.xsl" ?>
<booklist>
<book>
<title>Old Man</title>
<author>F. H. Wang</author>
</book>
<book>
<title>Goodness</title>
<author>C. T. Lee</author>
</book>
</booklist>
XML
文件
XSLT
處理器
XSL
文件
<xsl:stylesheet version = “1.0” xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<h1>
<xsl:value-of select="//title"/>
</h1>
轉換模板
<h2>
<xsl:value-of select="//author"/>
</h2>
</xsl:template>
</xsl:stylesheet>
轉換
結果
(HTML)
(只抓到第一組?)
<h1>Old Man</h1>
<h2>F. H. Wang</h2>
XSL程式本身也是XML文件
 由一堆轉換規則所組成(甚至沒有任何規則也
可以)

› 每一條規則利用XPATH指令指定規則的啟動節點
› 規則內部則指定轉換的動作, 或針對其他節點(也
是用XPATH指令描述)啟動相關規則
<xsl:template match="xpath" >
<h1>Test XSL</h1>
<xsl:apply-templates select=“xpath2“ />
</xsl:template>

XSL程式的執行是資料(節點)導向
XML DOM Tree
XSL 程式
XSL
Processor
Action
輸出結果
Rule 1: Action1
Rule 2: Action2
Rule 3: Action3
…
Rule N: ActionN
<?xml version=“1.0”?>
<sports>
<game title = “baseball”>
A popular game in Taiwan!
</game>
</sports>
/
sports
<xsl:template match=“sports" >
<h1>
<xsl:apply-templates select=“game“ />
</h1>
</xsl:template>
<xsl:template match=“game" >
<p>
<xsl:value-of select=“@title“ /> is
<xsl:value-of select=“.“ />
</p>
</xsl:template>
title=“baseball”
<h1>….. </h1>
game
A popular
game in Taiwan
XSL
Processor
<p>baseball is
A popular game in
Taiwan </p>

xsl:stylesheet
 XSL文件的根元素
 <xsl:stylesheet>....</xsl:stylesheet>

xsl:template
 <xsl:template match=”xpath” >
 利用「match」設定xpath 指令設定本規則的啟動節點

xsl:apply-templates
 <xsl:apply-templates select=“xpath” />
 在xpath指定的節點上逐一套用適當的模板

xsl:value-of
 <xsl:value-of select= “xpath”/>
 取出xpath定位到的第一個節點的值
<?xml version=“1.0”?>
<sports>
<game title = “baseball”>
A popular game in Taiwan!
</game>
</sports>
<xsl:stylesheet version=“1.0”
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
<!-- 沒有任何規則 -->
</xsl:stylesheet >
/
sports
title=“baseball”
game
A popular
A popular
game in Taiwan
game in Taiwan
XSL
Processor
A popular game in
Taiwan

<xsl:template match=“/ | *”>
<xsl:apply-templates />
</xsl:template>
 此模板比對document node 以及element node,然後對其
孩子節點繼續套用適當的模板

<xsl:template match=“text() | @*”>
<xsl:value-of select=“.” />
</xsl:template>
 此模板比對任何的文字節點 以及屬性節點,然後顯示該節點的
值

<xsl:template match=“processing-instruction()|
comment()” />
 此模板比對任何的PI節點 以及註解節點,然後不作任何處理(也
就是忽略這些節點)

新增元素
› 在輸出文件中建立一個元素標籤
› <xsl:element name=”新增元素名稱”>...</xsl:element>

建立屬性
› 在輸出文件的元素內插入屬性
› 運用在一個元素標籤內或上述的<xsl:element>之內
› <xsl:attribute name=”屬性名稱">屬性的值<xsl:attribute>

取得屬性
› 括弧 { } 加「@」
› 如
<xsl:element name=“{@title}”> … </xsl:element>
<?xml version=“1.0”?>
<sports>
<game title = “baseball”>
A popular game in Taiwan!
</game>
</sports>
<xsl:template match=“game" >
<xsl:element name=“{@title}”>
<xsl:attribute name=“des“ >
<xsl:value-of select=“.“ />
</xsl:attribute>
我是新增的元素
</xsl:element>
</xsl:template>
/
sports
<baseball
des=“A popular …”>
我是新增的元素
</baseball>
title=“baseball”
game
A popular
game in Taiwan
XSL
Processor

唯一條件判斷
› <xsl:if test=”判斷條件”>...</xsl:if>

多重條件判斷
<xsl:choose>
<xsl:when test=”條件”>...</xsl:when>
.....
<xsl:otherwise>...</otherwise>
</xsl:choose>
<?xml version=“1.0”?>
<sports>
<game title = “baseball”>
A popular game in Taiwan!
</game>
</sports>
<xsl:template match=“game" >
<xsl:if test=“@title=‘baseball’“ >
<font color=“red”>
<xsl:value-of select=“@title“ /> is
<xsl:value-of select=“.“ />
</font>
</xsl:if>
</xsl:template>
/
sports
<font color=“red”>
baseball is A popular
game in Taiwan
</font>
title=“baseball”
game
A popular
game in Taiwan
XSL
Processor

xsl:for-each
› <xsl:for-each select=“xpath” >
…(裡面的參考節點是xpath所定位的節點)
</xsl:for-each>
› <xsl:sort order= “ascending |
descending” select=“xpath2” />
 將for-each的結果依照某種xpath2順序來依序
處理(遞增或遞減)
<?xml version=“1.0”?>
<sports>
<game title = “baseball”>
A popular game in Taiwan!
</game>
<game title = “tennisl”>
A less popular game in Taiwan!
</game>
</sports>
<xsl:template match=“sports" >
<xsl:for-each select=“game” >
<xsl:sort order= “descending” select=“@title” />
<p>
<xsl:value-of select=“@title“ /> is
<xsl:value-of select=“.“ />
</p>
</xsl:for-each>
</xsl:template>
XSL
Processor
<p>
tennis is A less popular
game in Taiwan
</p>
<p>
baseball is A popular
game in Taiwan
</p>

xsl:comment
 <xsl:comment>comment</xsl:comment>
 輸出備註資料

xsl:copy
 <xsl:copy>…</xsl:copy>
 將目前處理的節點輸出
 (不包括子節點以及屬性節點)

xsl:copy-of
 <xsl:copy-of select=“xpath” />
 將xpath所定位的XML節點輸出
 包括其子孫節點以及屬性節點
<?xml version=“1.0”?>
<sports>
<game title = “baseball”>
A popular game in Taiwan!
</game>
</sports>
<xsl:template match=“sports" >
<xsl:copy >
<p>
<xsl:value-of select=“game/@title“ /> is
<xsl:value-of select=“game“ />
</p>
</xsl:copy>
</xsl:template>
XSL
Processor
<sports>
<p>
baseball is A popular
game in Taiwan
</p>
</sports>
<?xml version=“1.0”?>
<sports>
<game title = “baseball”>
A popular game in Taiwan!
</game>
</sports>
XSL
Processor
<xsl:template match=“sports" >
<xsl:copy-of select=“game” />
</xsl:template>
<game title = “baseball”>
A popular game in Taiwan!
</game>
XSL可作為XML文件的格式轉換器
 介紹XSL基本語法與要素

› XSL程式的架構與執行概念
› XSL轉換規則的撰寫
› 各種轉換動作
 新增元素,屬性
 條件控制
 迴圈控制
本投影片謹簡述若干基本XSL元素,同學仍須參考課本或講義更多細節