|
#1
|
|||
|
|||
|
XML Count Functionality
Hi All,
I am trying to learn XML on my own. I am trying to write a stylesheet which would take an XML document as input and generate another XML doxcument as output. The goal of the stylesheet is to count the number of elements in the xml document. For Example: If this is the xml file: <inventory> <room> <name>Office</name> <item> <name>Desk</name> <value>200</value> </item> <item> <name>Chair</name> <value>100</value> </item> <item> <name>Bookshelf</name> <value>300</value> </item> </room> </inventory> The output XML file should be: <results> <result name="inventory" count="1"/> <result name="room" count="1"/> <result name="name" count="4"/> <result name="item" count="3"/> <result name="value" count="3"/> </results> Can someone please help me with this problem... I would like to know what should be the logic that I should follow to write this stylesheet. If you guys give me a small snippet that would help me a lot too. Thanks, XML Rookie |
|
#2
|
|||
|
|||
|
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="xml"/>
<xsl:key name="only" match="//*" use="local-name()"/>
<xsl:template match="/">
<results>
<xsl:for-each select="//descendant::*[generate-id() = generate-id(key('only', local-name())[1])]">
<result>
<xsl:attribute name="name">
<xsl:value-of select="local-name(.)"/>
</xsl:attribute>
<xsl:variable name="kn" select="local-name(.)"></xsl:variable>
<xsl:attribute name="count">
<xsl:value-of select="count(//descendant::*[local-name(.)=$kn])"/>
</xsl:attribute>
</result>
</xsl:for-each>
</results>
</xsl:template>
</xsl:stylesheet>
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Try XML Junction | Tim | xml.announcements | 0 | 10-11-2001 05:00 PM |
| XML Startkabel Updates | xmlstartkabel | XML | 0 | 10-10-2001 05:07 AM |
| Data Junction Announces XML Junction 7.51 | Tim Frost | xml.announcements | 0 | 04-02-2001 11:53 AM |
| Check out Turbo XML: Free trial download | Extensibility | web.announcements | 0 | 07-20-2000 07:20 PM |
| Check out Turbo XML | Sales | xml.announcements | 0 | 07-20-2000 07:18 PM |