Top DevX Stories
Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Search the forums:

Go Back   DevX.com Forums > DevX Developer Forums > XML

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 10-23-2009, 10:00 PM
xmlrookie123 xmlrookie123 is offline
Registered User
 
Join Date: Oct 2009
Posts: 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
Reply With Quote
  #2  
Old 10-29-2009, 03:12 PM
xml-looser xml-looser is offline
Registered User
 
Join Date: May 2009
Posts: 38
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>
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

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


All times are GMT -4. The time now is 12:42 AM.


Sponsored Links



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.