html - XML - XSLT 到 HTML 转换 - 消除特定行/回车

标签 html xml xslt tei

我正在使用 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 hereticos 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>.

最佳答案

在您对自己帖子的回答中,您写道您“不明白为什么这会产生影响”。让我尝试提供帮助:您需要避免解析 choicepersName[choice] 中的所有空白子节点,即 之间的空格, 例如。这些不是您内容的一部分,而只是 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>

最后一句话:注意你的架构。如果允许 persNamechoice 之外包含非空白文本(通常是这样),您应该区别对待。只有当 persName 始终包含 choiceregorig 时,这里的解决方案才有效。

关于html - XML - XSLT 到 HTML 转换 - 消除特定行/回车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46269107/

相关文章:

java - 第一次使用 XML,这个错误是什么意思?

html - 参数和变量必须位于模板的顶部吗?

debugging - XSLT 1.0 中的输出上下文节点(完整路径)?

html - Docbook XSL – HTML 自定义层 – 节标题到标题

javascript - 选择框的 html 选项的动态设置不会出现在 select html 中

html - 如何使用 native 中的 ionic 菜单为 ionic 中的侧边菜单制作圆 Angular

html - Sublime Text 标签/退出标签?

java - 从 dom 文档创建 json

html - CSS:表格中每一行的水平滚动

c# - 非序列化无效