I'm having trouble trying to output the contents of a matched node that I'm parsing:
<div class="description">some text <br/>more text<br/></div>
I'm using HTML::TreeBuilder::XPath to find the node (there's only one div with this class):
my $description = $tree->findnodes('//div[#class="description"]')->[0];
It finds the node (returned as a HTML::Element I believe) but $description->as_HTML includes the element itself too - I just want everything contained inside the element as HTML:
some text <br/>more text<br/>
I can obviously regex strip it out, but that feels messy and I'm sure I'm just missing a function somewhere to do it?
Try doing this :
my $description = $tree->findnodes('//div[#class="description"]/text()')->[0];
This is a Xpath trick.
Use ./node() to fetch all subnodes including text and elements.
my $description = $tree->findnodes('//div[#class="description"]/node()');
Related
I need to create the element and then delete it. Is there a way to find the element by it's text after it was created?
The xpath of the element is //div[#id='mif-tree-6']/span/span[3].
You can use xpath for it for example. Like:
//div[#id='mif-tree-6']//span[contains(text(),'your_text_here')]
UPDATE
Please provide an example of your html. It is possible to find a parent of your element with xpath and after that to find all the childs. For example your html =
<div id='lol'>
<div>first_item</div>
<div>second_item</div>
<div>third_element</div>
</div>
You get an array of elements with xpath =
//div[contains(text(),'first_')]/../div
So you can do something like:
click | //div[contains(text(),'first_')]/../div[2]
BUT if there are a lot of brothers-elements to find by text of one sibling it will be necessary to use loop to get every of them.
Once again. If you will provide full information about what are you doing and an example of your html it will be much easier to suggest.
I have a foreach loop code in a tpl file like this:
[{foreach from=$oView->getArticleList() item=actionproduct name=test_articleList}]
[{include file="inc/product_alt.tpl" product=$actionproduct testid="action_"|cat:$actionproduct->oxarticles__oxid->value test_Cntr=$smarty.foreach.test_articleList.iteration}]
[{/foreach}]
the included file product_alt.tpl in the foreach loop contains a simple div container and get displayed for each product. Now i am looking for a solutions to add to every second div container a extra class.
I google a bit and found out (i think so) that I must work with even and odd. But i stucked how to apply this exactly to the foreach loop with the goal that every secod div container get an extra class.
You are looking for cycle. What you can do for instance is assign an extra $class variable in your include statement, that gets changed by the assign, like so:
{cycle values='yourClass1,youClass2' assign='class'}
That's probably where your odd/even thought comes from: the manual says
{cycle values='odd,even' assign='class'}
But those are just values. Anyeay, your variable 'class' now has alternating "yourClass1" and "yourClass2" (or odd/even) as content. If you assign this to your include, and then add something like
<div class="{$class}">
You get alternating classes. One of them is the one you want. the other can be empty..
check out the cycle manual: http://www.smarty.net/docsv2/en/language.function.cycle
I need to find any extra links and print them out. I started by doing:
get_xpath_count('//li/a')
and comparing it to the size of an array that holds the name of all the links for the sidebar. When the count is too high/low, I need to print out all the extra/missing links. I would like to make a list of the names so I can compare it to the array. I've tried a few things like get_text('//li/a'), which returns the name of the first. get_text('//li/a[1]) does the same, but any other index returns nothing.
Any ideas? Also, I need the name that's displayed on the link, not the actual href.
Edit* Also, i'm pretty new to selenium and Xpath. Please let me know if there's info I let out that is needed, or just any suggestions towards thew way I'm going about this.
I have been able to get this to work using CSS element locators. Since I use CSS selectors far more often than Xpath, I find it easier to always use them with Selenium as well.
$selenium->get_text("css=li a:nth-child(1)")
$selenium->get_text("css=li a:nth-child(2)")
$selenium->get_text("css=li a:nth-child(...)")
$selenium->get_text("css=li a:nth-child(n)")
Use:
(//li/a)[$someNumber]
this will get you the text of $someNumber-th //li/a in the XML document.
In order to know what values to use to substitute the $someNumber with, you need to know the total count of these elements:
count(//li/a)
This is in JAVA. You can use the same concept in perl
int totCountInPage=selenium.getXpathCount(//li/a);
for(int count=1;count<=totCountInPage;count++)
System.out.println(selenium.getText("xpath=//li[count]/a"));
This should print text inside the anchor links under all li tag.
I have a php page which contains a large amount of HTML in it. One part of the HTML has a div in the following format:
<div class="reusable-block" id="xyzabcwy">there is a lot of HTML here which may be in any format</div>
Keep in mind, this div is contained within the DOM at any location however, I do know the div ID programatically.
I was originally finding this string within my database, since a record of it exists there however, the format between the data in the database record and the page are sometimes different due to whitespace but other than the white space, the strings are exactly the same. The problem is, I don't know what format the whitespace is in.
It seems it is better to write a regular expression to find this div and replace it entirely.
I could use a hand though.
Other ideas are also welcome.
Many thanks!
If you are using jQuery,
$('#xyzabcwy').html(new_data);
if not
document.getElementById('xyzabcwy').innerHTML = new_data;
otherwise, here is a PHP example.
Edit: PHP
<?php
$id = "xyzabcwy";
$html = "<div id=\"" . $id . "\">this is html</div>";
$newdata = "test";
echo preg_replace("#<div[^>]*id=\"{$id}\".*?</div>#si",$newdata,$html);
?>
This should output
<div id="123">test</div>
Answer from: Replace a div content with PHP
I'd like to know how to search for a string within an xml document. The object type is System.Xml.XmlNode.XmlDocument. The string can be anything with the document. I.e. attribute or element.
I tried
Select-Xml -Xml $xml -XPath "./Test"
but got no results
The pattern you are trying to use selects root nodes named Test.
You could use the pattern (//text()|//#*)[contains(string(), "test")], that selects the attributes that contain the string test or the text nodes that contain it (i.e. not the elements).
But you want to select the elements, right? Using (//*|//#*)[contains(., "test")] does that, but it selects elements that contain the string test, even if it is through some child element, which is not what is wanted either.
So I guess you'll have to use something like (//*[contains(text(), "test")]|//#*[contains(., "test")]), which gives you what you want, but is not very pretty.