Get parent element name in XPath - dom

I am trying to figure out how to get the name of the parent from a text node's scope.
//text()[name(parent)='p']
How can you get the name of the current node's parent?

If you're trying to test the name, you almost had it:
//text()[name(parent::*)='p']
If you're trying to return the name:
name(//text()/parent::*)

FYI, point of terminology: a text node is not an element.
Anyway, the most succinct way to select the parent of the current node is ..
So, the name of the parent element of the current node (which could be a text node) is name(..)
Substituting that into your XPath expression:
//text()[name(..)='p']
But a less roundabout way to write that would be
//p/text()
(assuming the p elements in the document have no namespace prefix). Either way, you're selecting all text nodes that are children of elements named p.

//text/..[#name='p']
This will get all parents of <text> nodes as long as the parent has a name attribute of p.

Related

how to know if a dom element is a certain type of tag

Sorry I am pretty new to programming so bear with me.
An example is if I want to know if a dom node is a p or a h1 tag.
I can't write if(node === 'h1') since the node is not represented as a string but actual tags.
Let's think something first. Suppose you have the node, now you want to know what tag the node is, right? There is a question, how have you gotten that node? If you have gotten that node by a tag name then you already know the tag name. But if you have gotten that node by id or class or something else then you can find the tag name by simply calling the tagName property.
Something like:
var node = document.getElementById("node_id");
node.tagName;
This will give you the tag name you want(e.g.H1, P, H2 etc).
Best of luck

Access jcr:content propeties from page node

I have dropdown where the values of options are like jcr:content/jcr:title, jcr:content/jcr:description, /jcr:content/par/entry/text etc. Here the last one is the property of parent, like jcr:title is the property of jcr:content node & text is the property of entry node, but entry has parent par and par has parent jcr:content. I am at the page node and using the below code to fetch such values which doesn't work :
Node n = (Node)nodeIter.next();
log.info(n.getProperty("/jcr:content/par/entry/text"));
Any Idea how to get values in such a way.
Thanks
Remove the starting slash from the property path. It makes the path absolute while you are interested in the relative one (as in other examples you've described):
n.getProperty("jcr:content/par/entry/text");

In CQ How disable the topmost node of a tag

Is there any way to disable the topmost node of a tag when I am adding a tag.
It is like suppose I have a dialog in which there is a field with xtype= tags and the tag is XYZ under which A, B, C are child nodes.
I want the author to select only A or B or C. He can not select XYZ.
How can I implement this. in the tagging_field.js
Thanks.
Sorry for replying after long time, this might be helpful for others. This can be achieved something like this, if the user/author selects xyz tag the value gets added to the tag field, you can get the array of values added in the tag field using getValue method and if the array contains the xyz value delete the value and setValue back to the tag field using setValue method
Here is the link to API docs for tag field for you to refer. Tag Field API Docs
Regards,
Yash Ahuja.

get sibling node without relying on parent index?

I find it easy to get the text values inside cells whose row has a reliable attribute, eg. $browser.tr(:class, "datarow2_sm")
However I also need to grab the data from the very next <tr> in the table, defined only as <tr class="">.
Its HTML contents don't have anything very unique either, watir-speaking.
One reluctant method to catch that row is:
cell1value = $browser.tr(:class, "datarow2_sm").parent[3][1].text
cell2value = $browser.tr(:class, "datarow2_sm").parent[3][2].text.to_f # etc.
But I don't want to rely on a fixed index [3] as such things in the wider table may shift. In addition to .parent is there anything like .sibling (.next/*.previous* like in Mechanize)? Perhaps one that would prefer a node of the same type (tr to another tr not a td or some non-row node in the DOM)?
You could use the css adjacent sibling selector. Note that Watir-Webdriver only currently supports css selectors for elements node.
You would do the following (noting that the to_subtype is to convert it back to a TableRow rather than Element):
puts b.element(:css, "tr.datarow2_sm + tr").to_subtype.text
Update
If you want to get the second cell in that next row, you can do one of the following:
puts b.element(:css, "tr.datarow2_sm + tr").to_subtype[1].text
puts b.element(:css, "tr.datarow2_sm + tr").td(:index, 1).text
CSS has a special selector for this called adjacent sibling selector:
Adjacent sibling selectors have the following syntax: E1 + E2, where
E2 is the subject of the selector. The selector matches if E1 and E2
share the same parent in the document tree and E1 immediately precedes
E2, ignoring non-element nodes (such as text nodes and comments).

Accessing value attribute in Protovis lines

I'm using Protovis Arc layout and I'd like to color links between nodes accoriding to the 'value' property defined in dataset. How can I access it?
Dataset is defined like that:
Nodes:
...
{nodeName:"Books"}
...
Links:
...
{source:1, target:4, value:20}
...
arc.link.add(pv.Line).strokeStyle(function(d) d.value > 10 ? "#cc0000" : "#eeeeee"); - does not work
The d property refers to the node. There's no value attribute defined on the node in this case; the link weights are defined on the links, which is why the property function isn't doing what you expect.
You can rewrite your property function to access the link (rather than node) data. The link data is associated with the link's parent panel, and is available as the second argument:
.strokeStyle(function(d, p) p.value > 10 ? "#c00" : "#eee")
There's more of an explanation in the layout documentation. And see also the pv.Layout.Network API reference:
The link mark is added to a child
panel, whose data property is
defined as layout's links property.
The link's data property is then a
two-element array of the source node
and target node. Thus, poperties such
as strokeStyle and fillStyle can
be overridden to compute properties
from either the node data (the first
argument) or the link data (the second
argument; the parent panel data)
dynamically.