XPath finding certain node with certain text - php

So I had a question regarding something similar to that at:
getting a parent node and its child, where the child has a certain text
But the situation change and I find some problems with my above idea.
I am now trying to find a node with specific text because it seems when I use 29 it will also find nodes that has 2999 or anything else that has 29 in it.
So my question is how can I turn :
$myvar = $xpath->query('(//*[text()[contains(., "something")]])');
so it will look for node with certain text. not for a node that "has" certain text.

Just use //*[text() = "29"] instead of contains().

Use:
//*[. = 29]
This selects any element, whose string value, when converted to a number is equal to 29.

Related

First Element of XPATH

The question looks like the same as : XPath Get first element of subset but it's, I think, a bit different.
Here's the following blog:
http://www.mademoiselledeco.com/
I want to get the first picture of each post. For that, I thought of the following xpath query :
//div[contains(#class,'type-post status-publish')]//img/#src
Following the example of the previous post I mentionned, I also tried:
//div[contains(#class,'type-post status-publish')](//img/#src)[1]
but that says
Warning: DOMXPath::query(): Invalid expression
any idea?
Thanks a lot
OK, I understand, after inspection of the source: each <img> is contained in a <p>, thus img[1] will match all pictures, since they are, in the context of a paragraph, the first image.
In this context, I would rather try getting the first paragraph containing an image:
//div[contains(#class,'type-post status-publish')]//p[img][1]/img/#src
With this XPath I get 9 img/#src.
//div[#class='post-content-container']//p[./img][1]/img
This is not the best solution but I think it would work.
//div[#class='post-content-container']
Should get each post
//p[./img][1]/img
Should get the first paragraph, which contains an image. Then selects the image.
Actually the duplicate question you've picked isn't that far off. It has an explanation in one of it's answers which sounds pretty legit:
The [] operator has a higher precedence (binds stronger) than the // abbreviation.
So the //img abbreviation stands in your way. Let's expand it:
/descendant-or-self::node()/child::img
Adding [1] at the end would select each first img child (which is exactly as others have outlined). This is also the reason why there is higher precedence for the predicate here.
The Abbreviated Syntax section in Xpath 1.0 actually covers this with a note:
NOTE: The location path //para[1] does not mean the same as the location path /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents.
That is: you're not looking for the descendant-or-self axis and any nodes children therein, but just for the first img element in the descendant axis:
/descendant::img[1]
So the xpath expression in full:
//div[contains(#class,'type-post status-publish')]/descendant::img[1]/#src
Result with your example (10):
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/03/Couleur-FionaLynch-Caroline-St.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/02/2-OF-MO-cascade-lumineuse2-1024x398.jpg"
src="https://s-media-cache-ak0.pinimg.com/736x/2e/f7/eb/2ef7eb28dc3e6ac9830cf0f1be7defce.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/01/couleur-peinture-flamant-vert-trekking.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/01/Lily-of-the-Valley-Designed-by-Marie-Deroudilhe-02.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/01/shopping-decoration-jaune-bleu-delamaison-1024x866.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/01/wikao-cheminee-berlin-mademoiselledeco4.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2015/01/voeux2015-mademoiselledeco-blog.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2014/12/suite-novotel-constance-guisset-1.jpg"
src="http://www.mademoiselledeco.com/wp-content/uploads/2014/12/wish-list-decoration-noel-2014.jpg"
I hope this sheds some light.

Include tag count with Wordpress get_tag_link

I'm using Wordpress, and i'm trying to create an index for a particular part of my site. I'd like to be able to list certain tags (not the entire tag list) with their respective post-count in brackets next to each link.
For example, I can get tag #16 to show as a link using:
tagname
but for the life of me I can't figure out how to get it to show with the post-count next to it, like 'tagname (20)', etc.
I'm sure there's a simple answer (it seems like it should be easy?) but i've googled every permutation of 'get_tag_link with post count' that I can think of and have come up empty.
Any help would really be appreciated!
Kate.
First you need to use get_term_by, then you need to pass in(as the first argument), either the id, slug, or name of the category/term/tag, whatever.
Then you can simply call ->count on your new object.
$term = get_term_by('id', 16, 'post_tag');
Then you can simply call:
$term->count;

Simple HTML DOM Parser find closest Element

I want to find the "nearest Element" for a specific String using (simplehtmldom.sourceforge.net)
For example, if I search for the word "Imprint" I want to get the nearest element over it, therefore I programmed a "recursive function"
function getelementofcontent($html,$search){
for($i=0;strpos($html->childNodes($i)->plaintext,$search)==-1;$i++);
getelementofcontent($html->childNodes($i),$search);
}
So the method is always called when there is still the string... what did I do wrong?

grabbing an array value, but only the text - how?

I'm trying to grab the value of an array that I got from parsing an xml file (using PHP's simpleXML) so that I can throw it into a database table. The problem I'm having is that one of the array values has a div and "a" tags in it after a sentence or two (which is what I really want). I'm not sure how to grab only the text. The array value looks like this:
[0] => The central purpose and philosophy of this podcast series<div class="feedflare">
So I'm assuming that maybe I could do some kind of function that grabs the value up to the point of the "<" and stop there and throw this new variable into the database. I'm kind of a n00b with PHP so I don't even know where to start doing that. Any help is greatly appreciated.
sounds like strip_tags() is what you_re looking for. just do:
$text = strip_tags($my_array[0]);

PHP: How can I access this XML entity when its name contains a reserved word?

I'm trying to parse this feed: http://musicbrainz.org/ws/1/artist/c0b2500e-0cef-4130-869d-732b23ed9df5?type=xml&inc=url-rels
I want to grab the URLs inside the 'relation-list' tag.
I've tried fetching the URL with PHP using simplexml_load_file(), but I can't access it using $feed->artist->relation-list as PHP interprets "list" as the list() function.
I have a feeling I'm going about this wrong (not much XML experience), and even if I was able to get hold of the elements I want, I don't know how to extract their attributes (I just want the type and target fields).
Can anyone gently nudge me in the right direction?
Thanks.
Matt
Have a look at the examples on the php.net page, they actually tell you how to solve this:
// $feed->artist->relation-list
$feed->artist->{'relation-list'}
To get an attribute of a node, just use the attribute name as array index on the node:
foreach( $feed->artist->{'relation-list'}->relation as $relation ) {
$target = (string)$relation['target'];
$type = (string)$relation['type'];
// Do something with it
}
(Untested)

Categories