在尝试制作样式表来转换使用非常过时的 SGML DTD 进行格式化的书籍的旧 LoC 转录的过程中,我在以下情况遇到了障碍:
在转换后的 XML 文件中,有一些如下所示的文本行:
<p> Text on left <hsep></hsep> Text on right </p>
hsep 实质上将剩余文本推向右对齐。不幸的是,我不知道有什么方法可以通过转换标签来将其转换为 HTML,因为 HTML 没有像 hsep 那样缺乏可疑的 CSS hacks。我认为能够将其转换为类似的内容会更有用:
<p> Text on left <span class="right">Text on right</span> </p>
但是,我不确定如何执行此操作,因为它需要这样做,在 <p>
中元素,我判断是否有 <hsep>
然后根据剩余文本的存在创建一个标签,同时将模板应用于可能存在的任何元素。我不认为我有类似的情况
<p> Text a <em> Text b <hsep></hsep> Text c </em> </p>
很常见甚至存在,所以我认为这不会造成问题,但可能存在以下情况:
<p> <em> Text a Text b <hsep></hsep> Text c </em> </p>
我可以想到涉及正则表达式的复杂、可怕的方法,但我希望有一种不可怕的方法。
最佳答案
create a tag surrounding the remaining text based on it being there, while also applying templates to any elements that might be there
我认为为了更好地进行前向处理,您可以使用此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()[1]|@*"/>
</xsl:copy>
<xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>
<xsl:template match="hsep">
<span class="right">
<xsl:apply-templates select="following-sibling::node()[1]"/>
</span>
</xsl:template>
</xsl:stylesheet>
根据 Dimitre 的输入:
<html>
<p> Text a <em> Text b <hsep></hsep> Text c </em> </p>
<p> <em> Text a Text b <hsep></hsep> Text c </em> </p>
</html>
输出:
<html>
<p> Text a <em> Text b <span class="right"> Text c </span></em></p>
<p><em> Text a Text b <span class="right"> Text c </span></em></p>
</html>
注意:在 out 模式下,您可以为元素声明一次规则,无论是在 hsep
之前还是之后。
关于html - 使用 XSLT 根据空标签更改周围数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3562074/