选择当前元素和当前元素的下一个元素之间的所有元素 [英] Select all of an element between the current element and the next of the current element
本文介绍了选择当前元素和当前元素的下一个元素之间的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 XSLT 1.0(最好),如何选择出现在当前元素和下一次当前元素出现之间的所有元素?
Using XSLT 1.0 (preferably), How can I select all of an element which occurs between the current element and the next time the current element occurs?
假设我有这个 XML(已编辑):
Say I have this XML (edited):
<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_1
之间的所有heading_2
.在处理 heading_2
等时选择 heading_3
相同,你会得到图片.
When processing heading_1
I want to select all of heading_2
between the heading I am processing and the next heading_1
. The same for selecting heading_3
when processing heading_2
etc. you get the picture.
推荐答案
你可以使用这个:
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>
在您的示例输入上运行时的结果:
Result when run on your sample input:
<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>
这篇关于选择当前元素和当前元素的下一个元素之间的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文