3

我正在使用 XSLT 3.0 将 XML 文件输出到 HTML,但在消除逗号和句点之前的空格时遇到了麻烦。下面是我遇到的确切问题的一个示例:XML 中有行/回车,这些回车正在 HTML 中重现。通常这不是问题,因为浏览器会将空白折叠为一个空白;但是,正如您在下面的示例中看到的那样,它在逗号和句点之前保留了一个空格。

(关于 XML 的注意事项:这是中世纪手稿的文本编码,因此其中可以包含各种元素,并且它可以嵌套在不同级别的其他元素中)。

XML:

           <persName>
              <choice>
                 <orig>ar. p<hi rend="sup">a</hi>der</orig>
                 <reg>Arnaldum Prader</reg>
              </choice>
           </persName> et socium eius hereticos et vidit ibi cum eis <persName>
              <choice>
                 <orig>P. barrau</orig>
                 <reg>Poncium Barrau</reg>
              </choice>
           </persName>, <persName>
              <choice>
                 <orig>Iordanetū del maſ</orig>
                 <reg>Iordanetum del Mas</reg>
              </choice>
           </persName>, <persName>
              <choice>
                 <orig>Iordanū de quiders</orig>
                 <reg>Iordanum de Quiders</reg>
              </choice>
           </persName> et <persName>
              <choice>
                 <orig>W. Vitał</orig>
                 <reg>Willelmum Vitalis</reg>
              </choice>
           </persName> predictum et <persName>
              <choice>
                 <orig>ux̄ dc̄ī W. Vitał</orig>
                 <reg>uxor dicti Willelmi Vitalis</reg>
              </choice>
           </persName>.

XML 模板:

<!-- format super/sub scripts -->
<xsl:template match="tei:hi" name="template_supersub">
    <xsl:choose>
        <xsl:when test="@rend ='sup'"><sup class="subsup"><xsl:apply-templates/></sup></xsl:when>
        <xsl:when test="@rend ='sub'"><sub class="subsup"><xsl:apply-templates/></sub></xsl:when>
    </xsl:choose> 
</xsl:template>

<!-- parse persName into <spans> -->
<xsl:template match="tei:persName/tei:choice/tei:reg">
    <span class="interpretive"><xsl:apply-templates/></span>
</xsl:template>

<xsl:template match="tei:persName/tei:choice/tei:orig">
    <span class="diplomatic"><xsl:apply-templates/></span>
</xsl:template>

当前的 HTML 输出:

     <span class="diplomatic">ar. p<sup class="subsup">a</sup>der</span>
     <span class="interpretive">Arnaldum Prader</span>

      et socium eius hereticos et vidit ibi cum eis 

     <span class="diplomatic">P. barrau</span>
     <span class="interpretive">Poncium Barrau</span>

     , 

     <span class="diplomatic">Iordanetū del maſ</span>
     <span class="interpretive">Iordanetum del Mas</span>

     , 

     <span class="diplomatic">Iordanū de quiders</span>
     <span class="interpretive">Iordanum de Quiders</span>

      et 

     <span class="diplomatic">W. Vitał</span>
     <span class="interpretive">Willelmum Vitalis</span>

      predictum et 

     <span class="diplomatic">ux̄ dc̄ī W. Vitał</span>
     <span class="interpretive">uxor dicti Willelmi Vitalis</span>

     .

最终,有问题的输出:

Arnaldum Prader et socium eius heticos et vidit ibi cum eis Poncium Barrau, Iordanetum del Mas, Iordanum de Quiders et Willelmum Vitalis predictum et uxor dicti Willelmi Vitalis。

strip-space、replace()、translate() 的各种组合都没有针对这个问题。它们通常会导致元素之间的每个空白空间都崩溃。

理想情况下,我希望逗号和句点前没有空格,逗号或句点后有一个空格。但我找不到解决这个问题的机制,更不用说破解了。谢谢。

所需的 HTML 输出:

 <span class="diplomatic">ar. p<sup class="subsup">a</sup>der</span>
 <span class="interpretive">Arnaldum Prader</span> et socium eius 
 hereticos et vidit ibi cum eis <span class="diplomatic">P. 
 barrau</span><span class="interpretive">Poncium Barrau</span>, <span 
 class="diplomatic">Iordanetū del maſ</span><span 
 class="interpretive">Iordanetum del Mas</span>, <span 
 class="diplomatic">Iordanū de quiders</span><span 
 class="interpretive">Iordanum de Quiders</span> et <span 
 class="diplomatic">W. Vitał</span><span class="interpretive">Willelmum 
 Vitalis</span> predictum et <span class="diplomatic">ux̄ dc̄ī W. 
 Vitał</span><span class="interpretive">uxor dicti Willelmi 
 Vitalis</span>.
4

2 回答 2

1

在您对自己的帖子的回答中,您写道您“不明白为什么这会有所作为”。让我尝试提供帮助:您需要避免解析内部choicepersName[choice]不被解析的所有空白子节点,例如 <choice> 和 <orig> 之间的空格。这些不是您的内容的一部分,而只是 TEI 结构的一部分,必须忽略。当您使用 TEI 时,这个问题会在不同层面经常出现。

这里的这些模板应该演示如何以更“理解”的方式涵盖这个问题。您可以仅显式命名输出所需的元素,而不是应用所有模板(因此包括文本节点)。

<xsl:template match="tei:choice">
    <xsl:apply-templates select="tei:reg"/>
    <xsl:apply-templates select="tei:orig"/>
</xsl:template>

<xsl:template match="tei:persName[tei:choice]">
    <xsl:apply-templates select="tei:choice"/>
</xsl:template>

最后一句话:注意你的架构。如果persName允许在外部包含非空白文本choice(通常是这样),则应区别对待。此处的解决方案仅在persName始终包含choicewithreg和时才有效orig

于 2017-10-19T13:09:36.107 回答
0

发布对我自己的问题的回复,以避免冗长复杂的帖子。

我调整了这个 XSL:

<!-- parse persName into <spans> -->
<xsl:template match="tei:persName/tei:choice/tei:reg">
    <span class="interpretive"><xsl:apply-templates/></span>
</xsl:template>

<xsl:template match="tei:persName/tei:choice/tei:orig">
    <span class="diplomatic"><xsl:apply-templates/></span>
</xsl:template>

对于这个 XSL:

<!-- parse persName into <spans> -->
<xsl:template match="tei:persName">
<span class="interpretive"><xsl:apply-templates select="tei:choice/tei:reg"/></span><span class="diplomatic"><xsl:apply-templates select="tei:choice/tei:orig"/></span>
</xsl:template>

现在它完全根据需要导出 HTML。没有对 XSL 文件进行其他调整。我不明白为什么这会有所不同,但这是一个很大的不同。

新的 HTML:

 <span class="interpretive">Arnaldum Prader</span><span 
 class="diplomatic">ar. p<sup class="subsup">a</sup>der</span> et 
 socium eius hereticos et vidit ibi cum eis <span 
 class="interpretive">Poncium Barrau</span><span class="diplomatic">P. 
 barrau</span>, <span class="interpretive">Iordanetum del Mas</span>
 <span class="diplomatic">Iordanetū<span class="line_num diplomatic">
 <span class="interpretive"> </span>del maſ</span>, <span 
 class="interpretive">Iordanum de Quiders</span><span 
 class="diplomatic">Iordanū de quiders</span> et <span 
 class="interpretive">Willelmum Vitalis</span><span 
 class="diplomatic">W. Vitał</span> predictum et <span 
 class="interpretive">uxor dicti Willelmi Vitalis</span><span 
 class="diplomatic">ux̄ dc̄ī W. Vitał</span>.
于 2017-09-18T11:25:41.267 回答