320 likes | 428 Views
單元 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” ? > <booklist> <book>
E N D
單元5-3:XSL範例練習 王豐緒 銘傳大學資工系
單元目標 • 練習XSL程式的撰寫 • 了解XSL的轉換意義
XML文件中宣告XSL • 在XML文件中宣告XSL • <?xml-stylesheettype=”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>
如何撰寫XSL程式 • 務必先了解XSL的運作原理 • 了解題目所給XML文件的結構 • 了解所欲轉換成的結果(如HTML網頁) • 分析XML與轉換結果的對應關係 • 撰寫XSL的對應轉換規則 • 測試-除錯-修改-直到完成
以下的範例修改自: http://www.zvon.org/xxl/XSLTutorial/Output/contents.html
XSL範例1 <xsl:template match=“source”> <h1><xsl:value-of select=“title” /></h1> <h2><xsl:value-of select=“author” /></h2> </xsl:template> <h1>XML</h1><h2>FHWANG</h2> Xsl1? <source><title>XML</title> <author>FHWANG</author> </source> <xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> ???? </xsl:stylesheet> Xsl2? <h2>FHWANG</h2> <h1>XML</h1> <xsl:template match=“source”> <h1><xsl:value-of select=“title” /></h1> <h2><xsl:value-of select=“author” /></h2> </xsl:template>
XSL範例2 <xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> </xsl:stylesheet> <source> <em>Hello, My Students</em> </source> ???? Hello, My Students
XSL範例3(apply-templates) <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> <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: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>
XSL範例4 <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> <source> <bold>Hello, My Students.</bold> <red>I am </red> <italic>your teacher.</italic> </source> ???? <p> <b> Hello, My Students. </b> </p> <p style="color:red"> I am </p> <p> <i> your teacher </i> </p>
XSL範例5 (xpath and matching) <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> <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> ???? <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>
XSL範例6 (Matching) <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> <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> ???? <p> I am a1</p> <p>I am a2</p> <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> You are a1</p> <p>You are a2</p>
XSL範例7 <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> ???? firstname is Joe surname is Smith <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="*"> <xsl:value-of select="name()"/> is <xsl:apply-templates/> </xsl:template> </xsl:stylesheet> ???? source is Joe Smith
XSL範例8-1 <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> ???? <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> You are b1 You are b2 You are b3 You are b4 You are b5 She is c1 She is c2 He is d1 I am a1 I am a2
<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> 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> She is c1 She is c2 You are b1 You are b2 You are b3 You are b4 You are b5 She is c2
XSL範例9-1 <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> ???? <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> I am red c1 I am red c2 I am red c3 I am blue c1 I am blue c2 I am blue c3 I am c1 I am c2 I am c3
XSL範例9-2 <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> <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> ???? I am red c1 I am red c2 I am red c3
XSL範例10 <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> <source> <employee id="js0034"> Joe Smith </employee> </source> ???? Joe Smith with id: js0034
XSL範例11 <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> <source> <car id="a234”checked="yes"/> <car id="a111" checked="yes"/> <car id="a005"/> </source> ???? Car: a234 Car: a111 <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: a005
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 b1 I am b2 I am b3 I am b4 I am b5 You are c1
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> ???? I am Alice I am Charles I am George I am John I am Josua I am Martha <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> ???? You are Martha You are Josua You are John You are George You are Charles You are Alice
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:sortdata-type="text" select="@id"/> Car: <xsl:value-of select="@id"/> </xsl:for-each> </xsl:template> </xsl:stylesheet> ???? Car: 105 Car: 11 Car: 28 Car: 6 Car: 9 <xsl:stylesheet version = '1.0‘" xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="/"> <xsl:for-each select="//car"> <xsl:sortdata-type="number" elect="@id"/> Car: <xsl:value-of select="@id"/> </xsl:for-each> </xsl:template> </xsl:stylesheet> ???? Car: 6 Car: 9 Car: 11 Car: 28 Car: 105
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:sortcase-order="upper-first" select="@id"/> I am <xsl:value-of select="@id"/> </xsl:for-each> </xsl:template> </xsl:stylesheet> ???? I am Czech I am cook I am czech I am Took I am Took I am took <xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="/"> <xsl:for-each select="//word"> <xsl:sortcase-order="lower-first" select="@id"/> I am <xsl:value-of select="@id"/> </xsl:for-each> </xsl:template> </xsl:stylesheet> ???? I am cook I am czech I am Czech I am took I am Took I am Took
XSL範例14-1 :條件處理(1/2) <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> <source> <list> <entry name="A"/> <entry name="B"/> <entry name="C"/> <entry name="D"/> </list> </source> ???? A,B,C,D, <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
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 paper DATA: I need a bread. DATA: I need butter.
XSL範例15:尋找數字開頭的文字 <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> <source> <value>125</value> <value>3aacc</value> <value>qa111</value> <value>9-12-45</value> <value>Q6-88</value> <value>5-ACD</value> </source> ???? 125 Bingo! 3aacc Bingo! qa111 9-12-45 Bingo! Q6-88 5-ACD Bingo!
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
XSL範例16-2: :產生數字與格式(2/4) <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> <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> ???? 1First Chapter 2Second Chapter 2.1Subchapter 1 2.2Subchapter 2 3Third Chapter 3.1Subchapter A 3.2Subchapter B 3.2.1sub a 3.2.2sub b 3.3Subchapter C
XSL範例16-3:產生數字與格式(3/4) <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> <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> ???? 1First Chapter 2Second Chapter 2.ASubchapter 1 2.BSubchapter 2 3Third Chapter 3.ASubchapter A 3.BSubchapter B 3.B.asub a 3.B.bsub b 3.CSubchapter C
XSL範例16-4:產生數字與格式(4/4) <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> <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> ???? 1:First Chapter 2:Second Chapter 2-1:Subchapter 1 2-2:Subchapter 2 3:Third Chapter 3-1:Subchapter A 3-2:Subchapter B 3-2-a:sub a 3-2-b:sub b 3-3:Subchapter C
XSL範例17-1:變數(1/2) <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> <source> <chapter>Chapter A</chapter> <chapter>Chapter B</chapter> <chapter>Chapter C</chapter> <chapter>Chapter D</chapter> </source> ???? Chapter A : 1/4Chapter B : 2/4Chapter C : 3/4Chapter D : 4/4
XSL範例17-2:變數(2/2) <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> <source> <chapter>Chapter A</chapter> <chapter>Chapter B</chapter> <chapter>Chapter C</chapter> <chapter>Chapter D</chapter> </source> ???? First chapter : Chapter AChapter : Chapter BChapter : Chapter CLast chapter : Chapter D
單元複習 • 透過諸多範例,理解XSL程式的撰寫 • 轉換動作順序的控制 • 轉換規則的套用 • 迴圈,排序,條件處理 • 文字轉換函數 • 數字格式編排 • 變數的應用