p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   How to count total number of nodes in an xml file? (http://p2p.wrox.com/showthread.php?t=16755)

nimesh August 12th, 2004 01:53 AM

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"/>

pgtips August 12th, 2004 03:26 AM

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.

All times are GMT -4. The time now is 04:44 AM.

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