How to Select Sibling Nodes

Is there a way to select sibling nodes?

Well... sure... just access the parent and then the children.

 node.parentNode.childNodes[]

or... using jQuery:

$('#innerId').siblings()

Edit: Cletus as always is inspiring. I dug further. This is how jQuery gets siblings essentially:

function getChildren(n, skipMe){
var r = [];
for ( ; n; n = n.nextSibling )
if ( n.nodeType == 1 && n != skipMe)
r.push( n );
return r;
};

function getSiblings(n) {
return getChildren(n.parentNode.firstChild, n);
}

Select a XML node based on a sibling node

As Kirill Polishchuk said in a comment, my XPath was correct.

What I left out of the example XML was the key to the solution... Namespace!

I found my answer on this other question: Using Xpath With Default Namespace in C#

Select sibling at n distance from index (JavaScript)

You can use the parent.children array aswell as the Array.prototype.indexOf function.

function strideSiblings(element, stride){
var parent = element.parentNode;
var index = Array.prototype.indexOf.call(parent.children, element);
return parent.children[index + stride];
}

var el = document.getElementById("div-3");
var stridedSibling = strideSiblings(el, -2);
var stridedSibling1 = strideSiblings(el, 1);

console.log(stridedSibling);
console.log(stridedSibling1);
<div id="parent">
<div id="div-1">Here is div-1</div>
<div id="div-2">Here is div-2</div>
<div id="div-3">Here is div-3</div>
<div id="div-4">Here is div-4</div>
<div id="div-5">Here is div-5</div>
</div>

xpath: How to select only nearby parent siblings of current child node?

Given the limitations of XPath 1.0 and the assumption that the order of the sets of five w:r elements is consistent you could use a somewhat brute-force approach like:

(
(../preceding-sibling::w:r[w:fldChar/@w:fldCharType='begin'])[last()],
(../following-sibling::w:r[w:fldChar/@w:fldCharType='separate'])[position() = 1],
(../following-sibling::w:r[w:t])[position() = 1],
(../following-sibling::w:r[w:fldChar/@w:fldCharType='end'])[position() = 1]
)

Which is forming a sequence of four elements:

  • the last of the preceding "begin" elements
  • the first of the following "separate" elements
  • the first of the following "t" elements (w:r containing a w:t)
  • the first of the following "end" elements

If you also need to include the immediate parent of your context, then just add .. or parent::* or parent::w:r to the sequence:

(
(../preceding-sibling::w:r[w:fldChar/@w:fldCharType='begin'])[last()],
..,
(../following-sibling::w:r[w:fldChar/@w:fldCharType='separate'])[position() = 1],
(../following-sibling::w:r[w:t])[position() = 1],
(../following-sibling::w:r[w:fldChar/@w:fldCharType='end'])[position() = 1]
)

However, this may not be efficient if your input document is huge or performance is a significant factor.

If you are curious why your attempt returned three elements instead of two it is probably because you selected both "end" elements that were beyond the position of your context. The answer here may not be precisely what you need, but I suspect the last()/position() predicate examples will set you in the right direction.

BTW, if you ever have the opportunity to use XQuery for something like this, it is a perfect use case for the XQuery 3 "tumbling window" syntax which makes it very easy to identify the boundaries of sibling groups and iterate over the groups.

Select sibling node value based on multiple sibling conditions

You are almost correct in your attempts. I think the use of the XmlNodeList is what was causing your problems. If you switch to using an XmlNode and SelectSingleNode then you would've had the answer sooner.

    Dim root As XmlNode = objXml.DocumentElement
Dim node As XmlNode = root.SelectSingleNode("//Products/Product/Images[Type='IMAGE' and Key='XS']/Url")
Dim url As String = node.InnerText

xPath: select a range of sibling nodes until a specific sibling node

How are you using the xpath?

If you are merely looping through the supercategories and thus know their positions and can build the xpath dynamically you can simply count the preceding subercategories.

//li[@class="supercategory"][position()=2]/following-sibling::li[@class ="subcategory"][count(preceding-sibling::li[@class='supercategory']) = 2]

CSS selector: how to select non-element sibling node?

You can't. CSS selectors can only select element nodes. This is why the adjacent sibling combinator works as described.

If you need to apply styles, try applying them to the div and overriding them in the label. This does depend on what styles you're applying, of course, since some of the styles can't be undone or are subject to inheritance.



Related Topics



Leave a reply



Submit