How do I get the nth element with a certain name in the entire XML document? - iphone

so will something like this work using Hpple Xpath
//a[4]
The fourth tag in a html tree?
Or do i need to do it programmatically by counting in a for() loop?

The XPath for the fourth <a> in an HTML document is:
(//a)[4]
Your example //a[4] will produce a set of all <a>s that are the fourth <a> in their respective parent, and that is not what you want here.
See also: https://stackoverflow.com/a/14209492/1945651

Related

How to get the values of an element by name

I am using T-SQl and xPath to query an xml doc. and I need to extract the value of an element by name
This is the way I initially implemented the code "using element indexing to access the element" but since element can have multiples element of , it does not longer work properly.
n.value(''./ROLES[1]/ROLE[1]/BORROWER[1]/GOVERNMENT_MONITORING[1]/HMDA_RACES[1][./HMDA_RACE/EXTENSION/ULDD:OTHER/ULDD:HMDA_RACE_EXTENSION/ULDD:HMDA_RACE_DETAIL/ULDD:HMDARaceType][1]'',''VARCHAR(100)'')
This is the xml I am querying
<HMDA_RACES>
<HMDA_RACE>
<EXTENSION>
<ULDD:OTHER xmlns:ULDD="http://www.datamodelextension.org/Schema/ULDD">
<ULDD:HMDA_RACE_EXTENSION>
<ULDD:HMDA_RACE_DESIGNATIONS>
<ULDD:HMDA_RACE_DESIGNATION>
<ULDD:HMDARaceDesignationType>Samoan</ULDD:HMDARaceDesignationType>
</ULDD:HMDA_RACE_DESIGNATION>
</ULDD:HMDA_RACE_DESIGNATIONS>
<ULDD:HMDA_RACE_DETAIL>
<ULDD:HMDARaceType>NativeHawaiianOrOtherPacificIslander</ULDD:HMDARaceType>
</ULDD:HMDA_RACE_DETAIL>
</ULDD:HMDA_RACE_EXTENSION>
</ULDD:OTHER>
</EXTENSION>
</HMDA_RACE>
<HMDA_RACE>
<EXTENSION>
<ULDD:OTHER xmlns:ULDD="http://www.datamodelextension.org/Schema/ULDD">
<ULDD:HMDA_RACE_EXTENSION>
<ULDD:HMDA_RACE_DETAIL>
<ULDD:HMDARaceType>White</ULDD:HMDARaceType>
</ULDD:HMDA_RACE_DETAIL>
</ULDD:HMDA_RACE_EXTENSION>
</ULDD:OTHER>
</EXTENSION>
</HMDA_RACE>
</HMDA_RACES>
this is what I am trying but I am not getting the result I want.
value(''./ROLES[1]/ROLE[1]/BORROWER[1]/GOVERNMENT_MONITORING[1]/HMDA_RACES[child = "ULDD:HMDARaceType"][1]'',''VARCHAR(100)'')
a successful query would return value "NativeHawaiianOrOtherPacificIslander White". I am getting just the first value "NativeHawaiianOrOtherPacificIslander"
Just in terms of the xpath expression, and using the xml code in your question, this expression
*//HMDARaceType/text()
or this:
//*[local-name()='HMDARaceType']/text()
selects
NativeHawaiianOrOtherPacificIslander
White
Is that what you're looking for?

Is it possible to find and store element's location by text in selenium ide?

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.

cakephp element not displaying

I have a number of form inputs inside an element as they are repeated in other forms, however it does not output the content of the element when i include it in a view. I can place "Die();" within the element and it will show everything up until that point including the inputs within the element. When checking the source code (without the "Die();") it seems to have disregarded everything within the element including formatting such as DIV's.
The only thing i can think is that cakephp does not like you setting a form in a view then including inputs to the form using elements, does any one know if this is the case, or know the actual reason why this is happening?
The answer is i'm an idiot and forgot to echo the element.... yeah.

Need to find the tags under a tag in an XML using jQuery

I have this xml as part of the responseXml of an Ajax call:
<banner-ad>
<title><span style="color:#ffff00;"><strong>Title</strong></span></title>
</banner-ad>
When I used this jQuery(responseXml).find("title").text(); the result is "Title".
I also tried jQuery(responseXml).find("title:first-child") but the result is [object Object].
I want to get the result:
<span style="color:#ffff00;"><strong>Title</strong></span>
Please let me know how to do this in jQuery.
Thanks in advance for any help.
Regards,
Racs
Your problem is that you cannot simply append nodes from one document (the XML response) to another (your HTML page). The issue is two-fold:
You can use jQuery to append nodes from the XML document to the HTML page. This works; the nodes appear in the HTML DOM, but they stay XML nodes and therefore the browser ignores the style attribute, for example. Consequently the text will not be yellow (#ffff00).
As far as I can see, jQuery offers no built-in way to get the XML string (i.e. a serialized node) from an XML node. jQuery can handle XML documents quite well, but there is no equivalent to what .html() does in HTML documents.
So to make this work we need to extract the XML string from the XML document. Some browsers support the .xml property on XML nodes (namely, IE), the others come with an XMLSerializer object:
// find the proper XML node
var $title = $(doc).find("title");
// either use .xml or, when unavailable, an XMLSerializer
var html = $title[0].xml || (new XMLSerializer()).serializeToString($title[0]);
// result:
// '<title><span style="color:#ffff00;"><strong>Title</strong></span></title>'
Then we have to feed this HTML string to jQuery so new, real HTML elements can be created from it:
$("#target").append(html);
There is a fiddle to show this in action: http://jsfiddle.net/Tomalak/QWHj8/. This example also gets rid of the superfluous <title> element.
Anyway. If you have a chance to influence the XML itself, it would make sense to change it:
<banner-ad>
<title><span style="color:#ffff00;"><strong>Title</strong></span></title>
</banner-ad>
Just XML-encode the payload of <title> and you can do this in jQuery:
$("#target").append( $(doc).find("title").text() );
This would probably work:
$(responseXml).find("title").html();

Need to print out all links on a sidebar in selenium (xpath?)

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.