XSLT:处理TEI里程碑元素

时间:2020-02-06 14:35:06

标签: xml xslt tei

我的问题与XSLT with overlapping elements?有关-但是建议的解决方案不适用于我。

输入

我有一些这样编码的TEI-XML:

<delSpan spanTo="#abcbb6b8-b7bd-4b96-93c1-0a34500e12c0"/>
<lg>
    <l>some text that is deleted</l>
</lg>
<lg>
    <l>much more text</l>
    <l>another line of text</l>
</lg>
<anchor xml:id="abcbb6b8-b7bd-4b96-93c1-0a34500e12c0"/>

我想用XSL处理它,我的输出应该像这样:

<div class="delSpan">
    <div class="lg">
        <span>some text that is deleted</span>
    </div>
    <div class="lg">
        <span>much more text</span>
        <span>another line of text</span>
    </div>
</div>

XSLT 目前,我正在尝试使用以下模板:

<xsl:template match="tei:delSpan">
    <xsl:variable name="id">
        <xsl:value-of select="substring-after(@spanTo, '#')"/>
    </xsl:variable>
    <xsl:for-each-group select="*" group-ending-with="tei:anchor[@xml:id=$id]">
        <div class="delSpan">
            <xsl:apply-templates />
        </div>
    </xsl:for-each-group>
</xsl:template>

<xsl:template match="tei:lg">
    <div class="lg">
        <xsl:apply-templates/>
    </div>
</xsl:template>

<xsl:template match="tei:l">
    <span>
        <xsl:apply-templates/>
    </span>
</xsl:template>

但这只会产生以下输出:

<div class="lg">
        <span>some text that is deleted</span>
    </div>
    <div class="lg">
        <span>much more text</span>
        <span>another line of text</span>
    </div>

所以我问自己,是否存在任何通用且简单的解决方案来应对上述的里程碑元素和过程?

1 个答案:

答案 0 :(得分:1)

如果将for-each-group移到任何delSpan的父元素(或您希望对其应用换行的任何元素),则看起来像

  <xsl:template match="*[delSpan]">
      <xsl:for-each-group select="*" group-starting-with="delSpan">
          <xsl:choose>
              <xsl:when test="self::delSpan">
                  <xsl:variable name="id-ref" select="substring(@spanTo, 2)"/>
                  <div class="{local-name()}">
                      <xsl:for-each-group select="current-group() except ." group-ending-with="id($id-ref)">
                          <xsl:choose>
                              <xsl:when test="current-group()[last()] is id($id-ref)">
                                  <xsl:apply-templates select="current-group()[not(position() = last())]"/>
                              </xsl:when>
                              <xsl:otherwise>
                                  <xsl:apply-templates select="current-group()"/>
                              </xsl:otherwise>
                          </xsl:choose>
                      </xsl:for-each-group>
                  </div>
              </xsl:when>
              <xsl:otherwise>
                  <xsl:apply-templates select="current-group()"/>
              </xsl:otherwise>
          </xsl:choose>
      </xsl:for-each-group>
  </xsl:template>

https://xsltfiddle.liberty-development.net/pPJ9hEk/1