XML XSLT转换

LearningXML XSLT转换 concepts, 语法 and application, Understandsuch as何usingXSLT将XMLdocumentation转换 for HTML, XML or other格式, implementationdata flexible展示 and processing

XSLTIntroduction

XSLT (Extensible Stylesheet Language Transformations, 可scale样式表language转换) is a用于将XMLdocumentation转换 for other格式 (such asHTML, XML, 纯文本etc.) language. 它 is XSL (Extensible Stylesheet Language, 可scale样式表language) 一部分, XSL还includingXPath (用于选择XMLnode) and XSL-FO (用于formatXMLdocumentation) .

XSLT 作用

  • 将XMLdata转换 for HTML, 以便 in 浏览器in显示
  • 将XMLdata转换 for otherXML格式, implementationdata格式转换
  • 将XMLdata转换 for 纯文本, 用于生成报告, logetc.
  • implementationXMLdata 筛选, sort and group
  • 将 many 个XMLdocumentationmerge for 一个documentation

XSLT basic原理

XSLTusing模板 (Template) 来定义such as何转换XMLdocumentation. 每个模板都package含一个匹配模式 (用于选择要转换 XMLnode) and 转换规则 (用于定义such as何转换选定 node) . XSLTprocessing器 from XMLdocumentation 根node开始, 按照模板规则递归地processing每个node, 生成转换 after documentation.

XSLTbasic语法

XSLTdocumentation本身也 is a XMLdocumentation, 它usingXSLTnamespace来定义转换规则. 以 under is XSLTdocumentation basicstructure:

XSLTdocumentationbasicstructure
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    
    
</xsl:stylesheet>

XSLT模板

模板 is XSLT core, 它using<xsl:template>元素来定义. 每个模板都 has 一个matchproperty, 用于指定要匹配 XMLnode.

XSLT模板example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    
    <xsl:template match="/">
        <html>
            <body>
                <h1>graph书list</h1>
                <xsl:apply-templates select="books" />
            </body>
        </html>
    </xsl:template>
    
    
    <xsl:template match="books">
        <table border="1">
            <tr>
                <th>书名</th>
                <th>作者</th>
                <th>价格</th>
            </tr>
            <xsl:apply-templates select="book" />
        </table>
    </xsl:template>
    
    
    <xsl:template match="book">
        <tr>
            <td><xsl:value-of select="title" /></td>
            <td><xsl:value-of select="author" /></td>
            <td><xsl:value-of select="price" /></td>
        </tr>
    </xsl:template>
    
</xsl:stylesheet>

XSLT常用元素

元素 describes
<xsl:stylesheet> XSLTdocumentation 根元素, 定义XSLTversion and namespace
<xsl:template> 定义转换模板, package含匹配模式 and 转换规则
<xsl:apply-templates> application匹配 模板 to 选定 node
<xsl:value-of> 提取选定node 文本 in 容
<xsl:for-each> 遍历选定 node集
<xsl:if> 条件判断
<xsl:choose> many 条件判断, package含<xsl:when> and <xsl:otherwise>
<xsl:sort> for nodeforsort

XSLTexample

以 under is a 完整 XSLTexample, 演示such as何将XMLdocumentation转换 for HTMLdocumentation.

XMLdocumentation: books.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>
<books>
    <book category="计算机">
        <title>XMLBasicstutorial</title>
        <author>张三</author>
        <year>2025</year>
        <price>99.00</price>
    </book>
    <book category="文学">
        <title>Javaprogramming思想</title>
        <author>李四</author>
        <year>2024</year>
        <price>89.00</price>
    </book>
    <book category="计算机">
        <title>Pythondataanalysis</title>
        <author>王五</author>
        <year>2025</year>
        <price>129.00</price>
    </book>
</books>
XSLTdocumentation: books.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    
    <xsl:template match="/">
        <html>
            <head>
                <title>graph书list</title>
                <style>
                    body {
                        font-family: Arial, sans-serif;
                        margin: 20px;
                    }
                    h1 {
                        color: #4285F4;
                    }
                    table {
                        border-collapse: collapse;
                        width: 100%;
                        margin-top: 20px;
                    }
                    th, td {
                        border: 1px solid #ddd;
                        padding: 8px;
                        text-align: left;
                    }
                    th {
                        background-color: #f2f2f2;
                    }
                    tr:nth-child(even) {
                        background-color: #f9f9f9;
                    }
                </style>
            </head>
            <body>
                <h1>graph书list</h1>
                <table>
                    <tr>
                        <th>书名</th>
                        <th>作者</th>
                        <th>年份</th>
                        <th>价格</th>
                        <th>classification</th>
                    </tr>
                    <!-- 遍历所 has book元素, 并按价格sort -->
                    <xsl:for-each select="books/book">
                        <xsl:sort select="price" data-type="number" order="ascending" />
                        <tr>
                            <td><xsl:value-of select="title" /></td>
                            <td><xsl:value-of select="author" /></td>
                            <td><xsl:value-of select="year" /></td>
                            <td><xsl:value-of select="price" /></td>
                            <td><xsl:value-of select="@category" /></td>
                        </tr>
                    </xsl:for-each>
                </table>
                
                
                <div style="margin-top: 20px; padding: 10px; background-color: #e3f2fd; border-radius: 5px;">
                    <h3>statisticsinformation</h3>
                    <p>graph书总数: <xsl:value-of select="count(books/book)" />本</p>
                    <p>计算机classgraph书: <xsl:value-of select="count(books/book[@category='计算机'])" />本</p>
                    <p>平均价格: <xsl:value-of select="format-number(sum(books/book/price) div count(books/book), '0.00')" />元</p>
                </div>
            </body>
        </html>
    </xsl:template>
    
</xsl:stylesheet>

XSLT转换结果

当浏览器打开books.xmlfile时, 它会自动applicationbooks.xsl样式表, 将XMLdocumentation转换 for HTMLdocumentation并显示. 转换结果such as under :

HTML转换结果
<html>
    <head>
        <title>graph书list</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                margin: 20px;
            }
            h1 {
                color: #4285F4;
            }
            table {
                border-collapse: collapse;
                width: 100%;
                margin-top: 20px;
            }
            th, td {
                border: 1px solid #ddd;
                padding: 8px;
                text-align: left;
            }
            th {
                background-color: #f2f2f2;
            }
            tr:nth-child(even) {
                background-color: #f9f9f9;
            }
        </style>
    </head>
    <body>
        <h1>graph书list</h1>
        <table>
            <tr>
                <th>书名</th>
                <th>作者</th>
                <th>年份</th>
                <th>价格</th>
                <th>classification</th>
            </tr>
            <tr>
                <td>Javaprogramming思想</td>
                <td>李四</td>
                <td>2024</td>
                <td>89.00</td>
                <td>文学</td>
            </tr>
            <tr>
                <td>XMLBasicstutorial</td>
                <td>张三</td>
                <td>2025</td>
                <td>99.00</td>
                <td>计算机</td>
            </tr>
            <tr>
                <td>Pythondataanalysis</td>
                <td>王五</td>
                <td>2025</td>
                <td>129.00</td>
                <td>计算机</td>
            </tr>
        </table>
        
        <div style="margin-top: 20px; padding: 10px; background-color: #e3f2fd; border-radius: 5px;">
            <h3>statisticsinformation</h3>
            <p>graph书总数: 3本</p>
            <p>计算机classgraph书: 2本</p>
            <p>平均价格: 105.67元</p>
        </div>
    </body>
</html>

XSLTadvanced features

1. variable and parameter

XSLT允许usingvariable and parameter来store and 传递值. variableusing<xsl:variable>元素定义, parameterusing<xsl:param>元素定义.

XSLTvariable and parameterexample
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    
    <xsl:variable name="author" select="'张三'" />
    
    
    <xsl:param name="category" select="'计算机'" />
    
    <xsl:template match="/">
        <html>
            <body>
                <h1>{$author} graph书</h1>
                <h2>classification: {$category}</h2>
                
                
                <xsl:variable name="bookCount" select="count(books/book[@category=$category])" />
                <p>graph书数量: {$bookCount}</p>
                
                
            </body>
        </html>
    </xsl:template>
    
</xsl:stylesheet>

2. 模板递归

XSLTsupport模板递归, 可以用来processing具 has 层次structure XMLdocumentation (such astree形structure) .

XSLT模板递归example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/">
        <html>
            <body>
                <h1>Table of Contentsstructure</h1>
                <ul>
                    <xsl:apply-templates select="directory" />
                </ul>
            </body>
        </html>
    </xsl:template>
    
    
    <xsl:template match="directory">
        <li>
            <strong><xsl:value-of select="@name" /></strong>
            <xsl:if test="directory or file">
                <ul>
                    <xsl:apply-templates select="directory" />
                    <xsl:apply-templates select="file" />
                </ul>
            </xsl:if>
        </li>
    </xsl:template>
    
    
    <xsl:template match="file">
        <li><xsl:value-of select="@name" /></li>
    </xsl:template>
    
</xsl:stylesheet>

3. 输出格式控制

XSLTproviding了<xsl:output>元素来控制输出documentation 格式, including输出class型, 编码, indentetc..

XSLT输出格式控制example
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    
    <xsl:output 
        method="html" 
        encoding="UTF-8" 
        indent="yes" 
        doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
        doctype-system="http://www.w3.org/TR/html4/loose.dtd"
    />
    
    
    
</xsl:stylesheet>

实践case: usingXSLT生成RSS feed

casedescribes

creation一个XSLT样式表, 将博客文章 XMLdocumentation转换 for RSS 2.0 feed格式.

implementation步骤

  1. creation博客文章 XMLdocumentation
  2. creationXSLT样式表, 定义RSS feed 转换规则
  3. applicationXSLT样式表, 生成RSS feed

最终code

XMLdocumentation: blog.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="blog.xsl"?>
<blog>
    <title>techniques博客</title>
    <link>http://www.example.com/blog</link>
    <description>分享techniquesknowledge and experience</description>
    <language>zh-CN</language>
    <lastbuildDate>2025-01-15T10:30:00+08:00</lastbuildDate>
    
    <posts>
        <post id="1">
            <title>XMLBasicstutorial</title>
            <link>http://www.example.com/blog/xml-basics</link>
            <description>LearningXML basicconcepts, 语法 and application. </description>
            <pubDate>2025-01-15T09:00:00+08:00</pubDate>
            <author>张三</author>
            <category>XML</category>
        </post>
        <post id="2">
            <title>XSLT转换techniques</title>
            <link>http://www.example.com/blog/xslt-transformations</link>
            <description>深入UnderstandXSLT转换techniques, implementationXMLdata flexibleprocessing. </description>
            <pubDate>2025-01-14T14:30:00+08:00</pubDate>
            <author>李四</author>
            <category>XSLT</category>
        </post>
        <post id="3">
            <title>XPath选择器</title>
            <link>http://www.example.com/blog/xpath-selectors</link>
            <description>MasterXPath选择器,  high 效定位XMLdocumentationin node. </description>
            <pubDate>2025-01-13T16:45:00+08:00</pubDate>
            <author>王五</author>
            <category>XPath</category>
        </post>
    </posts>
</blog>
XSLTdocumentation: blog.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />
    
    <xsl:template match="/">
        <!-- RSS 2.0根元素 -->
        <rss version="2.0">
            <channel>
                
                <title><xsl:value-of select="blog/title" /></title>
                <link><xsl:value-of select="blog/link" /></link>
                <description><xsl:value-of select="blog/description" /></description>
                <language><xsl:value-of select="blog/language" /></language>
                <lastbuildDate><xsl:value-of select="blog/lastbuildDate" /></lastbuildDate>
                <generator>XSLT RSS Generator</generator>
                
                
                <xsl:for-each select="blog/posts/post">
                    <!-- 按release日期sort, 最 new   in  before  -->
                    <xsl:sort select="pubDate" order="descending" />
                    
                    <item>
                        <title><xsl:value-of select="title" /></title>
                        <link><xsl:value-of select="link" /></link>
                        <description>
                            <xsl:text disable-output-escaping="yes"><![CDATA[<p>]]></xsl:text>
                            <xsl:value-of select="description" />
                            <xsl:text disable-output-escaping="yes"><![CDATA[</p>]]></xsl:text>
                        </description>
                        <pubDate><xsl:value-of select="pubDate" /></pubDate>
                        <author><xsl:value-of select="author" /></author>
                        <category><xsl:value-of select="category" /></category>
                        <guid isPermaLink="true"><xsl:value-of select="link" /></guid>
                    </item>
                </xsl:for-each>
            </channel>
        </rss>
    </xsl:template>
    
</xsl:stylesheet>

互动练习

练习1: writingXSLT样式表

针 for 以 under XMLdocumentation, writing一个XSLT样式表, 将其转换 for HTML表格, 要求:
  1. 显示所 has 学生 姓名, 年龄 and 成绩
  2. 按成绩降序sort
  3. for 成绩 big 于80分 学生添加红色背景
  4. 计算并显示平均成绩
<?xml version="1.0" encoding="UTF-8"?>
<students>
    <student>
        <name>张三</name>
        <age>20</age>
        <score>85</score>
    </student>
    <student>
        <name>李四</name>
        <age>21</age>
        <score>78</score>
    </student>
    <student>
        <name>王五</name>
        <age>19</age>
        <score>92</score>
    </student>
    <student>
        <name>赵六</name>
        <age>20</age>
        <score>75</score>
    </student>
</students>

XSLT样式表such as under :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/">
        <html>
            <head>
                <title>学生成绩表</title>
                <style>
                    table {
                        border-collapse: collapse;
                        width: 100%;
                        font-family: Arial, sans-serif;
                    }
                    th, td {
                        border: 1px solid #ddd;
                        padding: 8px;
                        text-align: left;
                    }
                    th {
                        background-color: #f2f2f2;
                    }
                    .high-score {
                        background-color: #ffebee;
                    }
                    .average {
                        margin-top: 20px;
                        font-weight: bold;
                    }
                </style>
            </head>
            <body>
                <h1>学生成绩表</h1>
                <table>
                    <tr>
                        <th>姓名</th>
                        <th>年龄</th>
                        <th>成绩</th>
                    </tr>
                    <!-- 遍历所 has 学生, 并按成绩降序sort -->
                    <xsl:for-each select="students/student">
                        <xsl:sort select="score" data-type="number" order="descending" />
                        
                        
                        <xsl:choose>
                            <xsl:when test="score > 80">
                                <tr class="high-score">
                                    <td><xsl:value-of select="name" /></td>
                                    <td><xsl:value-of select="age" /></td>
                                    <td><xsl:value-of select="score" /></td>
                                </tr>
                            </xsl:when>
                            <xsl:otherwise>
                                <tr>
                                    <td><xsl:value-of select="name" /></td>
                                    <td><xsl:value-of select="age" /></td>
                                    <td><xsl:value-of select="score" /></td>
                                </tr>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:for-each>
                </table>
                
                
                <div class="average">
                    <xsl:variable name="totalScore" select="sum(students/student/score)" />
                    <xsl:variable name="studentCount" select="count(students/student)" />
                    <xsl:variable name="averageScore" select="$totalScore div $studentCount" />
                    平均成绩: <xsl:value-of select="format-number($averageScore, '0.00')" />
                </div>
            </body>
        </html>
    </xsl:template>
    
</xsl:stylesheet>