注意:XSL样式表本身也是一个XML文档,所以第一行必须为XML声明。
三、XSLT是一种闭包,它的输入和输出都是树,可以管道化利用,如:文档1 -> 文档2-> 文档3。
四、XSLT语法:XSL样式表由一个或多个模板(template)的规则组成,每个模板含有当前某个指定节点被匹配时所应用的规则。
a) <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">元素
XSLT的根元素,用于定义该文档是一个XSLT样式表,上例中包含版本号以及XSLT命名空间两个属性。其中stylesheet也可写为transform,两者是完全相同的。<xsl:template match="title"> Title </xsl:template>
<xsl:include href="book.xsl"> (:<xsl:import href="book.xsl">:) <xsl:template match="title"> NewTitle </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>
<td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td>
<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>
<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>
<xsl:for-each select="catalog/cd"> <xsl:value-of select="title"/> <xsl:if test="position()!=last()"> <xsl:text>, </xsl:text> </xsl:if> <xsl:if test="position()=last()-1"> <xsl:text> and </xsl:text> </xsl:if> <xsl:if test="position()=last()"> <xsl:text>!</xsl:text> </xsl:if> </xsl:for-each>
<xsl:choose> <xsl:when test="expression"> ... Output ... </xsl:when> <xsl:when test="expression"> ... Output ... </xsl:when> <xsl:otherwise> ... Output ... </xsl:otherwise> </xsl:choose>
<xsl:variable name="color"> <xsl:choose> <xsl:when test="@color"> <xsl:value-of select="@color"/> </xsl:when> <xsl:otherwise>green</xsl:otherwise> </xsl:choose> </xsl:variable>
<?xml version="1.0" encoding="UTF-8"?> <list> <book ID="666"> <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> subsub a</chapter> <chapter> subsub b</chapter> </chapter> </chapter> </book> </list>
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <xsl:template match="/"> <xsl:for-each select="//chapter"> <br/> <xsl:number level="multiple" format="1.A.a "/> <xsl:value-of select="./text()"/> </xsl:for-each> </xsl:template> </xsl:stylesheet>
<applet> <code class="Applet"/> <codebase>/src/code</codebase> </applet> <xsl:template match="/"> <xsl:apply-templates select="applet"> <applet code="{code/@class}" codebase="{codebase}/java"/> </xsl:apply-templates> </xsl:stylesheet>
实际效果为:
<applet code="Applet" codebase="src/code/java"/>
五、 XML与XSL关联:
<?xml-stylesheet type="text/xsl" href="***.xslt"?>
<?xml version="1.0" encoding="UTF-8"?> <Name> <AAA repeat="3"/> <BBB repeat="2"/> <CCC repeat="5"/> </Name>
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <xsl:template match="/Name/*"> <p> <xsl:call-template name="while"> <xsl:with-param name="test"> <xsl:value-of select="@repeat"/> </xsl:with-param> </xsl:call-template> </p> </xsl:template> <xsl:template name="while"> <xsl:param name="test"/> #test记录了repeat的次数 <xsl:value-of select="name()"/> #输出AAA或BBB或CCC <xsl:text/> <xsl:if test="$test!=1"> #判断当前repeat属性是否为1 <xsl:call-template name="while"> <xsl:with-param name="test"> <xsl:value-of select="$test - 1"/> #递归调用while模板,参数修正为test值减1,注意$test和-、-和1之间均有空格 </xsl:with-param> </xsl:call-template> </xsl:if> </xsl:template> </xsl:stylesheet>
CCCCCCCCCCCCCCC
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <xsl:variable name="totalChapters"> <xsl:value-of select="//chapter[last()]"/> </xsl:variable> <xsl:template match="/"> <xsl:value-of select="$totalChapters"/> </xsl:template> </xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <xsl:key name="findAuthor" match="list/book" use="author"/> <xsl:param name="author">Jim Blue</xsl:param> <xsl:template match="/"> <xsl:copy-of select="key('findAuthor', $author)"/> </xsl:template> </xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?> <list> <book ID="234"> <author>Jim Blue</author> <author>Dan Farm</author> <author>Blue Flowers</author> </book> <book ID="666"> <author>Mark Simp</author> <author>Jim Blue</author> <author>Green Trees</author> </book> <book ID="898"> <author>Jay Bart</author> <author>Red Tulips</author> </book> </list>
九、通过XSLT与XQuery生成HTML
给定students.xml文件,按照出生年月日排序,学号为Z00123101的学生信息用绿色显示,学号为Z00123102的学生信息用蓝色显示,其余用红色显示。
如
源students.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <students> <student id="Z00123101"> <name>The No.1 student</name> <birthday>1987.2</birthday> <phone>62220001</phone> </student> <student id="Z00123102"> <name>the No.2 student</name> <birthday>1975.6</birthday> <phone>62220002</phone> </student> <student id="Z00123103"> <name>the No.3 student</name> <birthday>1978.8</birthday> <phone>62220003</phone> </student> <student id="Z00123104"> <name>the No.4 student</name> <birthday>1976.2</birthday> <phone>62220004</phone> </student> </students>
通过XSLT生成:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:template match="/"> <html> <head> <table border="1" cols="4" width="100%"> <tr> <th>No</th> <th>Name</th> <th>Phone</th> <th>Birthday</th> </tr> <xsl:for-each select="students/student"> <xsl:sort select="birthday"/> <xsl:choose> <xsl:when test="@id='Z00123101'"> <tr style="color:green"> <td><xsl:value-of select="@id"/></td> <xsl:apply-templates select="name|phone"/> <xsl:apply-templates select="birthday"/> </tr> </xsl:when> <xsl:when test="@id='Z00123102'"> <tr style="color:blue"> <td><xsl:value-of select="@id"/></td> <xsl:apply-templates select="name|phone"/> <xsl:apply-templates select="birthday"/> </tr> </xsl:when> <xsl:otherwise> <tr style="color:red"> <td><xsl:value-of select="@id"/></td> <xsl:apply-templates select="name|phone"/> <xsl:apply-templates select="birthday"/> </tr> </xsl:otherwise> </xsl:choose> </xsl:for-each> </table> </head> <body></body> </html> </xsl:template> <xsl:template match="name|phone|birthday"> <td><xsl:value-of select="."></xsl:value-of></td> </xsl:template> </xsl:stylesheet>
通过XQuery生成:
xquery version "1.0"; <html> <head> <table border="1" cols="4" width="100%"> <tr> <th>No</th> <th>Name</th> <th>Phone</th> <th>Birthday</th> </tr> { for $x in doc("students.xml")/students/student order by $x/birthday return if(data($x/@id)="Z00123102") then <tr style="color:blue"> <td>{data($x/@id)}</td> <td>{data($x/name)}</td> <td>{data($x/phone)}</td> <td>{data($x/birthday)}</td> </tr> else if(data($x/@id)="Z00123101") then <tr style="color:green"> <td>{data($x/@id)}</td> <td>{data($x/name)}</td> <td>{data($x/phone)}</td> <td>{data($x/birthday)}</td> </tr> else <tr style="color:red"> <td>{data($x/@id)}</td> <td>{data($x/name)}</td> <td>{data($x/phone)}</td> <td>{data($x/birthday)}</td> </tr> } </table> </head> <body/> </html>