Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old August 12th, 2004, 01:53 AM
Registered User
Join Date: Aug 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to count total number of nodes in an xml file?


Just wondering if anyone can help me.

I need to to write a simple XSLT script which returns the total number of nodes within a sample XML file.
I tried running the following code, but it returns 30 nodes, instead of 20 for the following sample file.
Any ideas how to amend the code so it returns the correct node count?


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:value-of select="count(//node())"/>

<?xml version="1.0" encoding="UTF-8"?>

<FiML version="0-7">
<fiSourcePartyReference href="SOURCE"/>

Old August 12th, 2004, 03:26 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post

Well, it all depends what you think a node is. The node() test matches all types of nodes, i.e. comment, PI, element and text nodes, so if you're getting the "wrong" answer, then you're using the wrong test!

I don't see how you get 30 from the sample XML you posted, nor do I see why you expect 20. I suggest you look at the alternatives to node() at http://www.w3.org/TR/xpath and decide which one(s) you want to count.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to Count number of lines in a file lawsoncobol Access VBA 2 July 19th, 2007 04:28 AM
count the number of nodes in an XML doc crmpicco Perl 3 December 8th, 2006 08:30 AM
count the TOTAL number of segments crmpicco Classic ASP Basics 2 February 1st, 2005 05:03 AM
Getting Total Page Count eman2a Crystal Reports 0 August 11th, 2004 10:53 AM
Sub Total, Count discriminating dkaco BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 July 23rd, 2004 12:04 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.