Jump to content
Sign in to follow this  
Guest pugg09

counting children of a specific node index

Recommended Posts

Guest pugg09

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>

Share this post


Link to post
Share on other sites

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>

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...