Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » Python

python/xpath question..

 
Jump to:  
 
bruce
PostPosted: Wed Sep 03, 2008 6:36 pm    Post subject: python/xpath question..
       
morning....

i apologize up front as this is really more of an xpath question..

in my python, i'm using the xpath function to iterate/parse some html. i can
do something like

s=d.xpath("//tr/td/text()")
count=len(s)

and get the number of nodes that have text

i can then do something like
s=d.xpath("//tr/td")
count2=len(s)

and get the number of total nodes...
by subtracting, i can get the number of nodes, without text.. is there an
easier way??!!
count2-count

ie, if i have something like
<tr>
<td></td>
<td>foo</td>
</tr>

is there a way to get the count that there is a single "td" node with
text()=""

thanks
 

 
John Krukoff
PostPosted: Wed Sep 03, 2008 8:14 pm    Post subject: Re: python/xpath question..
       
On Wed, 2008-09-03 at 13:36 -0700, bruce wrote:
Quote:
morning....

i apologize up front as this is really more of an xpath question..

in my python, i'm using the xpath function to iterate/parse some html. i can
do something like

s=d.xpath("//tr/td/text()")
count=len(s)

and get the number of nodes that have text

i can then do something like
s=d.xpath("//tr/td")
count2=len(s)

and get the number of total nodes...
by subtracting, i can get the number of nodes, without text.. is there an
easier way??!!
count2-count

ie, if i have something like
tr
td></td
td>foo</td
/tr

is there a way to get the count that there is a single "td" node with
text()=""

thanks


--
LINK

Well, you could just do the test (and the count!) in the xpath
expression:

count( //tr/td[ text() != "" ] )

It sounds like you're not familiar with xpath? I would recommend the
O'Reilly XSLT book, it has an excellent introduction to xpath in chapter
3.
--
John Krukoff <jkrukoff@ltgc.com>
Land Title Guarantee Company
 

 
Stefan Behnel
PostPosted: Thu Sep 04, 2008 5:12 am    Post subject: Re: python/xpath question..
       
Hi,

you should really read about XPath. There are also newsgroups specifically for
this topic, please use them.


bruce wrote:
Quote:
in my python, i'm using the xpath function to iterate/parse some html. i can
do something like

s=d.xpath("//tr/td/text()")
count=len(s)

and get the number of nodes that have text

That is a wrong assumption. It will give you the number of text nodes, not the
number of elements. They may or may not be the same.


Quote:
i can then do something like
s=d.xpath("//tr/td")
count2=len(s)

and get the number of total nodes...
by subtracting, i can get the number of nodes, without text.. is there an
easier way??!!

Yes, learn to use XPath, e.g.

//tr/td[not string()]

Stefan
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

filmiki Język C++. Gotowe rozwiązania dla programistów Kate Ryan teledyski Oblicza świąt gry i zabawy