当前元素与下一当前元素之间选择所有元素(Select all of an element betwe

2019-10-18 01:48发布

使用XSLT 1.0(优选地),如何可以选择所有其中电流元件和电流元件发生下一次之间发生元件的?

说我有这个XML(编辑):

<root>
    <heading_1>Section 1</heading_1>
    <para>...</para>
    <list_1>...</list_1>
    <heading_2>Section 1.1</heading_2>
    <para>...</para>
    <heading_3>Section 1.1.1</heading_3>
    <para>...</para>
    <list_1>...</list_1>
    <heading_2>Section 1.2</heading_2>
    <para>...</para>
    <footnote>...</footnote>
    <heading_1>Section 2</heading_1>
    <para>...</para>
    <list_1>...</list_1>
    <heading_2>Section 2.1</heading_2>
    <para>...</para>
    <list_1>...</list_1>
    <list_2>...</list_2>
    <heading_3>Seciton 2.1.1</heading_3>
    <para>...</para>
    <heading_2>Section 2.2</heading_2>
    <para>...</para>
    <footnote>...</footnote>
</root>

当处理heading_1我想选择所有的heading_2我处理方位与未来之间heading_1 。 同样选择heading_3处理时heading_2等你得到的图片。

Answer 1:

您可以使用此:

following-sibling::heading_2[generate-id(preceding-sibling::heading_1[1]) = 
                             generate-id(current())]

工作示例:

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

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

  <xsl:template match="/*">
    <xsl:copy>
      <xsl:apply-templates select="heading_1" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="heading_1">
    <xsl:copy>
      <xsl:apply-templates
        select="following-sibling::heading_2[generate-id(
                                                preceding-sibling::heading_1[1]) = 
                                             generate-id(current())]" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="heading_2">
    <xsl:copy>
      <xsl:apply-templates
        select="following-sibling::heading_3[generate-id(
                                                preceding-sibling::heading_2[1]) = 
                                             generate-id(current())]" />
    </xsl:copy>
  </xsl:template>    
</xsl:stylesheet>

当你的样品输入运行结果:

<root>
  <heading_1>
    <heading_2>
      <heading_3>...</heading_3>
    </heading_2>
    <heading_2 />
  </heading_1>
  <heading_1>
    <heading_2>
      <heading_3>...</heading_3>
    </heading_2>
    <heading_2 />
  </heading_1>
</root>


Answer 2:

尝试下面的XPath处理标题1时选择heading2。

(/root/heading_1/following-sibling::heading_1/preceding-sibling::heading_2) | (/root/heading_1[preceding-sibling::heading_1]/following-sibling::heading_2)


文章来源: Select all of an element between the current element and the next of the current element