重新排列层次结构 - 将元素的后续同级元素移动到该元素末尾之前

问题描述 投票:0回答:1

我需要重新排列层次结构(移动出现在段落后面、段落内部的列表):

From: <para updated="20240205"><text> ...  </text> ... </para><list type="num"> ... </list>
To: <para updated="20240205"><text> ... </text> ... <list type="num"> ... </list></para>

备注:

  • DTD 允许 para 包含列表,列表也可以包含 para
  • 该段落后面可能有多个列表需要移动到前一个段落的末尾

我对 XSL 比较陌生,不确定如何处理这个问题,因为它们目前是兄弟姐妹

我还没有尝试任何事情,因为我不知道如何解决这个问题。

谢谢

xml xpath xslt
1个回答
0
投票

使用例如

  <xsl:template match="*[para]">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:for-each-group select="*" group-starting-with="para">
        <xsl:choose>
          <xsl:when test="self::para">
            <xsl:copy>
              <xsl:apply-templates select="@*, node(), tail(current-group())"/>
            </xsl:copy>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="current-group()"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

我认为

tail
是 XPath 3/XSLT 3,因此如果您确实使用 XSLT 2 处理器,请改用
subsequence(current-group(), 2)

当然,推动一切通过

apply-templates
假设您已经为不想更改的节点设置了身份转换(模板),并为您想要更改的节点上的任何转换添加了模板。

© www.soinside.com 2019 - 2024. All rights reserved.