0

As an extension to the question that solved here, how we can count number of frequency of each tag and attribute?

For example, for this document:

<a>
<apple color="red"/>
<banana color="yellow"/>
<sugar taste="sweet"/>
<cat size="small"/>
</a>

something like this result (preferably in two separate tables) is desired:

For tags: apple = 1, banana =1, sugar =1, cat=1

For attributes: color= 2, taste=1, size=1

5
  • 4
    This is a grouping question. Do a search, it's one of the most often asked questions here. Note that answers are different for XSLT 1.0 or 2.0. Commented Dec 30, 2014 at 8:19
  • I have researched a lot (here and in other forums), but the provided solutions were not applicable for this case (at least, I could not find the appropriate ones!). Anyway, I'm continue researching. Commented Dec 30, 2014 at 11:56
  • @EiliaAbraham, see this ANSWER, stackoverflow.com/a/19828481/3049413 Commented Dec 30, 2014 at 12:06
  • @Rudramuni TP, Thanks for that, it seems it answered my problem in reverse direction. So, it can be regarded as a good starting point to working on. Moreover, I'm currently considering this one Commented Dec 30, 2014 at 12:18
  • Also, it is very helpful: (xml.com/lpt/a/1010) Commented Dec 30, 2014 at 12:31

1 Answer 1

1

Try this:

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="/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>,&#160;</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:&#160;</xsl:text></xsl:if>
            <xsl:value-of select="concat(name(.), ' ', count(key('kEleName', name(.))))"/>
            <xsl:if test="following-sibling::*">
                <xsl:text>,&#160;</xsl:text>
            </xsl:if>

            <xsl:if test="position()=last()">
                <xsl:text>&#10;For attributes:&#160;</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>

OutPut:

For tags: apple 2, banana 1, sugar 1, cat 1 
For attributes: color 3, taste 1, size 1 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.