Does querypath have a :contains() filter or an equivalent?

136 views
Skip to first unread message

kylemathews

unread,
Dec 1, 2009, 2:47:28 PM12/1/09
to devel-querypath
I've just started using querypath and am liking it so far. One thing I
can't figure out from the docs is if there's a :contains filter
similar to jquery's (http://docs.jquery.com/Selectors/contains#text).

I'm parsing some XML like this:

<header>
<headername>Content-Type</headername>
<headervalue>text/html</headervalue>
<parameter>
<paramname>charset</paramname>
<paramvalue>UTF-8</paramvalue>
</parameter>
</header>

and want to select the headervalue if it contains "text/html" but I
can't figure out how to do that other than loop through the results
and do a manual check there.

Any ideas?

Thanks!

Kyle Mathews

TechnoSophos

unread,
Dec 1, 2009, 5:00:58 PM12/1/09
to devel-q...@googlegroups.com
:contains() should work. If it doesn't that's a bug.

Matt
> --
>
> You received this message because you are subscribed to the Google Groups "devel-querypath" group.
> To post to this group, send email to devel-q...@googlegroups.com.
> To unsubscribe from this group, send email to devel-querypa...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/devel-querypath?hl=en.
>
>
>



--
http://technosophos.com
http://querypath.org

Kyle Mathews

unread,
Dec 1, 2009, 5:07:33 PM12/1/09
to devel-q...@googlegroups.com
I guess it's a bug then. . . 

For the following code:

  echo "testing contains\n";
  print $qp->find("header>headername:contains('Subject')")->text() . "\n";
  
  foreach ($qp->top()->find("header>headername") as $headername) {
    switch($headername->text()) {
      case "Subject":
        $subject = $headername->next()->text();
        echo "Subject: " . $subject . "\n";
        break;
   }
 }     

The contains line prints nothing while the line inside the switch does find and print the subject.

--Kyle Mathews

kyle.mathews2000.com/blog
http://twitter.com/kylemathews

TechnoSophos

unread,
Dec 1, 2009, 6:17:14 PM12/1/09
to devel-q...@googlegroups.com
I'll give it a shot and let you know what I find.

Matt

TechnoSophos

unread,
Dec 1, 2009, 7:13:05 PM12/1/09
to devel-q...@googlegroups.com
Oh, I'm sorry I didn't notice this before...

:contains() treats the parens as string delimiters. So you don't need
quotes around the string:

:contains(Subject)

or

:contains(This is a longer string)

If you do wrap the string in quotes, QueryPath looks for the quotes.
In other words, :contains('Subject') only matches the pcdata
<element>'subject'</element>.

I wrote it this way based on the CSS 3 selector spec. If jQuery
behaves differently, though, then I would definitely consider
stripping the quotes before doing the matching.

On a related note, I discovered a bug while looking into this issue.
The escape character inside of pseudoclasses is a backslash. Thus, you
should be able to do :contains(\)) to search for elements that have
pcdata ). However, for some reason the backslash is not correctly
removed. http://github.com/technosophos/querypath/issues#issue/12

Matt

On Tue, Dec 1, 2009 at 4:07 PM, Kyle Mathews <mathew...@gmail.com> wrote:

Kyle Mathews

unread,
Dec 2, 2009, 12:53:31 PM12/2/09
to devel-q...@googlegroups.com
Nevermind -- found them:
On Wed, Dec 2, 2009 at 10:43 AM, Kyle Mathews <mathew...@gmail.com> wrote:
Tried this morning removing the quotes from within :contains() and everything works now! Thanks for your help -- I can now remove a bunch of ugly code.

jQuery does work as I described. I think it would make sense to conform to their implementation as they're must people's first exposure to css 3 selectors. Where, btw, are the CSS 3 selector specs? I tried looking quite awhile yesterday and couldn't find them.

Kyle Mathews

unread,
Dec 2, 2009, 12:43:52 PM12/2/09
to devel-q...@googlegroups.com
Tried this morning removing the quotes from within :contains() and everything works now! Thanks for your help -- I can now remove a bunch of ugly code.

jQuery does work as I described. I think it would make sense to conform to their implementation as they're must people's first exposure to css 3 selectors. Where, btw, are the CSS 3 selector specs? I tried looking quite awhile yesterday and couldn't find them.
On Tue, Dec 1, 2009 at 5:13 PM, TechnoSophos <techno...@gmail.com> wrote:

TechnoSophos

unread,
Dec 2, 2009, 7:08:28 PM12/2/09
to devel-q...@googlegroups.com
Kyle,

I just opened an issue for this:
http://github.com/technosophos/querypath/issues#issue/13

It looks like you have another issue there, too. I responded to that one.

Matt
Reply all
Reply to author
Forward
0 new messages