これは、単一の XPath 式であるソリューションです。
2 つのノードセット と の交点にケイシアンの式を使用:$ns1
$ns2
$ns1[count(. | $ns2) = count($ns2)]
現在のノードに続く兄弟$ns1
のノードセットで単純に置き換え、次のノードの前にある兄弟のノードセットで置き換えます。<a>
<b>
$ns2
<a>
<b>
これを使用した完全な変換は次のとおりです。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="*/b"/>
</xsl:template>
<xsl:template match="b">
At: <xsl:value-of select="."/>
<xsl:variable name="vNextB" select="following-sibling::b[1]"/>
<xsl:variable name="vA-sAfterCurrentB" select="following-sibling::a"/>
<xsl:variable name="vA-sBeforeNextB" select=
"$vNextB/preceding-sibling::a
|
$vA-sAfterCurrentB[not($vNextB)]
"/>
<xsl:copy-of select=
"$vA-sAfterCurrentB
[count(.| $vA-sBeforeNextB)
=
count($vA-sBeforeNextB)
]
"/>
</xsl:template>
</xsl:stylesheet>
この変換が次の XML ドキュメントに適用される場合:
<t>
<img/>
<b>First</b>
<br />  
<img/>  
<a href="/first-href">First Href</a> - 19:30
<br />
<img/>
<b>Second</b>
<br />
<img/>  
<a href="/second-href">Second Href</a> - 19:30
<br />
<img/> 
<a href="/third-href">Third Href</a> - 19:30
<br />
</t>
正しい結果が生成されます。
At: First <a href="/first-href">First Href</a>
At: Second <a href="/second-href">Second Href</a>
<a href="/third-href">Third Href</a>