Combining the use of preceding- and following-sibling in the same XPath query up to first instance
Following the post here Combining the use of preceding and following sibling in the same xpath query I am wanting to do the same except I want to get nodes up to the first occurrence of p2.
So the modified example data is
<a>
<b property="p1">zyx</b>
<b>wvu</b>
<b>tsr</b>
<b>dcv</b>
<b property="p2">qpo</b>
<b>qcs</b>
<b property="p2">wee</b>
<b>tbg</b>
<b>rty</b>
<b property="p2">qwe</b>
<b>jkl</b>
</a>
I have tried
/a/b[preceding-sibling::b/@property='p1' and following-sibling::b[1]/@property='p2']
which gives me dcv, qcs, and rty.
Then I tried
/a/b[preceding-sibling::b/@property='p1' and (following-sibling::b/@property='p2')[1]]
which gives me wvu, tsr, dcv, qpo, qcs, wee, tbg, and rty.
What I actually need is from p1 to the first p2 or wvu, tsr, and dcv. I have tried almost every combination of [1] but have failed and would like some help on the construct.
xpath siblings find-occurrences
add a comment |
Following the post here Combining the use of preceding and following sibling in the same xpath query I am wanting to do the same except I want to get nodes up to the first occurrence of p2.
So the modified example data is
<a>
<b property="p1">zyx</b>
<b>wvu</b>
<b>tsr</b>
<b>dcv</b>
<b property="p2">qpo</b>
<b>qcs</b>
<b property="p2">wee</b>
<b>tbg</b>
<b>rty</b>
<b property="p2">qwe</b>
<b>jkl</b>
</a>
I have tried
/a/b[preceding-sibling::b/@property='p1' and following-sibling::b[1]/@property='p2']
which gives me dcv, qcs, and rty.
Then I tried
/a/b[preceding-sibling::b/@property='p1' and (following-sibling::b/@property='p2')[1]]
which gives me wvu, tsr, dcv, qpo, qcs, wee, tbg, and rty.
What I actually need is from p1 to the first p2 or wvu, tsr, and dcv. I have tried almost every combination of [1] but have failed and would like some help on the construct.
xpath siblings find-occurrences
add a comment |
Following the post here Combining the use of preceding and following sibling in the same xpath query I am wanting to do the same except I want to get nodes up to the first occurrence of p2.
So the modified example data is
<a>
<b property="p1">zyx</b>
<b>wvu</b>
<b>tsr</b>
<b>dcv</b>
<b property="p2">qpo</b>
<b>qcs</b>
<b property="p2">wee</b>
<b>tbg</b>
<b>rty</b>
<b property="p2">qwe</b>
<b>jkl</b>
</a>
I have tried
/a/b[preceding-sibling::b/@property='p1' and following-sibling::b[1]/@property='p2']
which gives me dcv, qcs, and rty.
Then I tried
/a/b[preceding-sibling::b/@property='p1' and (following-sibling::b/@property='p2')[1]]
which gives me wvu, tsr, dcv, qpo, qcs, wee, tbg, and rty.
What I actually need is from p1 to the first p2 or wvu, tsr, and dcv. I have tried almost every combination of [1] but have failed and would like some help on the construct.
xpath siblings find-occurrences
Following the post here Combining the use of preceding and following sibling in the same xpath query I am wanting to do the same except I want to get nodes up to the first occurrence of p2.
So the modified example data is
<a>
<b property="p1">zyx</b>
<b>wvu</b>
<b>tsr</b>
<b>dcv</b>
<b property="p2">qpo</b>
<b>qcs</b>
<b property="p2">wee</b>
<b>tbg</b>
<b>rty</b>
<b property="p2">qwe</b>
<b>jkl</b>
</a>
I have tried
/a/b[preceding-sibling::b/@property='p1' and following-sibling::b[1]/@property='p2']
which gives me dcv, qcs, and rty.
Then I tried
/a/b[preceding-sibling::b/@property='p1' and (following-sibling::b/@property='p2')[1]]
which gives me wvu, tsr, dcv, qpo, qcs, wee, tbg, and rty.
What I actually need is from p1 to the first p2 or wvu, tsr, and dcv. I have tried almost every combination of [1] but have failed and would like some help on the construct.
xpath siblings find-occurrences
xpath siblings find-occurrences
edited Nov 15 '18 at 23:01
zx485
15k133048
15k133048
asked Nov 15 '18 at 22:59
sinDizzysinDizzy
59951845
59951845
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
One way to achieve this is by using the following XPath-1.0 expression:
/a/b[preceding-sibling::b/@property='p1' and not(preceding-sibling::b/@property='p2' or @property='p2')]
It checks if a given b
element has a preceding-sibling named p1
and is not a preceding-sibling of p2
or p2
itself. That's all.
Very clever answer. Being relatively new to xpath i didn't know how to put it together. Many thanks.
– sinDizzy
Nov 15 '18 at 23:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329068%2fcombining-the-use-of-preceding-and-following-sibling-in-the-same-xpath-query-up%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
One way to achieve this is by using the following XPath-1.0 expression:
/a/b[preceding-sibling::b/@property='p1' and not(preceding-sibling::b/@property='p2' or @property='p2')]
It checks if a given b
element has a preceding-sibling named p1
and is not a preceding-sibling of p2
or p2
itself. That's all.
Very clever answer. Being relatively new to xpath i didn't know how to put it together. Many thanks.
– sinDizzy
Nov 15 '18 at 23:35
add a comment |
One way to achieve this is by using the following XPath-1.0 expression:
/a/b[preceding-sibling::b/@property='p1' and not(preceding-sibling::b/@property='p2' or @property='p2')]
It checks if a given b
element has a preceding-sibling named p1
and is not a preceding-sibling of p2
or p2
itself. That's all.
Very clever answer. Being relatively new to xpath i didn't know how to put it together. Many thanks.
– sinDizzy
Nov 15 '18 at 23:35
add a comment |
One way to achieve this is by using the following XPath-1.0 expression:
/a/b[preceding-sibling::b/@property='p1' and not(preceding-sibling::b/@property='p2' or @property='p2')]
It checks if a given b
element has a preceding-sibling named p1
and is not a preceding-sibling of p2
or p2
itself. That's all.
One way to achieve this is by using the following XPath-1.0 expression:
/a/b[preceding-sibling::b/@property='p1' and not(preceding-sibling::b/@property='p2' or @property='p2')]
It checks if a given b
element has a preceding-sibling named p1
and is not a preceding-sibling of p2
or p2
itself. That's all.
answered Nov 15 '18 at 23:18
zx485zx485
15k133048
15k133048
Very clever answer. Being relatively new to xpath i didn't know how to put it together. Many thanks.
– sinDizzy
Nov 15 '18 at 23:35
add a comment |
Very clever answer. Being relatively new to xpath i didn't know how to put it together. Many thanks.
– sinDizzy
Nov 15 '18 at 23:35
Very clever answer. Being relatively new to xpath i didn't know how to put it together. Many thanks.
– sinDizzy
Nov 15 '18 at 23:35
Very clever answer. Being relatively new to xpath i didn't know how to put it together. Many thanks.
– sinDizzy
Nov 15 '18 at 23:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329068%2fcombining-the-use-of-preceding-and-following-sibling-in-the-same-xpath-query-up%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown