XML ドキュメントを、値が前の兄弟の属性に基づくリストに変換しようとしています。
サンプル XML:
<myRoot>
<Person>Craig</Person>
<Person rank="10">Woody</Person>
<Person>Brian</Person>
<Person>Michael</Person>
<Person rank="20">Emily</Person>
<Person>Chris</Person>
</myRoot>
私が欲しいもの:
<myNewRoot>
<Index>1: Craig</Index>
<Index>10: Woody</Index>
<Index>11: Brian</Index>
<Index>12: Michael</Index>
<Index>20: Emily</Index>
<Index>21: Chris</Index>
</myNewRoot>
@rank 属性を持つ最後の先行兄弟と現在のノードとの間の距離を特定できず、行き詰まりました。
ここに私の現在のスタイルシートがあります
<xsl:template match="Person">
<xsl:element name="Index">
<xsl:choose>
<xsl:when test="./@rank">
<xsl:value-of select="./@rank"/>
</xsl:when>
<xsl:when test="preceding-sibling::Person[@rank]">
<xsl:value-of select="count(.|preceding-sibling::*[. > current()/preceding-sibling::Person[@rank][1]]) + preceding-sibling::Person[@rank][1]/@rank"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="position()"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>: </xsl:text>
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
count() 機能を動作させることができず、結局は
<myNewRoot>
<Index>1: Craig</Index>
<Index>10: Woody</Index>
<Index>11: Brian</Index>
<Index>11: Michael</Index>
<Index>20: Emily</Index>
<Index>21: Chris</Index>
</myNewRoot>