在一個簡單的列表(不是組)這個XPath 1.0表達式:
/persons/person[preceding-sibling::person[@name='f']]
[following-sibling::person[@name='g']]
如果有團體,這個XPath 1.0表達式:
/persons
/person
[count(.|preceding-sibling::person[@name='f'][1]
/following-sibling::person[@name='g'][1]
/preceding-sibling::*) =
count(preceding-sibling::person[@name='f'][1]
/following-sibling::person[@name='g'][1]
/preceding-sibling::*)]
在XPath 2.0你可以使用對於兩種情況:
/persons/person[preceding-sibling::person[@name='f'][1]
/following-sibling::person[@name='g'][1] >> .]
在XSLT 1.0,你可以使用:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kPersonByMarks" match="person"
use="concat(generate-id(
preceding-sibling::person[@name='f'][1]),
'+',
generate-id(
following-sibling::person[@name='g'][1]))"/>
<xsl:template match="person[@name='f']">
<xsl:copy-of select="key('kPersonByMarks',
concat(generate-id(),'+',
generate-id(
following-sibling::person
[@name='g'][1])))"/>
</xsl:template>
</xsl:stylesheet>
輸出:
<person name="b"></person>
編輯:正確的 「包括」 表達了用於XPath 1.0和XPath 2.0
+1以獲得正確的答案。 – 2010-12-13 18:39:14