Solved

XPath -count no. of specific tag occurences

Posted on 2004-09-25
2
627 Views
Last Modified: 2012-05-05
I am using libxml2 library in C to parse an xml document.  I am trying to count the no. of tags <Book>.  I tried using
"count(//Book)".  But I want the number rather than the node-set.   Is there an xpath expression to do this?

<?xml version="1.0" ?>
- <Book List>
- <Book>
  <Name>book1</Name>
  <Author>author1</Author>
  <Price>15.00</Price>  
  </Book>
- <Book>
  <Name>book2</Name>
  <Author>author2</Author>
  <Price>10.00</Price>  
  </Book>
  </Book List>

Thanks
0
Comment
Question by:inquisitive123
2 Comments
 
LVL 6

Assisted Solution

by:Praesidium
Praesidium earned 250 total points
ID: 12153891
XmlNode[] full = new XmlNode[full.Count];

Seems like this should work for you, count the number of nodes in your node list...  Why do you want the number of nodes, because depending on what you are doing, I may be able to offer you a better alternative...
0
 
LVL 26

Accepted Solution

by:
rdcpro earned 250 total points
ID: 12155402
Well, count(//Book) *is* an XPath eppression that does this.  Well, rather it's an XSLT expression that uses XPath.  But, I don't think there are any methods in Libxml that don't return nodesets...That is, in XSLT such an XPath expression will return a number but using DOM methods, it's not allowed.  You can get the nodelist, then use the DOM length property--this is the standard way of doing it.  I'm not a C programmer, but in Python (using the XPath wrappers for libxml2):

import libxml2

doc = libxml2.parseFile("tst.xml")
ctxt = doc.xpathNewContext()
res = ctxt.xpathEval("//book")
if len(res) != 2:
    print "XPath Query did not return the expected node set"
    sys.exit(1)
doc.freeDoc()
ctxt.xpathFreeContext()

Note that you must explicitly free both the document and the context.

Regards,
Mike Sharp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Getting lots of W3C errors in my XHTML site... 43 68
RSS Feed Parsing for Images 5 59
How do i send an api key in a request header 3 55
Insert Powershell variable into XML 4 34
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now