【问题标题】:XLST 1.0 Split nodeXSLT 1.0 拆分节点
【发布时间】:2020-02-01 15:51:12
【问题描述】:

我想在<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>

我怎样才能摆脱那些额外的输出?

编辑:举一个更现实的例子。见http://xsltransform.net/pNP88xQ/2

【问题讨论】:

  • 这是一个与编辑前完全不同的问题。 p 中可以有多个 foo 元素吗?
  • 不止一个 foo:我没有想到,但是有可能。

标签: xslt-1.0


【解决方案1】:

-- 已针对您已编辑的问题进行了编辑--

这在 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>

【讨论】:

  • 不抱歉,这给了&lt;p&gt; &lt;p&gt;before &lt;/p&gt; &lt;foo&gt;inside&lt;/foo&gt; &lt;p&gt; after&lt;/p&gt; &lt;/p&gt; 另外,我的示例被简化了,“之前”和“之后”部分可以包含子节点。
  • 不,这不是通过应用上述方法收到的结果 - 请参阅:xsltfiddle.liberty-development.net/3NSTbeZ 此外,其他节点保留 - 请参阅:xsltfiddle.liberty-development.net/3NSTbeZ/1
  • 你说得对,对不起。然而,我意识到我确实过于简化了我的例子。我用更真实的帖子编辑了我的原始帖子。很抱歉造成混乱。
猜你喜欢
  • 2012-09-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
  • 1970-01-01
  • 2011-11-17
  • 2021-11-29
  • 2021-03-14
  • 2014-06-10
相关资源
最近更新 更多