我有一个使用 XSLT 2.0/3.0 的工作流程,它使用支持 XSLT 3.0 的 Saxon-HE 最新版本,它将 XML 文件中的 CDATA 部分的文本标记为 HTML。该文本没有 HTML 标记,但有最少的纯文本标记,它依赖于新行和行首方括号中的标记。工作过程(此处未显示)使用多个阶段将文本设置在变量中,使用带有模式和大量的替换函数<
等>
'
逐步进入最后一步。它不仅难以阅读,而且如果我想向标记添加另一个更改,它的可扩展性也不是很好。我已经开始尝试在下面构建一个更好的标记过程,但被卡住了。
这是我修剪的 XML 文件结构的一个小示例:
<?xml version="1.0" encoding="UTF-8"?>
<project>
---------------------------------------------------
<document>
<docText><![CDATA[
[page 001] 1
[margin] Person1 to Person2
This Indenture made this x''th Day of y in the year z Between person1, grantor, of place1 to person2, grantee, of place2 for 5 dollars ... the s''d person1 to s''d person2 ... signed under my hand.
Witnesses present
[signed] Mrs. Jane Doe (seal)
[witness] Mr. Witness1
[witness] Ms. Witness1
Court office month x''th year
I do hereby certify that ... and is thereon truly admitted to Record
[clerk] John G. Reynolds DCCC
]]></docText>
<persons>
<person role="grantor">Jane Doe</person>
<person role="grantee">Bob Jones</person>
</persons>
</document>
---------------------------------------------------
<document>
<docText><![CDATA[
[page 002] 2
[margin] Person3 to Person4
This Indenture made this x''th Day of y in the year z Between person1, grantor, of place1 to person2, grantee, of place2 for 5 dollars ... the s''d person1 to s''d person2 ... signed under my hand.
Witnesses present
[signed] Mr. John Doe (seal)
[witness] Mr. Witness1
[witness] Ms. Witness1
[page 003] 3
Court office month x''th year
I do hereby certify that ... and is thereon truly admitted to Record
[clerk] John G. Reynolds DCCC
]]></docText>
<persons>
<person role="grantor">John Doe</person>
<person role="grantee">Bob Jones</person>
</persons>
</document>
</project>
这些是我想对 CDATA 中的文本采取的一些步骤
- 使用 \n 新行标记所有行
- 以方括号中的单词开头的行(例如,[witness])
<div>
使用括号中的类(例如,<div class="witness">rest of line</div>
)标记 - 剩余的行用
<p>
标签标记 - 所有空行都被消除
- 扫描上面的
<div>
和<p>
文本节点中的文本以进行进一步处理: - 找到任何一对单引号(即成对的撇号)后跟 1 到 4 个大写或小写字母并放入
<sup></sup>
(例如,第 25'' 变成25<sup>th</sup>
) <div>
将相同类名的相邻分组到<div>
某个名称的外部,例如<div class="a">b</div> <div class="a">b</div> becomes <div class="a-outer"> <div class="a">b</div> <div class="a">b</div> </div>
- 根据需要附加标记。
我通过第 6 步(5 步的一半)得到了我想要的东西,尽管结构可能很差。这个样式表有效,并为我提供了我在更长的以前的样式表和模板中所拥有的大部分内容。
这是我的XSLT 3.0样式表和模板的缩短版本:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="my:functions"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="xsl xs my fn" expand-text="yes">
<xsl:output method="html" html-version="5.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>Test Title</title>
<style>
div {{background-color: pink;}}
p {{background-color: ; clear: right; margin-bottom: 0;}}
.clerk, .signed {{float:right;}}
.margin::before {{content: "[margin note:] ";}}
.clear {{clear: right;}}
</style>
</head>
<body>
<h2>Records</h2>
<xsl:apply-templates select="project/document"/>
</body>
</html>
</xsl:template>
<xsl:template match="document">
<article>
<h3><xsl:value-of select="persons/person[@role='grantor']"/> to
<xsl:value-of select="persons/person[@role='grantee']"/></h3>
<xsl:apply-templates select="docText"/> <!-- docText contains text inside CDATA section -->
<div class="clear"/>
</article><hr />
</xsl:template>
<!-- all lines of text are parsed here and tagged with either <p> or <div> and blank lines discarded-->
<xsl:template match="docText">
<xsl:variable name="vLines" select="fn:analyze-string(., '\n')" />
<xsl:for-each select="$vLines/fn:non-match">
<xsl:choose>
<xsl:when test="starts-with(.,'[')">
<xsl:variable name="v2" select="fn:analyze-string(.,'\[(witness|signed|clerk|margin)\]')"/>
<div class="{fn:replace($v2/fn:match , '\[(.*?)\]' , '$1')}">{$v2/fn:non-match}</div>
</xsl:when>
<xsl:otherwise>
<p>
<xsl:call-template name="tReplaceDblApos">
<xsl:with-param name="pText" select="."/>
</xsl:call-template>
</p>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<!-- any 1 to 4 characters following two adjacent single quotes is tagged with <sup> without quotes-->
<xsl:template name="tReplaceDblApos">
<xsl:param name="pText"/>
<xsl:analyze-string select="$pText" regex="''([a-zA-Z]{{1,4}})">
<xsl:matching-substring>
<sup><xsl:value-of select="regex-group(1)"/></sup>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
对于完成此类标记的更好方法以及如何使其可扩展并完成例如列出的最后一步的任何建议,我将不胜感激。在过去的几个月里,我已经尝试过让这个过程更简单,这是迄今为止我得到的最接近的。对任何滥用术语、长示例和代码的新手状态表示歉意。
迈克尔