Guest pugg09 Posted July 19, 2007 Share Posted July 19, 2007 I've done a search through the archives - I couldn't find the answer. So, maybe someone could show me a link or something.I'm using Javascript with XML.All I want to do is count how many children of each particular node (like how many items in the Brass category, or how many categories in the Musician menu).I have the following bit of XML, any help would be great, thanks: <?xml version="1.0" encoding="iso-8859-1"?><barbonz><menu name="musicians" total="101"> <category name="brass" id="1" total="101"> <item name="baritone brass" id="1" total="101">baritone brass</item> <item name="cornet" id="2" total="102">cornet</item> <item name="euphonium" id="3" total="103">euphonium</item> </category> <category name="fretted instruments" id="2" total="102"> <item name="banjo" id="4" total="104">banjo</item> <item name="dulcimer" id="5" total="105">dulcimer</item> <item name="sitar" id="6" total="106">sitar</item> </category> </menu><menu name="teachers" total="102"> <category name="keyboards" id="3" total="103"> <item name="according" id="7" total="107">according</item> <item name="harpsicord" id="8" total="108">harpsicord</item> <item name="piano" id="9" total="109">piano</item> </category> <category name="other" id="4" total="104"> <item name="composition" id="10" total="110">composition</item> <item name="conducting" id="11" total="111">conducting</item> <item name="sitar" id="12" total="112">sitar</item> </category> </menu> Link to comment Share on other sites More sharing options...
aalbetski Posted July 19, 2007 Share Posted July 19, 2007 you can use the .length property of the node set. Here is a complete example using an XML data island (tested only with IE 6.0) <xml id="mm"><?xml version="1.0" encoding="iso-8859-1"?><barbonz> <menu name="musicians" total="101"> <category name="brass" id="1" total="101"> <item name="baritone brass" id="1" total="101">baritone brass</item> <item name="cornet" id="2" total="102">cornet</item> <item name="euphonium" id="3" total="103">euphonium</item> </category> <category name="fretted instruments" id="2" total="102"> <item name="banjo" id="4" total="104">banjo</item> <item name="dulcimer" id="5" total="105">dulcimer</item> <item name="sitar" id="6" total="106">sitar</item> </category> </menu> <menu name="teachers" total="102"> <category name="keyboards" id="3" total="103"> <item name="according" id="7" total="107">according</item> <item name="harpsicord" id="8" total="108">harpsicord</item> <item name="piano" id="9" total="109">piano</item> </category> <category name="other" id="4" total="104"> <item name="composition" id="10" total="110">composition</item> <item name="conducting" id="11" total="111">conducting</item> <item name="sitar" id="12" total="112">sitar</item> </category> </menu></barbonz></xml><script> var oXML = document.getElementById("mm") var iCount = oXML.selectNodes("//category[@name='brass']").length; document.write(iCount) iCount = oXML.selectNodes("//menu[@name='musicians']/category").length; document.write(iCount)</script> Link to comment Share on other sites More sharing options...
ashvini Posted July 22, 2007 Share Posted July 22, 2007 Hi,I will suggest you, if you wanna count only node gor for XSLT.Its easy and simple, few line code- for your XML code is:<?xml version='1.0'?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"> <xsl:for-each select="*" > <xsl:value-of select="count(*)"/> </xsl:for-each></xsl:template></xsl:stylesheet>Ouput: Menu Node : 2Modified it according to you which node you wanna to count? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.