You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XSL 3精准匹配特定<date>节点的首个文本同级节点调整句号位置

问题解决:调整TEI中date元素的句号位置

输入TEI XML代码

<TEI xmlns="http://www.tei-c.org/ns/1.0" xml:id="MS609-1577">
    <teiHeader/>
    <text>
        <body>
            <ab xml:id="MS609-1577-LA" xml:lang="la">
                <seg type="dep_event" subtype="denial" xml:id="MS609-1577-1" sameAs="#MS609-1553"><lb break="y" n="24"/>Item. 
                <date type="deposition_date" sameAs="#MS609-1553" xml:id="MS609-1577_depdate">Anno <supplied>et die</supplied> predictis</date>.
                    <persName ref="#peire_guibert_asv-hg" role="dep">Petrus Guitberti</persName> 
                    testis juratus dixit idem per omnia quod 
                    <persName ref="#peire_bernart_asv-hg" role="ref">P<supplied reason="abbr-name">etrus</supplied>
                        Bernardi</persName>.<seg type="witnesses" sameAs="#MS609-1601"/></seg>
            </ab>
        </body>
    </text>
</TEI>

需求说明

将<date type="deposition_date">元素外部的句号,移至该元素内部最后子节点的位置:

  • 原片段:
<supplied>et die</supplied> predictis</date>.
  • 目标片段:
<supplied>et die</supplied> predictis.</date>

其余内容保持原样。

现有XSL的问题

现有XSL代码可将句号添加至目标元素的最后子节点,但第三个模板会对<date>的所有后续同级文本节点应用substring()处理,实际仅需针对<date>的首个同级文本节点(且该节点以句号开头)进行处理。

现有XSL代码:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:tei="http://www.tei-c.org/ns/1.0" 
    exclude-result-prefixes="tei"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output method="xml" indent="no"/>

  <xsl:template match="/">
        <xsl:apply-templates/>
  </xsl:template>
  
   <xsl:template match="tei:date[@type='deposition_date' and ./following-sibling::node()[1][. instance of text() and starts-with(.,'.')]]">
        <date xmlns="http://www.tei-c.org/ns/1.0">
          <xsl:copy-of select="./@*"/>
          <xsl:copy-of select="./(* | text())"/>
          <xsl:text>.</xsl:text>
        </date>
  </xsl:template>
  
   <xsl:template match="text()[starts-with(.,'.')][./preceding-sibling::tei:date[@type='deposition_date']]">
        <xsl:value-of select="substring(.,2)"/>
  </xsl:template>
  
</xsl:stylesheet>

修正后的XSL代码

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:tei="http://www.tei-c.org/ns/1.0" 
    exclude-result-prefixes="tei"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output method="xml" indent="no"/>

  <xsl:template match="/">
        <xsl:apply-templates/>
  </xsl:template>
  
   <xsl:template match="tei:date[@type='deposition_date' and ./following-sibling::node()[1][. instance of text() and starts-with(.,'.')]]">
        <date xmlns="http://www.tei-c.org/ns/1.0">
          <xsl:copy-of select="./@*"/>
          <xsl:copy-of select="./(* | text())"/>
          <xsl:text>.</xsl:text>
        </date>
  </xsl:template>
  
   <xsl:template match="text()[starts-with(.,'.')][./preceding-sibling::node()[1][self::tei:date[@type='deposition_date']]]">
        <xsl:value-of select="substring(.,2)"/>
  </xsl:template>
  
</xsl:stylesheet>

修正说明

修改第三个模板的匹配条件:将./preceding-sibling::tei:date[@type='deposition_date']替换为./preceding-sibling::node()[1][self::tei:date[@type='deposition_date']],这样只会匹配紧接在目标date元素之后的首个文本节点(且该节点以句号开头),避免误处理其他后续文本节点。

内容的提问来源于stack exchange,提问作者jbrehr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 03:24:25