XPath - select 节点通过分析以下兄弟姐妹

XPath - select nodes by analyzing following sibllings

我有以下 XML 文档,

<doc>
    <h1>aa</h1>
    <p>bb</p>
    <p>cc</p>

    <h1>dd</h1>
    <p>ee</p>
    <h2>ff</h2>
    <h2>gg</h2>

    <h1>jj</h1>
    <p>kk</p>
    <p>ll</p>

    <h1>hh</h1>
    <h2>ii</h2>
    <p>jj</p>
</doc>

我需要编写一个 XPath 查询以在出现另一个 <h1> 节点之前获取 <h1> 个存在以下兄弟 <h2> 个节点的节点。在这种情况下,应选择第 2 个和第 4 个 <h1> 节点。 (<h2> 个节点存在于另一个 <h1> 节点之前的后续同级中。

我尝试了几个小时,但想不出任何合适的解决方案来获取这些节点?

任何人都可以建议我该怎么做的方法吗?

Select h1 节点 h2 作为 h1 和 h2 节点中第一个跟随兄弟的 h1 节点

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

    <xsl:template match="/doc">
        <xsl:for-each select="h1[following-sibling::*[self::h1 or self::h2][1][self::h2]]">
            <xsl:copy-of select="."/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

只要找到h2并取前面的h1

 //h2/preceding-sibling::h1[1]

结果

Element='<h1>dd</h1>'
Element='<h1>hh</h1>'