gpt4 book ai didi

html - 使用多个步骤使用 XSLT 2.0/3.0 将 CDATA 中的纯文本解析为 html。途中

转载 作者:行者123 更新时间:2023-11-28 00:54:53 25 4
gpt4 key购买 nike

我有一个使用 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 中的文本采取的一些步骤

  1. 使用\n 新行标记所有行
  2. 以方括号中的单词开头的行(例如 [witness])标记为 <div>在括号中使用类(例如 <div class="witness">rest of line</div> )
  3. 其余行标记为 <p>标签
  4. 消除所有空行
  5. 扫描 <div> 中的文字和 <p>上面的文本节点用于进一步处理:
  6. 找到任何一对单引号(即成对的撇号)后跟 1 到 4 个大写或小写字母并放在 <sup></sup> 中(例如,第 25 位变为 25<sup>th</sup> )
  7. 相邻组 <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>
  8. 根据需要添加标记。

我已经通过第 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com