2

我有一个使用 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>

对于完成此类标记的更好方法以及如何使其可扩展并完成例如列出的最后一步的任何建议,我将不胜感激。在过去的几个月里,我已经尝试过让这个过程更简单,这是迄今为止我得到的最接近的。对任何滥用术语、长示例和代码的新手状态表示歉意。

迈克尔

4

1 回答 1

2

这是尝试直接在使用该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>
于 2017-07-31T09:18:37.607 回答