来自:
<Play>
<Character key="Horatio">[...]</Character>
<Character key="Hamlet">[...]</Character>
<Character key="Ghost">[...]</Character>
<Character key="Hamlet">[...]</Character>
<Character key="Polonius">[...]</Character>
<Character key="Hamlet">[...]</Character>
<Character key="Hamlet">[...]</Character>
<Character key="Queen">[...]</Character>
<Character key="Horatio">[...]</Character>
</Play>
我想输出一个副本,该副本将作为附加属性传递每个元素在共享相同@key的位置之间的位置,例如:
<Play>
<Character key="Horatio" token="1">[...]</Character>
<Character key="Hamlet" token="1">[...]</Character>
<Character key="Ghost" token="1">[...]</Character>
<Character key="Hamlet" token="2">[...]</Character>
<Character key="Polonius" token="1">[...]</Character>
<Character key="Hamlet" token="3">[...]</Character>
<Character key="Hamlet" token="4">[...]</Character>
<Character key="Queen" token="1">[...]</Character>
<Character key="Horatio" token="2">[...]</Character>
</Play>
由于这是对早期订购问题(XSLT – Pass attributes to create parent elements, then order filtered results)的一种跟进,我尝试使用Tim C的答案作为我的基础并使用xsl:number与它一起使用,但我没有没成功。
非常感谢!
答案 0 :(得分:0)
简单的解决方案是按键对字符进行分组,并在每个组中使用它们的位置。
如果您需要保留原始订单(或通过其他属性创建不同的订单),请尝试其他方法:
XSLT 2.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:key name="grp" match="Character" use="@key" />
<xsl:template match="Character">
<Character key="{@key}" token="{index-of(key('grp', @key)/generate-id(), generate-id())}">
<xsl:apply-templates/>
</Character>
</xsl:template>
</xsl:stylesheet>