我有一个使用 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>
如果有任何关于完成此类标记的更好方法以及如何使其可扩展并完成所列出的最后一步的建议,我将不胜感激。在过去的几个月里,我不断地尝试让这个过程变得更简单,这是迄今为止我最接近的一次。 对于任何术语误用、冗长的示例以及代码的新手状态,我们深表歉意。
迈克尔
最佳答案
这里尝试直接对使用 tokenize
函数标记的行进行分组:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs math mf"
version="3.0">
<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:for-each-group select="tokenize(., '\n')[normalize-space()]" group-adjacent="string(analyze-string(., '^\[(witness|signed|clerk|margin)\]')//*:match/*:group)">
<xsl:choose>
<xsl:when test="current-grouping-key() and current-group()[2]">
<div class="{current-grouping-key()}-outer">
<xsl:apply-templates select="current-group()" mode="wrap-div">
<xsl:with-param name="class" select="current-grouping-key()"/>
</xsl:apply-templates>
</div>
</xsl:when>
<xsl:when test="current-grouping-key()">
<xsl:apply-templates select="current-group()" mode="wrap-div">
<xsl:with-param name="class" select="current-grouping-key()"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()" mode="wrap-p"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
<xsl:template match=".[. instance of xs:string]" mode="wrap-div">
<xsl:param name="class"/>
<div class="{$class}">
<xsl:value-of select="replace(., '^\[.*?\]', '')"/>
</div>
</xsl:template>
<xsl:template match=".[. instance of xs:string]" mode="wrap-p">
<p>
<xsl:sequence select="mf:rep-quotes(.)"/>
</p>
</xsl:template>
<xsl:function name="mf:rep-quotes">
<xsl:param name="input" as="xs:string"/>
<xsl:analyze-string select="$input" 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:function>
</xsl:stylesheet>
我得到的输出是
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<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>
<article>
<h3>Jane Doe to
Bob Jones
</h3>
<p>[page 001] 1</p>
<div class="margin"> Person1 to Person2</div>
<p>This Indenture made this x<sup>th</sup> Day of y in the year z Between person1, grantor, of place1 to person2, grantee,
of place2 for 5 dollars ... the s<sup>d</sup> person1 to s<sup>d</sup> person2 ... signed under my hand.
</p>
<p>Witnesses present</p>
<div class="signed"> Mrs. Jane Doe (seal)</div>
<div class="witness-outer">
<div class="witness"> Mr. Witness1</div>
<div class="witness"> Ms. Witness1</div>
</div>
<p>Court office month x<sup>th</sup> year
</p>
<p>I do hereby certify that ... and is thereon truly admitted to Record</p>
<div class="clerk"> John G. Reynolds DCCC</div>
<div class="clear"></div>
</article>
<hr>
<article>
<h3>John Doe to
Bob Jones
</h3>
<p>[page 002] 2</p>
<div class="margin"> Person3 to Person4</div>
<p>This Indenture made this x<sup>th</sup> Day of y in the year z Between person1, grantor, of place1 to person2, grantee, of
place2 for 5 dollars ... the s<sup>d</sup> person1 to s<sup>d</sup> person2 ... signed under my hand.
</p>
<p>Witnesses present</p>
<div class="signed"> Mr. John Doe (seal)</div>
<div class="witness-outer">
<div class="witness"> Mr. Witness1</div>
<div class="witness"> Ms. Witness1</div>
</div>
<p>[page 003] 3</p>
<p>Court office month x<sup>th</sup> year
</p>
<p>I do hereby certify that ... and is thereon truly admitted to Record</p>
<div class="clerk"> John G. Reynolds DCCC</div>
<div class="clear"></div>
</article>
<hr>
</body>
</html>
关于html - 使用 XSLT 2.0/3.0 使用多个步骤将 CDATA 中的纯文本解析为 html。部分路途,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45407801/