Xpath array for child elements - arrays

I have a an xpath code which returns multiple variable child elements
i want to be able to read each child element.
Is it possible to create an array or is there some other way to pass a wildcard into xpath to pull all child elements?
Example:
<changeSet>
<item></item>
<item></item>
<item></item>
</changeSet>
The number of items could vary.
I would like to be able to read all items that might show up when i run the query.

Well with pure XPath 1.0 the expression /changeSet/item returns a set of all item element nodes that are children of the changeSet root element. Or /changeSet/* returns all child elements of the changeSet root elements. Of course depending on the XPath API you use you might get acollection or a list or array of nodes or in the worst case only the string value of the first selected node. In that case you need to look at the XPath API you use, it should offer an option to return a collection of nodes.

Related

need xpath where the next descendant that has text is returned

I am trying to write a locator where the next text descendant is returned. I wont know the text. The following xpath works:
//*[#id='myChart']//label[contains(text(),"Show:")]/following::div[4]
but I dont like the div[4] as this could easily change. The element is the first div type descendant under show that contains text. Any suggestions?
A
Considering the following clauses:
the next text descendant
I wont know the text
div[4] as this could easily change
element is the first div type descendant
To locate the element a couple of effective approaches are as follows:
Using xpath:
//*[#id='myChart']//label[contains(., "Show")]//div[text()]
Using xpath with descendant:
//*[#id='myChart']//label[contains(., "Show")]//descendant::div[text()]
Using xpath with following:
//*[#id='myChart']//label[contains(., "Show")]//following::div[text()]
I think this will work for you:
//*[#id='myChart']//label[contains(text(),"Show:")]//div[text()]
To give more confident answer we need to see the actual page / XML.
In case the desired div is a direct child of the label containing the "Show:" the above expression can be presided to
//*[#id='myChart']//label[contains(text(),"Show:")]/div[text()]

How to pass a variable in Xpath of an element identified using #FindBy annotation

I am using Page Object Model (POM)for automating an application
I have identified elements using #FindBy annotation which has List of elements. Now, I want to iterate through all the elements. How can I do that.
Create a string array with the list of xpaths. Loop through the array and pass the xpath at (i) into the Find by variable.

Selecting first element verifying a condiction using xPath in libxml

In libxml, I try to select the first element to verify a condition using xpath. If I understood correctly, "//div[contains(#id,'art')][1]" in xpath would give just one element, though I get more than one of them. I use the function getnodeset in the libxml tutorial (see here). Here is the code :
xmlXPathObjectPtr result=getnodeset(def,(xmlChar*) "//div[contains(#id,'art')][1]"); // where def is a htmlDocPtr
xmlNodeSetPtr nodeset;
if(result)
{
nodeset=result->nodesetval;
if(nodeset->nodeNr>1)
fprintf(stderr,"%i first div with id attribute *art* : %s\n",nodeset->nodeNr,nomDef);
}
Instead of
"//div[contains(#id,'art')][1]"
you want
"(//div[contains(#id,'art')])[1]"
The reason has to do with binding precedence. As you probably know, [1] is shorthand for [position() = 1]. In the variant that you were trying to use, this means "when the current node (the div element) is the first child of its parent". Clearly, there could be many such divs that are each the first child of their respective parent.
When you put parentheses around the expression //div[predicate] and append [1] to that, then you're asking the question you intended to ask: what is the first node in the nodeset selected by //div[predicate]?

How to easily grab an array of children from a batch node by tag

I know that I can get an array of all the children of a CCSpriteBatchNode by using its children property, but can I get an array easily of just the subset of children that share a common tag?
What I do now is:
Get the array of the children of the batch node
Make a new array for the children with the tag of interest
Iterate through the children, and if the individual child has that tag, add it to the new array
Seems kind of cumbersome so I thought there might be a way to to it easily. If you just want a single child, you can use getChildByTag I think...
That's the way to do it.
However you can (and should) initialize an array with the children that use the same tag in your class, and every time you add a child with that tag you'll also add it to the "childsWithTagX" array. Same for removing. That way you have an always up-to-date separate children array containing only nodes with a given tag.
I think I'll have to add this as a feature to the Kobold2D Roadmap. I needed that a few times already.

ExtJS Find Node within Tree Branch

I'm trying to check whether a certain node exists beneath a branch of an ExtJS tree. Knowing the ID of the parent node, is there a library function to check whether a node exists beneath the parent (by its ID)?
I've checked the API numerous times over, and can only seem to accomplish this by iterating through the entire branch of the tree.
Is there a library function which allows me to check if a child exists (by its ID) if the parent node ID is known?
Thanks!
PS, to find the parent ID, I'm using the following:
tree.getNodeById('myID');
Ext.tree.TreeNode "contains" function does exactly what you want:
var parent = tree.getNodeById('myID');
parent.contains(tree.getNodeById('childId'));
Have you looked at DomQuery? The API defines the method jsSelect: selects a group of elements.
jsSelect( String selector, [Node/String root] ) : Array
Parameters:
selector : String
The selector/xpath query (can be a comma separated list of selectors)
root : Node/String
(optional) The start of the query (defaults to document).
Returns an Array of DOM elements which match the selector. If there are no matches, and empty Array is returned.

Resources