xslt 删除特定元素周围的白色 space(xml 到 xml)

xslt remove white space around specific elements (xml to xml)

我有很多 XML 文件,其中从数据库导出时通过缩进添加了某些白色 space,现在我希望在 XSLT 3.0 转换中将其删除,以便新 [=41] =] 输出。我想删除 <lb><pb> 周围导出引入的白色 space (在原始文件中,导出之前,这两个元素紧靠其他没有白色 space 的元素 -导出中的一个隐藏错误使它们缩进了)。

这是要转换的问题文件的示例:

<p xml:id="MS609-0783-LA" xml:lang="LA">
       <seg type="dep_event" xml:id="MS609-0783-1">
          <pb n="58r"/>
          <lb n="1"/>Item. 
          <date type="deposition_date" when="1245-07-07">Anno Domini M°CC°XL°V° Nonas Iulii</date><persName
            nymRef="#ber_r_baz-hg" role="dep">Ber. R.</persName> testis juratus dixit quod vidit
        <persName nymRef="#heretics_in_public" ref="her">hereticos</persName>.</seg>
</p>

此处是所需 XML 输出的示例:

<p xmlns="http://www.tei-c.org/ns/1.0" xml:id="MS609-0783-LA" xml:lang="LA">
    <seg type="dep_event" xml:id="MS609-0783-1"><pb n="58r"/><lb n="1"/>Item. 
        <date type="deposition_date" when="1245-07-07">Anno Domini M°CC°XL°V° Nonas Iulii</date> <persName
            nymRef="#ber_r_baz-hg" role="dep">Ber. R.</persName> testis juratus dixit quod vidit
        <persName nymRef="#heretics_in_public" ref="her">hereticos</persName>.</seg>
</p>

我天真地以为我可以这样定位它:

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

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

  <xsl:output method="xml"/>

  <xsl:template match="/">
        <xsl:apply-templates/>
   </xsl:template>
 
   <xsl:template match="text()[normalize-space(.) = '']">
       <xsl:choose>
           <xsl:when test="./following-sibling::tei:pb">
               <xsl:text/>
           </xsl:when>
           <xsl:when test="./following-sibling::tei:lb">
               <xsl:text/>
           </xsl:when>
           <xsl:otherwise>
               <xsl:text> </xsl:text>
           </xsl:otherwise>
       </xsl:choose>
   </xsl:template>
</xsl:stylesheet>

但它没有产生预期的结果:https://xsltfiddle.liberty-development.net/93nwMpi

理想情况下,我正在努力寻找一种解决方案,在 <pb> and/or [=] 之前或之后去除 any 空白 space 16=](当它们与其他元素邻接时),<seg> 或其后代中的任何位置。

非常感谢您的指点。

我不清楚您要删除哪些文本节点,但是

  <xsl:template match="tei:seg//text()[not(normalize-space())][following-sibling::node()[1][self::tei:pb | self::tei:lb]]"/>

会在 seg.

内去除白色 - space 后跟 pblb

当然,您可以将其扩展到那些元素前面的元素,例如

  <xsl:template match="tei:seg//text()[not(normalize-space())][preceding-sibling::node()[1][self::tei:pb | self::tei:lb] or following-sibling::node()[1][self::tei:pb | self::tei:lb]]"/>

如果基于匹配模式的简单阻塞还不够,您可能想尝试一下您对“邻接”的定义是否可以用 group-adjacentxsl:for-each-group 以某种方式表达,然后删除任何白色space 组中的节点,例如

  <xsl:template match="tei:seg[tei:pb | tei:lb] | tei:seg//*[tei:pb | tei:lb]">
      <xsl:copy>
        <xsl:for-each-group select="node()" group-adjacent="boolean(self::text()[not(normalize-space())]|self::tei:pb|self::tei:lb)">
          <xsl:choose>
            <xsl:when test="current-grouping-key() and current-group()[self::tei:pb|self::tei:lb]">
               <xsl:sequence select="current-group()[not(self::text())]"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="current-group()"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each-group>          
      </xsl:copy>
  </xsl:template>