我有一个HTML文件:
<!DOCTYPE html>
<html>
<head>
<title>A title of the article</title>
<style type="text/css">
body {
font-family: Helvetica, arial, sans-serif;
font-size: 14px;
line-height: 1.6;
padding-top: 10px;
padding-bottom: 10px;
background-color: white;
padding: 30px; }
body > *:first-child {
margin-top: 0 !important; }
body > *:last-child {
margin-bottom: 0 !important; }
</style>
</head>
<body>
<p>The page is an article about an article.</p>
<p>This paragraph is not very good paragraph</p>
<p>This paragraph is very good paragraph</p>
<h4 id="toc_0">Page content</h4>
<ul>
<li>An itroduction</li>
<li>An inline piece of code <code>select * from dual</code></li>
<li>Buttons <kbd>OK</kbd> and <kbd>Cancel</kbd></li>
</ul>
<div>
<pre>
<code class="language-none">select * from dual
</code>
</pre>
</div>
<h4 id="toc_1">Usage</h4>
<table>
<thead>
<tr>
<th>Page ID</th>
<th>Page name</th>
</tr>
</thead>
<tbody>
<tr>
<td>1234</td>
<td>Page number 1234</td>
</tr>
<tr>
<td>5678</td>
<td>Page number 5678</td>
</tr>
<tr>
<td>90AB</td>
<td>Page number 90AB</td>
</tr>
</tbody>
</table>
</body>
</html>
我需要做以下工作:
-
替换标记
code
和
kbd
与
span
-
为每个新类添加一个类
跨度
使用以前的标记名(即
<code> ... </code>
应该转变成
<span class="code"> ... </span>
等)
-
保留标签内容
body
把剩下的都拿走。
我找到了部分的方法。此转换使步骤1和2:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="code">
<span class="code"><xsl:apply-templates select="@*|node()" /></span>
</xsl:template>
<xsl:template match="kbd">
<span class="kbd"><xsl:apply-templates select="@*|node()" /></span>
</xsl:template>
</xsl:stylesheet>
此转换使步骤3:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/html/body">
<xsl:copy-of select="node()"/>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
但每一个转变都只起作用。我可以替换标签,或者剪切
身体
.我试图将这些转换结合起来,但失败了。
另外,如果第二个转换已经存在,则它不会添加类。例如,这个
<code class="language-none">
变成这个
<span class="language-none">
尽管我希望有以下内容(类名的顺序无关紧要):
<span class="language-none code">
还有一件烦人的事。当源文件包含
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
然后我得到一个错误“无效的xml”。好吧,我同意这是无效的,但是在第二次转换之后,我的实用程序(我在mac上使用命令行实用程序xsltproc)将完全相同的行插入到结果文件中。