Xml 如何组合几个XSLT转换?
我有一个HTML文件:Xml 如何组合几个XSLT转换?,xml,xslt,Xml,Xslt,我有一个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
<!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>
用法
页面ID
页面名称
1234
第1234页
5678
第5678页
90AB
第90AB页
我需要做以下工作:
code
和kbd
替换为span
span
添加一个类(即..
应转换为..
等)正文的内容
并删除其余内容<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>
变成这样
<span class="language-none code">
虽然我希望有以下内容(类名的顺序无关紧要):
还有一件恼人的事。当源文件包含该行时
然后我得到一个错误“无效XML”。好的,我同意这是无效的,但是在第二次转换之后,我的实用程序(我在Mac上使用命令行实用程序xsltproc)将完全相同的行插入结果文件。要结合这三个步骤,您可以为只处理子项的
html
和body
编写模板,对于不起任何作用的html/head
,您需要找到一些方法来添加新类并保留现有类,下面是一个:
如果结果文档的根元素是
html
,则处理器假定您需要xsl:output method=“html”
并插入/添加该meta
元素,并以html语法执行该操作。如果您想创建中介X(HT)ML,则需要设置
,这样就不会得到元
,XSLT结果将根据XML规则序列化,并可由XML工具(如XSLT处理器)进一步处理。您可能需要检查xsltproc
功能,它基于libxml和libxslt,我认为它的开发人员引入了一些处理HTML而不是XML的方法。至于其他问题,您希望最终的输出结果是什么,输出应该是没有公共HTML
根元素的XML或HTML片段,所有这些p
和ul
以及输入的body
的其他子元素都是顶级同级元素?至于源文件中的meta
元素:如果存在忽略它的可能性,那就太好了。对于结果文件:如果足够将结果保存到包含任何根元素的文件中(html
也可以),则body
标记的内容不应更改,除非在问题的第1页和第2页进行了转换。谢谢!正是我想要的!
<code class="language-none">
<span class="language-none">
<span class="language-none code">
<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="html | html/body">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="html/head"/>
<xsl:template match="code">
<span class="{@class} code"><xsl:apply-templates select="@*[not(local-name() = 'class')] | node()" /></span>
</xsl:template>
<xsl:template match="kbd">
<span class="{@class} kbd"><xsl:apply-templates select="@*[not(local-name() = 'class')] | node()" /></span>
</xsl:template>
</xsl:stylesheet>