您可以使用一個鍵來計算排序指令中的屬性。含有樣式表中的以下內容:
<xsl:key name="p" match="property" use="@agency"/>
<xsl:template match="/immo">
<result>
<xsl:for-each select="agency">
<xsl:sort select="count(key('p', @name))"/>
<res id="{ @name }" count="{ count(key('p', @name)) }"/>
</xsl:for-each>
</result>
</xsl:template>
當施加到以下輸入:
<immo>
<agency name="a"/>
<agency name="b"/>
<agency name="c"/>
<property agency="a"/>
<property agency="a"/>
<property agency="a"/>
<property agency="b"/>
<property agency="b"/>
<property agency="b"/>
<property agency="b"/>
<property agency="c"/>
</immo>
產生以下輸出:
<result>
<res id="c" count="1"/>
<res id="a" count="3"/>
<res id="b" count="4"/>
</result>
如果要以相反的順序,對xsl:sort
使用order="descending"
。
編輯:如果property/@agency
可以有幾個機構的數字,由空格分開,下面的解決方案的工作,而不是(不使用鍵,雖然,再次選擇,每家機構在文檔中的所有屬性):
<xsl:template match="/immo">
<result>
<xsl:for-each select="agency">
<xsl:sort select="count(//property[tokenize(@agency, '\s+') = current()/@name])"/>
<res id="{ @name }"/>
</xsl:for-each>
</result>
</xsl:template>
請(1)向我們展示了XML輸入的**完成**例如,和(2)表示XSLT 1.0或2.0。 –