單元 5-3 : XSL 範例練習

32
單單 5-3 XSL 單單單單 單單單 單單單單單單單

description

單元 5-3 : XSL 範例練習. 王豐緒 銘傳大學資工系. 單元目標. 練習 XSL 程式的撰寫 了解 XSL 的轉換意義. XML 文件中宣告 XSL. 在 XML 文件中宣告 XSL < ? xml- stylesheet type=”text/ xsl ” href =“ 檔案位址 ” ? >. < ? xml version=“1.0” ? > < ? xml- stylesheet type=“text/ xsl ” href =“test.xsl” ? > - PowerPoint PPT Presentation

Transcript of 單元 5-3 : XSL 範例練習

Page 1: 單元 5-3 : XSL 範例練習

單元 5-3 :XSL 範例練習

王豐緒銘傳大學資工系

Page 2: 單元 5-3 : XSL 範例練習

單元目標

練習 XSL 程式的撰寫› 了解 XSL 的轉換意義

Page 3: 單元 5-3 : XSL 範例練習

XML 文件中宣告 XSL

在 XML 文件中宣告 XSL› <?xml-stylesheet type=”text/xsl” href=“ 檔案位

址” ?>

<?xml version=“1.0” ?><?xml-stylesheet type=“text/xsl” href=“test.xsl” ?><booklist> <book> <author>fhwang</author> <title>XML Programming</title> <chatpers>10</chapters> </book></booklist>

Page 4: 單元 5-3 : XSL 範例練習

如何撰寫 XSL 程式

務必先了解 XSL 的運作原理 了解題目所給 XML 文件的結構 了解所欲轉換成的結果 ( 如 HTML 網頁 ) 分析 XML 與轉換結果的對應關係 撰寫 XSL 的對應轉換規則 測試 - 除錯 - 修改 - 直到完成

Page 5: 單元 5-3 : XSL 範例練習

以下的範例修改自 : http://www.zvon.org/xxl/XSLTutorial/Output/contents.html

Page 6: 單元 5-3 : XSL 範例練習

XSL 範例 1

<source>

<title>XML</title> <author>FHWANG</author>

</source>

<h1>XML</h1><h2>FHWANG</h2>

<h2>FHWANG</h2><h1>XML</h1>

Xsl1?

Xsl2?

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> ????</xsl:stylesheet>

<xsl:template match=“source”> <h1><xsl:value-of select=“title” /></h1> <h2><xsl:value-of select=“author” /></h2></xsl:template>

<xsl:template match=“source”> <h1><xsl:value-of select=“title” /></h1> <h2><xsl:value-of select=“author” /></h2></xsl:template>

Page 7: 單元 5-3 : XSL 範例練習

XSL 範例 2

<source>

<em>Hello, My Students</em>

</source>

????

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

</xsl:stylesheet>

Hello, My Students

Page 8: 單元 5-3 : XSL 範例練習

XSL 範例 3 (apply-templates)

<source> <employee> <firstName>大同</firstName> <surname>李</surname> </employee> </source>

???

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="employee"> <b> <xsl:value-of select="."/> </b> </xsl:template>

<xsl:template match="surname"> <i> <xsl:value-of select="."/> </i> </xsl:template></xsl:stylesheet>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="employee"> <b> <xsl:apply-templates select="firstName"/> </b> <b> <xsl:apply-templates select="surname"/> </b> </xsl:template>

<xsl:template match="surname"> <i> <xsl:value-of select="."/> </i> </xsl:template></xsl:stylesheet>

???

<b> 大同李 </b>

<b> 大同 </b><b><i> 李 </i></b>

Page 9: 單元 5-3 : XSL 範例練習

XSL 範例 4

<source>

<bold>Hello, My Students.</bold> <red>I am </red> <italic>your teacher.</italic>

</source>

????

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="bold"> <p> <b> <xsl:value-of select="."/> </b> </p> </xsl:template>

<xsl:template match="red"> <p style="color:red"> <xsl:value-of select="."/> </p> </xsl:template>

<xsl:template match="italic"> <p> <i> <xsl:value-of select="."/> </i> </p> </xsl:template>

</xsl:stylesheet>

<p> <b> Hello, My Students. </b> </p> <p style="color:red"> I am </p><p> <i> your teacher </i> </p>

Page 10: 單元 5-3 : XSL 範例練習

XSL 範例 5 (xpath and matching)

<source> <AAA id="a1" pos="start"> <BBB id="b1"/> <BBB id="b2"/> </AAA> <AAA id="a2"> <BBB id="b3"/> <BBB id="b4"/> <CCC id="c1"> <DDD id="d1"/> </CCC> <BBB id="b5"> <CCC id="c2"/> </BBB> </AAA> </source>

????

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="BBB"> <p> I am <xsl:value-of select=“@id”/></p> </xsl:template>

<xsl:template match="/source/AAA/CCC/DDD"> <p> You are <xsl:value-of select=“@id”/></p> </xsl:template>

</xsl:stylesheet>

<p>I am b1</p><p>I am b2</p><p>I am b3</p><p>I am b4</p><p>You are d1</p><p>I am b5</p>

Page 11: 單元 5-3 : XSL 範例練習

XSL 範例 6 (Matching)

<source> <AAA id="a1" pos="start"> <BBB id="b1"/> <BBB id="b2"/> </AAA> <AAA id="a2"> <BBB id="b3"/> <BBB id="b4"/> <CCC id="c1"> <DDD id="d1"/> </CCC> <BBB id="b5"> <CCC id="c2"/> </BBB> </AAA> </source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="AAA"> <p> I am <xsl:value-of select=“@id”/></p> </xsl:template>

</xsl:stylesheet>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/"> <xsl:apply-templates/> </xsl:template>

<xsl:template match="/source"> <xsl:apply-templates/> </xsl:template>

<xsl:template match="AAA"> <p> You are <xsl:value-of select=“@id”/></p> </xsl:template></xsl:stylesheet>

????

????

<p> I am a1</p><p>I am a2</p>

<p> You are a1</p><p>You are a2</p>

Page 12: 單元 5-3 : XSL 範例練習

XSL 範例 7

<source>

<employee> <firstName>Joe</firstName> <surname>Smith</surname> </employee>

</source>

<xsl:stylesheet version = '1.0‘ xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="firstName|surname"> <xsl:value-of select="name()"/> is <xsl:apply-templates/> </xsl:template>

</xsl:stylesheet>

????

<xsl:stylesheet version = '1.0‘ xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="*"> <xsl:value-of select="name()"/> is <xsl:apply-templates/> </xsl:template>

</xsl:stylesheet>

????

firstname is Joesurname is Smith

source is Joe Smith

Page 13: 單元 5-3 : XSL 範例練習

XSL 範例 8-1<source> <AAA id=“a1” pos=“start”>      <BBB id=“b1”/>      <BBB id=“b2”/> </AAA> <AAA id=“a2”>      <BBB id=“b3”/>      <BBB id=“b4”/>      <CCC id=“c1”>           <DDD id=“d1”/>      </CCC>      <BBB id=“b5”>           <CCC id=“c2”/>      </BBB> </AAA> </source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="/"> <xsl:apply-templates select="//BBB"/> <xsl:apply-templates select="//CCC"/> <xsl:apply-templates select="//DDD"/> <xsl:apply-templates select="//AAA"/> </xsl:template>

<xsl:template match="AAA"> I am <xsl:value-of select="@id"/> </xsl:template>

<xsl:template match="BBB"> You are <xsl:value-of select="@id"/> </xsl:template>

<xsl:template match="CCC"> She is <xsl:value-of select="@id"/> </xsl:template>

<xsl:template match="DDD"> He is <xsl:value-of select="@id"/> </xsl:template>

</xsl:stylesheet>

????

You are b1You are b2You are b3You are b4You are b5She is c1She is c2He is d1I am a1I am a2

Page 14: 單元 5-3 : XSL 範例練習

XSL 範例 8-2<source> <AAA id=“a1” pos=“start”>      <BBB id=“b1”/>      <BBB id=“b2”/> </AAA> <AAA id=“a2”>      <BBB id=“b3”/>      <BBB id=“b4”/>      <CCC id=“c1”>           <DDD id=“d1”/>      </CCC>      <BBB id=“b5”>           <CCC id=“c2”/>      </BBB> </AAA> </source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/"> <xsl:apply-templates select="/source/AAA//CCC"/> <xsl:apply-templates select="/source//AAA/BBB//*"/> </xsl:template>

<xsl:template match="AAA"> I am <xsl:value-of select="@id"/> </xsl:template>

<xsl:template match="BBB"> You are <xsl:value-of select="@id"/> </xsl:template>

<xsl:template match="CCC"> She is <xsl:value-of select="@id"/> </xsl:template>

<xsl:template match="DDD"> He is <xsl:value-of select="@id"/> </xsl:template>

</xsl:stylesheet>

????

She is c1She is c2You are b1You are b2You are b3You are b4You are b5She is c2

Page 15: 單元 5-3 : XSL 範例練習

XSL 範例 9-1<source> <AAA id="a1" pos="start"> <BBB id="b1"/> <BBB id="b2"/> </AAA> <AAA id="a2"> <BBB id="b3"/> <BBB id="b4"/> <CCC id="c1"> <CCC id="c2"/> </CCC> <BBB id="b5"> <CCC id="c3"/> </BBB> </AAA> </source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/"> <xsl:apply-templates select="//CCC" mode="red"/> <xsl:apply-templates select="//CCC" mode="blue"/> <xsl:apply-templates select="//CCC"/> </xsl:template>

<xsl:template match="CCC" mode="red"> I am red <xsl:value-of select="@id"/> </xsl:template>

<xsl:template match="CCC" mode="blue"> I am blue <xsl:value-of select="@id"/> </xsl:template>

<xsl:template match="CCC"> I am <xsl:value-of select="@id"/> </xsl:template>

</xsl:stylesheet>

????

I am red c1I am red c2I am red c3I am blue c1I am blue c2I am blue c3I am c1I am c2I am c3

Page 16: 單元 5-3 : XSL 範例練習

XSL 範例 9-2<source> <AAA id="a1" pos="start"> <BBB id="b1"/> <BBB id="b2"/> </AAA> <AAA id="a2"> <BBB id="b3"/> <BBB id="b4"/> <CCC id="c1"> <CCC id="c2"/> </CCC> <BBB id="b5"> <CCC id="c3"/> </BBB> </AAA> </source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/"> <xsl:apply-templates select="//CCC" mode="red"/> <xsl:apply-templates select="//CCC" mode="yellow"/> </xsl:template>

<xsl:template match="CCC" mode="red"> I am red <xsl:value-of select="@id"/> </xsl:template>

<xsl:template match="CCC"> I am <xsl:value-of select="@id"/> </xsl:template>

</xsl:stylesheet>

????

I am red c1I am red c2I am red c3

Page 17: 單元 5-3 : XSL 範例練習

XSL 範例 10<source>

<employee id="js0034"> Joe Smith </employee>

</source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="employee"> <xsl:value-of select="."/> with id: <xsl:apply-templates select="@id"/> </xsl:template>

<xsl:template match="@id"> <xsl:value-of select="."/> </xsl:template>

</xsl:stylesheet>

????

Joe Smith with id: js0034

Page 18: 單元 5-3 : XSL 範例練習

XSL 範例 11<source>

<car id="a234”checked="yes"/> <car id="a111" checked="yes"/> <car id="a005"/>

</source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="car[@checked]"> Car: <xsl:value-of select="@id"/> </xsl:template>

</xsl:stylesheet>

????

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="car[not (@checked)]"> <xsl:text>Car: </xsl:text> <xsl:value-of select="@id"/> </xsl:template>

</xsl:stylesheet>

????

Car: a234Car: a111

Car: a005

Page 19: 單元 5-3 : XSL 範例練習

XSL 範例 12 :迴圈<source>

<AAA id="a1" pos="start"> <BBB id="b1"/> <BBB id="b2"/> </AAA> <AAA id="a2"> <BBB id="b3"/> <BBB id="b4"/> <CCC id="c1"> <DDD id="d1"/> </CCC> <BBB id="b5"> <CCC id="c2"/> </BBB> </AAA>

</source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/"> <xsl:for-each select="//BBB"> I am <xsl:value-of select="@id"/> </xsl:for-each> <xsl:for-each select="source/AAA/CCC"> You are <xsl:value-of select="@id"/> </xsl:for-each> </xsl:template>

</xsl:stylesheet>

????

I am b1I am b2I am b3I am b4I am b5You are c1

Page 20: 單元 5-3 : XSL 範例練習

XSL 範例 13-1 :排序 (1/3)<source>

<name>John</name> <name>Josua</name> <name>Charles</name> <name>Alice</name> <name>Martha</name> <name>George</name>

</source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/"> <xsl:for-each select="//name"> <xsl:sort order="ascending" select="."/> I am <xsl:value-of select="."/> </xsl:for-each> </xsl:template>

</xsl:stylesheet>

????

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/"> <xsl:for-each select="//name"> <xsl:sort order=“descending" select="."/> You are <xsl:value-of select="."/> </xsl:for-each> </xsl:template>

</xsl:stylesheet>

????

I am AliceI am CharlesI am GeorgeI am JohnI am JosuaI am Martha

You are MarthaYou are JosuaYou are JohnYou are GeorgeYou are CharlesYou are Alice

Page 21: 單元 5-3 : XSL 範例練習

XSL 範例 13-2 :排序 (2/3)<source>

<car id="11"/> <car id="6"/> <car id="105"/> <car id="28"/> <car id="9"/>

</source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/"> <xsl:for-each select="//car"> <xsl:sort data-type="text" select="@id"/> Car: <xsl:value-of select="@id"/> </xsl:for-each> </xsl:template>

</xsl:stylesheet>

????

<xsl:stylesheet version = '1.0‘" xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/"> <xsl:for-each select="//car"> <xsl:sort data-type="number" elect="@id"/> Car: <xsl:value-of select="@id"/> </xsl:for-each> </xsl:template>

</xsl:stylesheet>

????

Car: 105Car: 11Car: 28Car: 6Car: 9

Car: 6Car: 9Car: 11Car: 28Car: 105

Page 22: 單元 5-3 : XSL 範例練習

XSL 範例 13-3 :排序 (3/3)<source>

<word id="czech"/> <word id="Czech"/> <word id="cook"/> <word id="TooK"/> <word id="took"/> <word id="Took"/>

</source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="/"> <xsl:for-each select="//word"> <xsl:sort case-order="upper-first" select="@id"/> I am <xsl:value-of select="@id"/> </xsl:for-each> </xsl:template>

</xsl:stylesheet>

????

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="/"> <xsl:for-each select="//word"> <xsl:sort case-order="lower-first" select="@id"/> I am <xsl:value-of select="@id"/> </xsl:for-each> </xsl:template></xsl:stylesheet>

????

I am CzechI am cookI am czechI am TookI am TookI am took

I am cookI am czechI am CzechI am tookI am TookI am Took

Page 23: 單元 5-3 : XSL 範例練習

XSL 範例 14-1 :條件處理(1/2)

<source>

<list> <entry name="A"/> <entry name="B"/> <entry name="C"/> <entry name="D"/> </list>

</source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="list"> <xsl:for-each select="entry"> <xsl:value-of select="@name"/> , </xsl:for-each> </xsl:template>

</xsl:stylesheet>

????

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="list"> <xsl:for-each select="entry"> <xsl:value-of select="@name"/> <xsl:if test="not (position()=last())"> , </xsl:if> </xsl:for-each> </xsl:template>

</xsl:stylesheet>

????

A,B,C,D,

A,B,C,D

Page 24: 單元 5-3 : XSL 範例練習

XSL 範例 14-2 :條件處理(1/2)<source>

<SECTION> <DATA>I need a pen.</DATA> <DATA>I need some paper.</DATA> <SUMMARY>I need a pen and some paper</SUMMARY> </SECTION> <SECTION> <DATA>I need bread.</DATA> <DATA>I need butter.</DATA> </SECTION>

</source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="//SECTION"> <xsl:choose> <xsl:when test="SUMMARY"> SUMMARY: <xsl:value-of select="SUMMARY"/> </xsl:when> <xsl:otherwise> <xsl:for-each select="DATA"> DATA: <xsl:value-of select="."/> </xsl:for-each> </xsl:otherwise> </xsl:choose> </xsl:template>

</xsl:stylesheet>

????

SUMMARY: I need a pen and some paperDATA: I need a bread.DATA: I need butter.

Page 25: 單元 5-3 : XSL 範例練習

XSL 範例 15 :尋找數字開頭的文字

<source>

<value>125</value> <value>3aacc</value> <value>qa111</value> <value>9-12-45</value> <value>Q6-88</value> <value>5-ACD</value>

</source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/"> <xsl:apply-templates select="//value"/> </xsl:template>

<xsl:template match="value"> <xsl:value-of select="."/> <xsl:if test="starts-with(translate(., '0123456789', '9999999999'), '9')"> Bingo! </xsl:if> </xsl:template>

</xsl:stylesheet>

????

125 Bingo!3aacc Bingo!qa1119-12-45 Bingo!Q6-88 5-ACD Bingo!

Page 26: 單元 5-3 : XSL 範例練習

XSL 範例 16-1 :產生數字與格式 (1/4)

<source><chapter>First Chapter</chapter> <chapter>Second Chapter <chapter>Subchapter 1</chapter> <chapter>Subchapter 2</chapter> </chapter> <chapter>Third Chapter <chapter>Subchapter A</chapter> <chapter>Subchapter B <chapter>sub a</chapter> <chapter>sub b</chapter> </chapter> <chapter>Subchapter C</chapter> </chapter> </source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/"> <xsl:for-each select="//chapter"> <xsl:number/> <xsl:value-of select="./text()"/> </xsl:for-each> </xsl:template>

</xsl:stylesheet>

????

1First Chapter2Second Chapter 1Subchapter 12Subchapter 23Third Chapter 1Subchapter A2Subchapter B 1sub a2sub b3Subchapter C

Page 27: 單元 5-3 : XSL 範例練習

XSL 範例 16-2 : : 產生數字與格式 (2/4)

<source><chapter>First Chapter</chapter> <chapter>Second Chapter <chapter>Subchapter 1</chapter> <chapter>Subchapter 2</chapter> </chapter> <chapter>Third Chapter <chapter>Subchapter A</chapter> <chapter>Subchapter B <chapter>sub a</chapter> <chapter>sub b</chapter> </chapter> <chapter>Subchapter C</chapter> </chapter> </source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/"> <xsl:for-each select="//chapter"> <xsl:number level="multiple"/> <xsl:value-of select="./text()"/> </xsl:for-each> </xsl:template>

</xsl:stylesheet> ????

1First Chapter2Second Chapter 2.1Subchapter 12.2Subchapter 23Third Chapter 3.1Subchapter A3.2Subchapter B 3.2.1sub a3.2.2sub b3.3Subchapter C

Page 28: 單元 5-3 : XSL 範例練習

XSL 範例 16-3: 產生數字與格式 (3/4)

<source><chapter>First Chapter</chapter> <chapter>Second Chapter <chapter>Subchapter 1</chapter> <chapter>Subchapter 2</chapter> </chapter> <chapter>Third Chapter <chapter>Subchapter A</chapter> <chapter>Subchapter B <chapter>sub a</chapter> <chapter>sub b</chapter> </chapter> <chapter>Subchapter C</chapter> </chapter> </source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/"> <xsl:for-each select="//chapter"> <xsl:number level="multiple" format="1.A.a "/> <xsl:value-of select="./text()"/> </xsl:for-each> </xsl:template>

</xsl:stylesheet>

????

1First Chapter2Second Chapter 2.ASubchapter 12.BSubchapter 23Third Chapter 3.ASubchapter A3.BSubchapter B 3.B.asub a3.B.bsub b3.CSubchapter C

Page 29: 單元 5-3 : XSL 範例練習

XSL 範例 16-4: 產生數字與格式 (4/4)

<source><chapter>First Chapter</chapter> <chapter>Second Chapter <chapter>Subchapter 1</chapter> <chapter>Subchapter 2</chapter> </chapter> <chapter>Third Chapter <chapter>Subchapter A</chapter> <chapter>Subchapter B <chapter>sub a</chapter> <chapter>sub b</chapter> </chapter> <chapter>Subchapter C</chapter> </chapter> </source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/"> <xsl:for-each select="//chapter"> <xsl:number level="multiple" format="I-1-a:"/> <xsl:value-of select="./text()"/> </xsl:for-each> </xsl:template>

</xsl:stylesheet>

????

1:First Chapter2:Second Chapter 2-1:Subchapter 12-2:Subchapter 23:Third Chapter 3-1:Subchapter A3-2:Subchapter B 3-2-a:sub a3-2-b:sub b3-3:Subchapter C

Page 30: 單元 5-3 : XSL 範例練習

XSL 範例 17-1 :變數 (1/2)

<source>

<chapter>Chapter A</chapter> <chapter>Chapter B</chapter> <chapter>Chapter C</chapter> <chapter>Chapter D</chapter>

</source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:variable name="totalChapters" select="count(//chapter)"/>

<xsl:template match="/"> <xsl:for-each select="//chapter"> <xsl:value-of select="."/> : <xsl:value-of select="position()"/> <xsl:text>/</xsl:text> <xsl:value-of select="$totalChapters"/> </xsl:for-each> </xsl:template>

</xsl:stylesheet>

????

Chapter A : 1/4Chapter B : 2/4Chapter C : 3/4Chapter D : 4/4

Page 31: 單元 5-3 : XSL 範例練習

XSL 範例 17-2 :變數 (2/2)

<source>

<chapter>Chapter A</chapter> <chapter>Chapter B</chapter> <chapter>Chapter C</chapter> <chapter>Chapter D</chapter>

</source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:variable name="text">Chapter</xsl:variable> <xsl:template match="/"> <xsl:for-each select="//chapter"> <xsl:variable name="text"> <xsl:choose> <xsl:when test="position() = 1">

First chapter </xsl:when> <xsl:when test="position()=last()">

Last chapter </xsl:when> <xsl:otherwise> <xsl:value-of select="$text"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:value-of select="$text"/> <xsl:text> : </xsl:text> <xsl:value-of select="."/> </xsl:for-each> </xsl:template></xsl:stylesheet>

????

First chapter : Chapter AChapter : Chapter BChapter : Chapter CLast chapter : Chapter D

Page 32: 單元 5-3 : XSL 範例練習

單元複習

透過諸多範例,理解 XSL 程式的撰寫› 轉換動作順序的控制› 轉換規則的套用› 迴圈,排序,條件處理› 文字轉換函數› 數字格式編排› 變數的應用