EzDevInfo.com

querypath

QueryPath is a PHP library for manipulating XML and HTML. It is designed to work not only with local files, but also with web services and database resources. QueryPath: Find your way

Use Querypath to directly access an ID

Hi pretty basic question that I'm surprised wasn't already asked here.

Is it possible to directly get an element based on an id, using Querypath?

Currently, the way I do it is

$qp = htmlqp('../index.php');
foreach ($qp->find('img') as $key) {

    if ($key -> attr('id') === "$picID") {
        $src = $key -> attr('src', $src) -> writeHTML("../indextest.php");
    }
}

however this does not seem to be the most efficient method as it loops through every image each time, when it could just access the ID directly.


Source: (StackOverflow)

2 fundamental questions about querypath

  1. How to find if a node does not exist ? i am using

    if ( $item->branch()->siblings($tagNames['desc'])->text())

is there a better way ?

  1. is there a way to execute an OR query? if tag A exists get its text(), otherwise get B's text() ?

I am using the following :

 $desc1 = (  $item->branch()->siblings($tagNames['desc'])->text()  ?
$item->branch()->siblings($tagNames['desc'])   :
$item->branch()->siblings($tagNames['descAlternative']) ) ;

which doesn't look like the most efficient way of doing things.

Thanks


Source: (StackOverflow)

Advertisements

Fatal error: Uncaught exception 'QueryPath\ParseException' namespace error :

I'm using QueryPath to parse Chimpmail's email templates (https://github.com/mailchimp/Email-Blueprints), but when I try to append a tr with a "mc:repeatable" attribute I get this error:

Fatal error: Uncaught exception 'QueryPath\ParseException' with message 'DOMDocumentFragment::appendXML(): namespace error : Namespace prefix mc for repeatable on tr is not defined 

Is there a way to tell QueryPath not to try to parse namespaces?


Source: (StackOverflow)

How to find the shortest Selector

How can i get the shortest selector for any clicked element?
I want use this selector later to recover the same (or a nearly same) result.

Is there any existing method?

My ideas for that are searching with priority for:

  1. single-used ID
  2. multi-used ID with counter
  3. single-used name
  4. multi-used name with counter
  5. single-used classname
  6. multi-used classname with counter
  7. shortest parent path to 1.-6.

Not shure how to build a short path for maximum reliability and maximum flexibility (f.e. new parent-tags or a movement in the code).


Source: (StackOverflow)

Using multiple find in foreach with QueryPath

I'm using QueryPath and PHP.

This finds the .eventdate okay, but doesn't return anything for .dtstart:

$qp = htmlqp($url);
foreach ($qp->find('table#schedule')->find('tr') as $tr){
    echo 'date: ';
    echo $tr->find('.eventdate')->text();
    echo ' time: ';
    echo $tr->find('.dtstart')->text();
    echo '<br>';
}

If I swap the two, .dtstart works okay, but .eventdate doesn't return anything. Thus, it seems that find() in querypath destroys the element and only returns the value it needs, making iteration over $tr not possible to search for multiple items.

Here's example HTML for a TR I'm dealing with:

<tr class="event"><th class="date first" scope="row"><abbr class="eventdate" title="Thursday, February 01, 2011" >02/01</abbr><span class="eventtime" ><abbr class="dtstart" title="2012-02-01T19:00:00" >7:00 PM</abbr><abbr class="dtend" title="2012-02-01T21:00:00" >9:00 PM</abbr></span></th><td class="opponent summary"><ul><li class="first">@ <a class="team" rel='nofollow' href="/high-schools/ridge-wolves/basketball-winter-11-12/schedule.htm" >Ridge </a> <span class="game-note">*</span></li><li class="location" title="Details: Ridge High School">Details: Ridge High School</li><li class="last"><a class="" rel='nofollow' href="/local/stats/pregame.aspx?contestid=4255-4c6c-906d&amp;ssid=381d-49f5-9f6d" >Preview Game</a></li></ul></td><td class="result last"><a class="pregame" rel='nofollow' href="/local/stats/pregame.aspx?contestid=4255-4c6c-906d&amp;ssid=381d-49f5-9f6d">Preview</a></td></tr>

I tried copying the $tr before the first find and replacing it before the second, but that didn't work.

How can I search during each $tr for certain variables?

FYI, beyond .eventdate and .dtstart, I also want the .opponent, href under the a for the opponent and the a anchor text.


Source: (StackOverflow)

QueryPath changing iframe tags to self-closing?

I'm using QueryPath to wrap a <div> around embedded videos on a site that contains a mixture of <object> and <iframe> embeds.

I tried the following code:

    $content = qp($content)
    ->find('object,iframe,a.evdPlayer')
    ->wrap('<div class="splashBack"></div>')
    ->top('body')->children()
    ->html(); // << It's wanting to remove the </iframe> Grrr.
return $content;

But it seems to want to change my <iframe></iframe> code to <iframe />, which is messing things up for some reason. Is there a way to keep it from changing the tags it's wrapping?

Thanks in advance!


Source: (StackOverflow)

Find and Replace with QueryPath

I have a string assigned to a variable of $output. I want to use QueryPath to iterate through the string and add a class to each matched element.

Currently I have:

$output = qp($output)->find('table')->addclass('table')->html();

The only issue with this is it removes the content of $output before the first match. Is there a way of using QueryPath to find the match and then add the class while keeping the contents of $output intact?

EDIT:

Pseudo code:

$output = '<table class="temp"><blah></blah></table>';

Magic QueryPath

$output = '<table class="temp blah"><blah></blah></table>';

Source: (StackOverflow)

dynamically alter html tables to display on mobile

I am creating a mobile optimized website that is dynamically created by recognizing a request from a mobile device, manipulating the existing page to be mobile optimized using php and the html parser, QueryPath. Some of the pages I plan to manipulate contain html tables, such as this one, which to don't display well on mobile.

I am looking for a function that can handle any html table of any amount of rows and columns and manipulate it so it will show up well formatted on a mobile device. I am using the jquery mobile framework.

a solution in either php or javascript/jquery would be great.

Here is a jsFiddle of what I mean: Link


Source: (StackOverflow)

XPath is returning element including the tags

I am using xPath to read an XML file, but when trying to get the contents of a certain element, it returns it including the tags <> .

The XML file is structured like this:

<item>
    <attribute01>something</attribute01>
    <attribute02>something</attribute02>
    <attribute03>something</attribute03>
    <attribute04>
        <category>Some category name</category>
        <category>Some other category name</category>
    </attribute04>
</item>

I am working in the context of //item and then using attribute04//category to get the category element. However, this is what I'm getting back:

xpathparser:04 :
<Category>Bed &amp; Breakfast and Inns</Category>
xpathparser:04 :
<Category>Hotel</Category>
...etc...

It returns the entire element including the tags. Does anybody have an idea what is going wrong here?

I am using the Feeds xPath parser module for Drupal (https://drupal.org/project/feeds_xpathparser).

Thank you in advance.


Source: (StackOverflow)

What is QP layer?

From blogs.innodb.com: InnoDB Memcached with Binlog Capability

In our earlier Labs release, we presented a MySQL NoSQL solution through InnoDB Memcached Daemon Plugin (see earlier Calvin’s and my posts). That earlier release allows the Memcached Plugin directly interacting with InnoDB, completely bypassing MySQL optimizer and QP layers. It differs with another popular MySQL NoSQL solution, HandlerSocket, by skipping even the Handler API layer and directly access the InnoDB through InnoDB APIs. Thus, in theory, it would be simpler and more efficient.

What are QP Layers?

Searching did not yield me any good results. The closest I came to was Query Path. Is this right?


Source: (StackOverflow)

How to change the tag itself in QueryPath?

I would like to change the tag itself, identified in QueryPath. To be specific, I would like to convert an anchor tag like this
<a rel='nofollow' href="abc.html">Example</a>
into
<?php Blah-Blah ?>Example</a>
or
<?php Blah-Blah2 ?>

I can find the anchor tag and retrieve its element:
$qp->find('a[href]'); $href = $qp->attr('href');
but then, is there any way to change/replace the tag itself in QueryPath?

Alternatively it is fine by me to wrap the component with <div id="specific"> tag - if this is possible, I suppose I can search for it with $qp->top('div[id="specific"]');, then can replace the entire child (the anchor tag and its element) with the php code.

However, I have failed to find either way in QueryPath...


Source: (StackOverflow)

When I modify docx xml file [document.xml] with PHP QueryPath 2.1.2 changes are not visibile in actual test1.docx file, why?

I am trying to learn how to modify .docx and .odt files with PHP [QueryPath] and when I run this script [below] from cmd.exe [command line] on WAMP, on the command line it shows that changes that I want have been done and all text inside tags have been changed, but when I open an actual docx file or its xml equivalent - none of the changes have been made. Why? And how can I rectify this? Thanks in advance.

    <?php

require 'src/QueryPath/QueryPath.php';

$file = 'zip://test1.docx#word/document.xml';

$doc = qp($file);

foreach($doc->find('w|t') as $item) {
        $item->text('BLABLABLA')->writeXML();
}


?>

Source: (StackOverflow)

Is it possible to select HTML comments using QueryPath?

I see this is possible using jQuery, but how can it be done in QueryPath?

Selecting HTML Comments with jQuery

If not, can anyone suggest an HTML parser that can select comments?


Source: (StackOverflow)

QueryPath selector fails with chained :has():contains()

The following CSS selector works in jQuery so I was expecting it to work in QueryPath 3.0.0, but it only returns an empty value:

div#caption:has(h2):contains('Product Description') div:first

Here is what I am trying to get it to retrieve:

<div id="caption"><h2>Product Description</h2><div>Text I want to capture is here.</div><div>I don't want this text.</div><br clear="all" /></div>

What is wrong with the selector?


Source: (StackOverflow)

How to fetch custom or namespaced elements in QueryPath?

I'm trying to fetch my custom elements in QueryPath library. But QueryPath does not work. Can anyone please help me how I can do it in QueryPath? See sample code below;

<plugin:text path="filename" />

Moreover, I also checked the PhpSimpleDom library in detail. Although it works good for me. But it does not provide the full support of css selectors and more. I also have face a very bad memory problem in PhpSimpleDom.

Can anyone please help me to fetch above mentioned element in QueryPath?

Thanks Smac


Source: (StackOverflow)