XPath to select element based on childs child value
Date : March 29 2020, 07:55 AM
wish helps you Trying to select an element based on the value of one of it's childrens childrens , Almost there. In your predicate, you want a relative path, so change ./book[/author/name = 'John']
./book[author/name = 'John']
./book[./author/name = 'John']
|
PHP : Rename keys of associative array to the value of a child element when given the childs key name
Date : March 29 2020, 07:55 AM
this will help Is there a PHP function I've missed that will change the keys of the parent array when given the key name of its child(associative array) or is there at least an alternative to a foreach loop which i am using at the moment to change the keys. , If I understand the question correctly, something like: $arr = array_combine(
array_column($arr, 'name'),
$arr
);
array(3) {
["one"]=>
array(2) {
["id"]=>
int(1)
["name"]=>
string(3) "one"
}
["two"]=>
array(2) {
["id"]=>
int(2)
["name"]=>
string(3) "two"
}
["three"]=>
array(2) {
["id"]=>
int(3)
["name"]=>
string(5) "three"
}
}
|
How do you recursively select all child elements under a parent element?
Tag : tsql , By : user167963
Date : March 29 2020, 07:55 AM
Any of those help Let's say we've got this table: Declare @YourTable Table ([NAME] varchar(50),[ID] varchar(50),[ManagerID] varchar(50))
Insert Into @YourTable Values
('John',1,3)
,('Sally',2,3)
,('Paul',3,4)
,('Jane',4,5)
,('Jennifer',5,8)
,('Boss',8,null)
Declare @Top int = 4 --<< NULL for Full Hier
Declare @Nest varchar(25) = '|-----' --<< Optional: Added for readability
;with cteP as (
Select Seq = cast(10000+Row_Number() over (Order by Name) as varchar(500))
,ID
,ManagerId
,Lvl=1
,Name
From @YourTable
Where IsNull(@Top,-1) = case when @Top is null then isnull(ManagerId ,-1) else ID end
Union All
Select Seq = cast(concat(p.Seq,'.',10000+Row_Number() over (Order by r.Name)) as varchar(500))
,r.ID
,r.ManagerId
,p.Lvl+1
,r.Name
From @YourTable r
Join cteP p on r.ManagerId = p.ID)
Select A.ID
,A.ManagerId
,A.Lvl
,Name = Replicate(@Nest,A.Lvl-1) + A.Name
From cteP A
Order By Seq
ID ManagerId Lvl Name
4 5 1 Jane
3 4 2 |-----Paul
1 3 3 |-----|-----John
2 3 3 |-----|-----Sally
|
recursively get CSS from an element and all the childs
Date : March 29 2020, 07:55 AM
help you fix your problem i came up with a solution that maybe give you idea about how improve your code. In order to test driving this code I've made an element that have some children in different depths and this code traverse all children by their depth in recursive way to find/get their css. After that, all founded css plus the element name will storage in an object (JSON like) for later use. Please Note: one : {
display: "block",
position: "relative"
}
two : {
display: "inline-block",
font-family: "Montserrat"
}
three_1 : {
display: "table",
position: "absolute",
left: "0px"
}
four_1 : {
display: "table-cell",
position: "relative"
}
three_2 : {
display: "table",
position: "absolute",
right: "0px"
}
four_2 : {
display: "table-cell",
position: "relative"
}
<div class="one">
<div class="two">
<div class="three_1">
<div class="four_1"></div>
</div>
<div class="three_2">
<div class="four_2"></div>
</div>
</div>
</div>
.one {display:block;position:relative;}
.two {display:inline-block;font-family:'Montserrat';}
.three_1 {display:table;position:absolute;left:0;}
.three_2 {display:table;position:absolute;right:0;}
.four_1 {display:table-cell;position:relative;}
.four_2 {display:table-cell;position:relative;}
function convertObjlike(css) {
var s = {};
if (!css) return s;
css = css.split("; ");
for (var i in css) {
var l = css[i].split(": ");
s[l[0].toLowerCase()] = (l[1]);
}
return s;
}
function getCss(a) {
var sheets = document.styleSheets, o = {};
for (var i in sheets) {
var rules = sheets[i].rules || sheets[i].cssRules;
for (var r in rules) {
if (a === rules[r].selectorText) {
o = convertObjlike(rules[r].style.cssText);
}
}
}
return o;
}
var anObject = {};
function styleRecursive(element){
anObject[element.className] = (getCss('.'+element.className));
var children = element.children;
for (var i = 0; i < children.length; i++) {
styleRecursive(children[i])
}
}
styleRecursive( document.querySelector('.one') );
console.log(anObject);
|
JS Recursively find element in parent-child array
Date : March 29 2020, 07:55 AM
I wish this helpful for you The result of the recursive call must be returned, for it to propagate up to the point of invocation and be returned as the result. function findNode(nodes, predicate) {
for (const node of nodes) {
if (predicate(node)) return node
if (node.rows) {
let match = findNode(node.rows, predicate)
if (match) return match
}
}
}
const tree = [{
value: 'a',
rows: [{
value: 'b'
}]
}, {
value: 'c',
rows: [{
value: 'd',
rows: [{
value: 'e'
}]
}]
}]
const result = findNode(tree, ({value}) => value === 'e')
console.log(result)
|