我该如何防止xsltproc生成重复的HTML代码?
我在扩展docbook xml模板时遇到一个问题。当我把XML文件转换为HTML时,某些HTML代码会被生成两次。
样式表是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:db="http://docbook.org/ns/docbook">
<xsl:import href="http://docbook.sourceforge.net/release/xsl/current/html/docbook.xsl"/>
<xsl:output method="html" indent="yes"/>
<xsl:template match="description">
<p>
<xsl:value-of select="."/>
</p>
<!-- <xsl:apply-templates/> -->
</xsl:template>
</xsl:stylesheet>
用于测试样式表的XML文件是:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="myxsl.xsl"?>
<article xmlns="http://docbook.org/ns/docbook" version="5.0">
<section>
<title>
D-Bus procedures
</title>
<description>
This procedure calls a method synchronously.
<code>This is a test</code>.
</description>
</section>
</article>
当我用下列命令把XML文件转换成HTML时
xsltproc myxsl.xsl myxml.xml > myxml.html
得到如下输出:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="article">
<div class="titlepage"><hr></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl class="toc"><dt><span class="section"><a href="#id1337">
D-Bus procedures
</a></span></dt></dl>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="id1337"></a>
D-Bus procedures
</h2></div></div></div>
<p xmlns:db="http://docbook.org/ns/docbook">
This procedure calls a method synchronously.
This is a test.
</p>
This procedure calls a method synchronously.
<code class="code">This is a test</code>.
</div>
</div></body>
</html>
文本“This procedure...”被渲染了两次。如果我把样式表中的那一行
<xsl:apply-templates/>
注释掉,问题就会消失,但文本“This is a test”不会被渲染为代码(...)。
预期的输出是
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="article">
<div class="titlepage"><hr></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl class="toc"><dt><span class="section"><a href="#id1337">
D-Bus procedures
</a></span></dt></dl>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="id1337"></a>
D-Bus procedures
</h2></div></div></div>
<p xmlns:db="http://docbook.org/ns/docbook">
This procedure calls a method synchronously.
This is a test.
</p>
</div>
</div></body>
</html>
解决方案
你没有贴出预期输出,所以我只能猜测你想要做的是:
<xsl:template match="description">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
这假设缺失的代码包含一个模板,能够将 code 元素原样复制。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。