欢迎光临
我们一直在努力

XSLT模板匹配实战指南

XSLT <template> 基础概念

XSLT(Extensible Stylesheet Language Transformations)是一种用于将 XML 文档转换为其他格式的语言。<template> 是 XSLT 的核心元素之一,用于定义如何匹配和处理 XML 中的节点。每个 <template> 可以包含转换规则,当匹配到特定节点时,这些规则会被执行。

<template> 的基本语法如下:

<xsl:template match="XPathExpression" mode="modeName" priority="number">
<!– 转换规则 –>
</xsl:template>

  • match 属性指定 XPath 表达式,用于匹配 XML 中的节点。
  • mode 和 priority 是可选的,用于处理复杂的匹配场景。

匹配特定节点

以下是一个简单的例子,展示如何匹配 XML 中的 book 节点并输出其内容:

输入 XML (books.xml):

<library>
<book id="101">
<title>XSLT Guide</title>
<author>John Doe</author>
</book>
<book id="102">
<title>XML Basics</title>
<author>Jane Smith</author>
</book>
</library>

XSLT 样式表 (transform.xsl):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>Book List</h1>
<xsl:apply-templates select="library/book"/>
</body>
</html>
</xsl:template>

<xsl:template match="book">
<div>
<h2><xsl:value-of select="title"/></h2>
<p>Author: <xsl:value-of select="author"/></p>
</div>
</xsl:template>
</xsl:stylesheet>

输出 HTML:

<html>
<body>
<h1>Book List</h1>
<div>
<h2>XSLT Guide</h2>
<p>Author: John Doe</p>
</div>
<div>
<h2>XML Basics</h2>
<p>Author: Jane Smith</p>
</div>
</body>
</html>


使用 mode 处理多重匹配

mode 属性允许对同一节点应用不同的模板规则。以下示例展示如何用不同模式输出 book 节点的内容:

XSLT 样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>Book List (Detailed)</h1>
<xsl:apply-templates select="library/book" mode="detailed"/>
<h1>Book List (Simple)</h1>
<xsl:apply-templates select="library/book" mode="simple"/>
</body>
</html>
</xsl:template>

<xsl:template match="book" mode="detailed">
<div>
<h2><xsl:value-of select="title"/></h2>
<p>Author: <xsl:value-of select="author"/></p>
<p>ID: <xsl:value-of select="@id"/></p>
</div>
</xsl:template>

<xsl:template match="book" mode="simple">
<p><xsl:value-of select="title"/> – <xsl:value-of select="author"/></p>
</xsl:template>
</xsl:stylesheet>

输出 HTML:

<html>
<body>
<h1>Book List (Detailed)</h1>
<div>
<h2>XSLT Guide</h2>
<p>Author: John Doe</p>
<p>ID: 101</p>
</div>
<div>
<h2>XML Basics</h2>
<p>Author: Jane Smith</p>
<p>ID: 102</p>
</div>
<h1>Book List (Simple)</h1>
<p>XSLT Guide – John Doe</p>
<p>XML Basics – Jane Smith</p>
</body>
</html>


使用 priority 解决冲突匹配

当多个模板匹配同一节点时,priority 属性可以指定优先级。数值越高,优先级越高。

XSLT 样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="library/book"/>
</xsl:template>

<!– 默认优先级为 0 –>
<xsl:template match="book">
<p>Default: <xsl:value-of select="title"/></p>
</xsl:template>

<!– 更高优先级 –>
<xsl:template match="book[@id='101']" priority="1">
<p>Priority Match: <xsl:value-of select="title"/></p>
</xsl:template>
</xsl:stylesheet>

输出:

<p>Priority Match: XSLT Guide</p>
<p>Default: XML Basics</p>


递归处理嵌套结构

<template> 可以递归处理嵌套的 XML 结构。以下示例展示如何遍历嵌套的目录:

输入 XML (directories.xml):

<directory name="root">
<directory name="docs">
<file name="readme.txt"/>
</directory>
<file name="config.xml"/>
</directory>

XSLT 样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<ul>
<xsl:apply-templates select="directory"/>
</ul>
</xsl:template>

<xsl:template match="directory">
<li>
Directory: <xsl:value-of select="@name"/>
<ul>
<xsl:apply-templates select="directory | file"/>
</ul>
</li>
</xsl:template>

<xsl:template match="file">
<li>File: <xsl:value-of select="@name"/></li>
</xsl:template>
</xsl:stylesheet>

输出 HTML:

<ul>
<li>
Directory: root
<ul>
<li>
Directory: docs
<ul>
<li>File: readme.txt</li>
</ul>
</li>
<li>File: config.xml</li>
</ul>
</li>
</ul>


条件处理与 <xsl:if> 和 <xsl:choose>

<template> 中可以结合条件逻辑实现动态输出。

XSLT 样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table border="1">
<tr>
<th>Title</th>
<th>Author</th>
<th>Status</th>
</tr>
<xsl:apply-templates select="library/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:choose>
<xsl:when test="@id='101'">Featured</xsl:when>
<xsl:otherwise>Standard</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>

输出 HTML:

<table border="1">
<tr>
<th>Title</th>
<th>Author</th>
<th>Status</th>
</tr>
<tr>
<td>XSLT Guide</td>
<td>John Doe</td>
<td>Featured</td>
</tr>
<tr>
<td>XML Basics</td>
<td>Jane Smith</td>
<td>Standard</td>
</tr>
</table>


使用 <xsl:call-template> 复用逻辑

通过命名模板(<xsl:template name="…">)和 <xsl:call-template> 可以复用代码块。

XSLT 样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:call-template name="header"/>
<xsl:apply-templates select="library/book"/>
<xsl:call-template name="footer"/>
</xsl:template>

<xsl:template match="book">
<p><xsl:value-of select="title"/></p>
</xsl:template>

<xsl:template name="header">
<h1>Book Titles</h1>
</xsl:template>

<xsl:template name="footer">
<hr/>
<p>End of list</p>
</xsl:template>
</xsl:stylesheet>

输出 HTML:

<h1>Book Titles</h1>
<p>XSLT Guide</p>
<p>XML Basics</p>
<hr/>
<p>End of list</p>


总结

XSLT 的 <template> 是转换 XML 的核心工具,支持以下功能:

  • 通过 match 属性匹配特定节点。
  • 使用 mode 和 priority 处理复杂场景。
  • 递归处理嵌套结构。
  • 结合条件逻辑动态生成内容。
  • 通过命名模板复用代码块。

通过灵活组合这些特性,可以实现复杂的 XML 转换需求。

赞(0)
未经允许不得转载:171主机测评 » XSLT模板匹配实战指南
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址