Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Bes XPath query?

Reply
Thread Tools

Bes XPath query?

 
 
CxT
Guest
Posts: n/a
 
      05-08-2009
Hi,

I need to be able to find the value (5.56) in the td that is a
sibling
of the "Earnings/Share" td. I'm not sure how I go about using XPath
to search for that specific string.
Any guidance would be much appreciated.
CxT
<table>
<tr>
<td>Beta</td>
<td class="cl1">1.66</td>
</tr>
<tr>
<td>Dividend &amp; Yield</td>
<td class="cl1">NA</td>
</tr>
<tr>
<td>Earnings/Share</td>
<td class="cl1">5.56</td>
</tr>
Note: the above comes from a very long html file (this is just a
snippet).
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      05-08-2009
CxT wrote:
> Hi,
>
> I need to be able to find the value (5.56) in the td that is a
> sibling
> of the "Earnings/Share" td. I'm not sure how I go about using XPath
> to search for that specific string.
> Any guidance would be much appreciated.
> CxT
> <table>
> <tr>
> <td>Beta</td>
> <td class="cl1">1.66</td>
> </tr>
> <tr>
> <td>Dividend &amp; Yield</td>
> <td class="cl1">NA</td>
> </tr>
> <tr>
> <td>Earnings/Share</td>
> <td class="cl1">5.56</td>
> </tr>


//table/tr[td[. = 'Earnings/Share']]/td[@class = 'cl1']
--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
 
 
 
CxT
Guest
Posts: n/a
 
      05-08-2009
On May 8, 5:45*am, Martin Honnen <mahotr...@yahoo.de> wrote:
> CxT wrote:
> > Hi,

>
> > I need to be able to find the value (5.56) in the td that is a
> > sibling
> > of the "Earnings/Share" td. *I'm not sure how I go about using XPath
> > to search for that specific string.
> > Any guidance would be much appreciated.
> > CxT
> > * * * * * * * * * * <table>
> > * * * * * * * * * * * <tr>
> > * * * * * * * * * * * * <td>Beta</td>
> > * * * * * * * * * * * * <td class="cl1">1.66</td>
> > * * * * * * * * * * * </tr>
> > * * * * * * * * * * * <tr>
> > * * * * * * * * * * * * <td>Dividend &amp; Yield</td>
> > * * * * * * * * * * * * <td class="cl1">NA</td>
> > * * * * * * * * * * * </tr>
> > * * * * * * * * * * * <tr>
> > * * * * * * * * * * * * <td>Earnings/Share</td>
> > * * * * * * * * * * * * <td class="cl1">5.56</td>
> > * * * * * * * * * * * </tr>

>
> * *//table/tr[td[. = 'Earnings/Share']]/td[@class = 'cl1']
> --
>
> * * * * Martin Honnen
> * * * *http://msmvps.com/blogs/martin_honnen/


Hmmm... that specific query is returning 0 elements. What would I do
if I wanted to search for an element that contains just the text
("Earnings/Share")? I thought I could do "//td[. = 'Earnings/Share']"
but that isn't returning any hits either. Very confused.

Thanks for any additional guidance.
CxT
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      05-08-2009
CxT wrote:

>>> <table>
>>> <tr>
>>> <td>Beta</td>
>>> <td class="cl1">1.66</td>
>>> </tr>
>>> <tr>
>>> <td>Dividend &amp; Yield</td>
>>> <td class="cl1">NA</td>
>>> </tr>
>>> <tr>
>>> <td>Earnings/Share</td>
>>> <td class="cl1">5.56</td>
>>> </tr>

>> //table/tr[td[. = 'Earnings/Share']]/td[@class = 'cl1']



> Hmmm... that specific query is returning 0 elements. What would I do
> if I wanted to search for an element that contains just the text
> ("Earnings/Share")? I thought I could do "//td[. = 'Earnings/Share']"
> but that isn't returning any hits either. Very confused.


Are you trying to use XPath against an XHTML document? In XHTML elements
are in the namespace http://www.w3.org/1999/xhtml and '//td' (in XPath
1.) always selects elements in no namespace so that could be one reason
why the expressions do not find any element.

Other than that you will need to provide some context as for how exactly
you use XPath.

--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
CxT
Guest
Posts: n/a
 
      05-08-2009
On May 8, 8:09*am, Martin Honnen <mahotr...@yahoo.de> wrote:
> CxT wrote:
> >>> * * * * * * * * * * <table>
> >>> * * * * * * * * * * * <tr>
> >>> * * * * * * * * * * * * <td>Beta</td>
> >>> * * * * * * * * * * * * <td class="cl1">1.66</td>
> >>> * * * * * * * * * * * </tr>
> >>> * * * * * * * * * * * <tr>
> >>> * * * * * * * * * * * * <td>Dividend &amp; Yield</td>
> >>> * * * * * * * * * * * * <td class="cl1">NA</td>
> >>> * * * * * * * * * * * </tr>
> >>> * * * * * * * * * * * <tr>
> >>> * * * * * * * * * * * * <td>Earnings/Share</td>
> >>> * * * * * * * * * * * * <td class="cl1">5.56</td>
> >>> * * * * * * * * * * * </tr>
> >> * *//table/tr[td[. = 'Earnings/Share']]/td[@class = 'cl1']

> > Hmmm... that specific query is returning 0 elements. *What would I do
> > if I wanted to search for an element that contains just the text
> > ("Earnings/Share")? *I thought I could do "//td[. = 'Earnings/Share']"
> > but that isn't returning any hits either. *Very confused.

>
> Are you trying to use XPath against an XHTML document? In XHTML elements
> are in the namespacehttp://www.w3.org/1999/xhtmland '//td' (in XPath
> 1.) always selects elements in no namespace so that could be one reason
> why the expressions do not find any element.
>
> Other than that you will need to provide some context as for how exactly
> you use XPath.
>
> --
>
> * * * * Martin Honnen
> * * * *http://msmvps.com/blogs/martin_honnen/


I'm using XPath to search through the following URL:

http://moneycentral.msn.com/detail/s...uote=Get+Quote

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-
US" is present at the top of the file.

If I can't do a more structured search can I still use XPath to
perform a simple text search and then obtain the node for where I find
the text?

Thank you so much for your help,
CxT

PS: Note that other XPath searches work in this document, for example:
"//table/tr[@class = 'rs0']/th/span[@class = 's1']"
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      05-08-2009
CxT wrote:

> I'm using XPath to search through the following URL:
>
> http://moneycentral.msn.com/detail/s...uote=Get+Quote
>
> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-
> US" is present at the top of the file.


So that is XHTML and that means, if the document is parsed by an XML
parser, that you need to bind a prefix to the namespace URI and use that
prefix in your XPath expressions.


> PS: Note that other XPath searches work in this document, for example:
> "//table/tr[@class = 'rs0']/th/span[@class = 's1']"


That is rather odd, with the namespace declaration being present on the
root element. How do you parse the document, which XPath API do you use?
Is that XPath over HTML, as some browsers like Mozilla or Opera provide?

--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
CxT
Guest
Posts: n/a
 
      05-08-2009
On May 8, 8:52*am, Martin Honnen <mahotr...@yahoo.de> wrote:

> So that is XHTML and that means, if the document is parsed by an XML
> parser, that you need to bind a prefix to the namespace URI and use that
> prefix in your XPath expressions.


Could you please provide an example of what such an expression would
look like?

> > PS: Note that other XPath searches work in this document, for example:
> > "//table/tr[@class = 'rs0']/th/span[@class = 's1']"

>
> That is rather odd, with the namespace declaration being present on the
> root element. How do you parse the document, which XPath API do you use?
> Is that XPath over HTML, as some browsers like Mozilla or Opera provide?


I am using NSXML under Cocoa/Objective-C (Mac OS X).

Once again, thank you for your help. I didn't even know XPath existed
until a few days ago.
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      05-08-2009
CxT wrote:
> On May 8, 8:52 am, Martin Honnen <mahotr...@yahoo.de> wrote:
>
>> So that is XHTML and that means, if the document is parsed by an XML
>> parser, that you need to bind a prefix to the namespace URI and use that
>> prefix in your XPath expressions.

>
> Could you please provide an example of what such an expression would
> look like?


The XPath API needs to provide a way to bind a prefix to a namespace
URI. Assuming we have bound the prefix 'xhtml' to
'http://www.w3.org/1999/xhtml' any XPath expression would then use the
prefix to qualify element names e.g.
/xhtml:html/xhtml:body//xhtml:table


> I am using NSXML under Cocoa/Objective-C (Mac OS X).


I don't know that one. The documentation
http://developer.apple.com/documenta...pts/NSXML.html
says it supports both XQuery and XPath.
If it really supports XQuery 1.0 then you might be able to avoid the
prefix and do

declare default element namespace "http://www.w3.org/1999/xhtml";
/html/body//table

But that all does not explain why some XPath expressions worked without
any prefix and other did not work. I am afraid you need to find some
forum/newsgroup/mailing list dealing with NSXML, unless someone here
comes along that knows NSXML.

I tried that URL you provided with Saxon 9's XQuery implementation but
it reports an XML parse error so it is not even able to build a data
model from that document.

--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      05-08-2009
CxT wrote:
> Could you please provide an example of what such an expression would
> look like?


The expression needs to use namespace prefixes, and you need to provide
a namespace context to the API. Details of the latter depend on what API
you're using.

It is possible to do this all within the XPath, but EXTREMELY ugly --
you need to wildcard the namespace and then use a predicate to specify it.
/*:foo[namespace()="http://whatever"]
Since this is uncommon, processors may be slower interpreting this
version than the prefix-and-bindings version.
 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      05-08-2009
Quick reminder: The default namespace (xmlns=) is *not* applied to
attributes. If you actually want an attribute name to be namespaced, you
must use a prefix on it.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the bes Ruby's book for beginners? Renato Veneroso Ruby 17 05-25-2008 11:41 PM
"Memory leak" in javax.xml.xpath.XPath Marvin_123456 Java 4 07-29-2005 03:49 PM
Are there any XPath parsers that generate XPath trees? goog XML 0 01-14-2004 01:47 PM
XPath that does not include other XPath Anna XML 0 07-31-2003 07:55 AM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57