本文主要是介绍XSLT入门 -- 第3章 实践篇,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 复制
这是个复制所有节点的例子(程序1)。例如从XHTML变换成XHTML等情况,源和结果的元素大部分相同时,就可以将该实例稍作修改,仅针对需要改动的部分书写代码即可。
<?xml version="1.0"?>
<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="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
2 行列交换
该实例可以将HTML表格的行和列进行交换(程序2)。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="table">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:for-each select="tr[1]/td">
<tr>
<xsl:variable name="x" select="last()"/>
<xsl:variable name="y" select="position()-1"/>
<xsl:for-each select="../../tr/td">
<xsl:if test="(position()-1) mod $x = $y">
<td>
<xsl:value-of select="."/>
</td>
</xsl:if>
</xsl:for-each>
</tr>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:for-each select="tr[1]/td">
<xsl:for-each select="../../tr/td">
变量x中保存新建表格的总行数,变量y中保存当前正在生成的行的行数-1。因此,
<xsl:if test="(position()-1) mod $x = $y">
将如图1所示的页面(程序3)进行转换,可以得到如图2所示的输出结果。

<?xml version="1.0"?>
<html>
<head>
<title>表格</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
</head>
<h1>表格</h1>
<body>
<table border="1">
<tr>
<td>甲</td>
<td>乙</td>
<td>丙</td>
<td>丁</td>
<td>戊</td>
</tr>
<tr>
<td>己</td>
<td>庚</td>
<td>辛</td>
<td>壬</td>
<td>癸</td>
</tr>
</table>
</body>
</html>

3 表格
表格可以看作是二维数组。让我们试着将它转换成一维数组。但是仅仅将表格内容排列出来就失去了意义,因此我们将保留原表格的行号和列号作为结果的属性值(程序4)。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<databank>
<xsl:apply-templates select=".//table"/>
</databank>
</xsl:template>
<xsl:template match="table">
<xsl:for-each select="tr">
<xsl:variable name="row" select="position()"/>
<xsl:for-each select="td">
<xsl:variable name="column" select="position()"/>
<data>
<xsl:attribute name="row">
<xsl:copy-of select="$row"/>
</xsl:attribute>
<xsl:attribute name="column">
<xsl:copy-of select="$column"/>
</xsl:attribute>
<xsl:value-of select="."/>
</data>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<xsl:variable name="row" select="position()"/>
<xsl:variable name="column"select="position()"/>
<xsl:attribute name="row">
<xsl:copy-of select="$row"/>
</xsl:attribute>
<xsl:attribute name="column">
<xsl:copy-of select="$column"/>
</xsl:attribute>
<?xml version="1.0" encoding="UTF-8"?>
<databank>
<data row="1" column="1">甲</data>
<data row="1" column="2">乙</data>
<data row="1" column="3">丙</data>
<data row="1" column="4">丁</data>
<data row="1" column="5">戊</data>
<data row="2" column="1">己</data>
<data row="2" column="2">庚</data>
<data row="2" column="3">辛</data>
<data row="2" column="4">壬</data>
<data row="2" column="5">癸</data>
</databank>
4 将表格转换为CSV文件
虽然XSLT一般用来将XML文档转换成XML文档,但是也能够输出XML文档之外的格式。下面是将含有table元素的HTML文档转换成CSV(Comma Separated Value)格式输出的例子(程序5)。 1
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:apply-templates select=".//tr"/>
</xsl:template>
<xsl:template match="tr">
<xsl:for-each select="td">
<xsl:value-of select="."/>
<xsl:if test="not(position()=last())">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
<xsl:output method="text" encoding="UTF-8"/>
<xsl:if test="not(position()=last())">
<xsl:text>,</xsl:text>
</xsl:if>
甲,乙,丙,丁,戊
己,庚,辛,壬,癸
这篇关于XSLT入门 -- 第3章 实践篇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!