嘗試這種情況:
XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:key name="kEleName" match="*" use="local-name()"/>
<xsl:key name="kAttribName" match="@*" use="local-name()"/>
<xsl:variable name="var1">
<xsl:for-each select="https://stackoverflow.com/a/*/@*[generate-id() = generate-id(key('kAttribName', name()))]">
<xsl:value-of select="concat(name(.), ' ', count(key('kAttribName', name(.))))"/>
<xsl:if test="not(position()=last())">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:template match="/*">
<xsl:apply-templates select="*[generate-id() = generate-id(key('kEleName', name()))]"/>
</xsl:template>
<xsl:template match="*">
<xsl:if test="position()=1"><xsl:text>For tags: </xsl:text></xsl:if>
<xsl:value-of select="concat(name(.), ' ', count(key('kEleName', name(.))))"/>
<xsl:if test="following-sibling::*">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:if test="position()=last()">
<xsl:text> For attributes: </xsl:text>
<xsl:value-of select="$var1"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
XML:
<a>
<apple color="red"/>
<apple color="green"/>
<banana color="yellow"/>
<sugar taste="sweet"/>
<cat size="small"/>
</a>
輸出:
For tags: apple 2, banana 1, sugar 1, cat 1
For attributes: color 3, taste 1, size 1
這是一個*分組*問題。做一個搜索,這是這裏最常見的問題之一。請注意,對於XSLT 1.0或2.0,答案是不同的。 –
我已經研究了很多(在這裏和其他論壇),但提供的解決方案不適用於這種情況(至少,我找不到合適的!)。無論如何,我仍在繼續研究。 – Eilia
@EiliaAbraham,看到這個答案,http://stackoverflow.com/a/19828481/3049413 –