XLST 1.0 сплит-узел - PullRequest
       5

XLST 1.0 сплит-узел

0 голосов
/ 01 февраля 2020

Я хочу разделить этот узел на <foo>:

  <p>
    <span>This is before foo.</span>
    <foo>inside foo</foo>
    <span>This is after foo.</span>
  </p>

Я хочу получить результат

  <p>
    <span>This is before foo.</span>
  </p>
  <foo>inside foo</foo>
  <p>
    <span>This is after foo.</span>
  </p>

Но я получаю

  <p>
    <span>This is before foo.</span>
    <p>
      <span>This is before foo.</span>
    </p>
    <foo>inside foo</foo>
    <p>
      <span>This is after foo.</span>
    </p>
    <span>This is after foo.</span>
  </p>

И вот таблица стилей, которую я пытаюсь использовать:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" indent="yes" />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="p">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>

    <xsl:template match="p/foo">
        <xsl:element name="p">
            <xsl:apply-templates select="preceding-sibling::node()"/>
        </xsl:element>

        <foo>
            <xsl:apply-templates/>
        </foo>

        <xsl:element name="p">
            <xsl:apply-templates select="following-sibling::node()"/>
        </xsl:element>
    </xsl:template>

</xsl:transform>

Как мне избавиться от этого дополнительного вывода?

Редактировать: Дать более реалистичный пример c. См http://xsltransform.net/pNP88xQ/2

1 Ответ

2 голосов
/ 01 февраля 2020

- отредактировано в ответ на ваш отредактированный вопрос -

Это непростая задача для выполнения sh в XSLT 1.0 (это намного проще сделать в XSLT 2.0, используя xsl:for-each-group).

Одним из возможных подходов является метод, известный как рекурсия брата :

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()" name="identity">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="p">
    <!-- start sibling recursion -->
    <xsl:apply-templates select="node()[1]"/>
</xsl:template>

<!-- first node in a group -->
<xsl:template match="p/node()[not(self::foo)]">
    <p>
        <xsl:call-template name="identity"/>
        <!-- collect the following node in this group -->
        <xsl:apply-templates select="following-sibling::node()[1][not(self::foo)]" mode="collect"/>
    </p>
    <!-- continue recursion with the following divider -->
    <xsl:apply-templates select="following-sibling::foo[1]"/>
</xsl:template>

<!-- other nodes in a group -->
<xsl:template match="node()" mode="collect">
    <xsl:call-template name="identity"/>
    <!-- collect the following node in this group -->
    <xsl:apply-templates select="following-sibling::node()[1][not(self::foo)]" mode="collect"/>
</xsl:template>

<xsl:template match="foo">
    <xsl:call-template name="identity"/>
    <!-- restart sibling recursion -->
    <xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>

</xsl:stylesheet>
...