Find preceding-sibling with specific name and without specific attribute
-
20-12-2019 - |
質問
I have a pretty big test checking order of things and I want to ignore baz
elements with an attribute attri
that is equal to y
, this is what I've tried, but it doesn't seem to be working correctly. It seems to be ignoring baz
regardless of attribute:
Context:
jd:foo|jd:bar|jd:baz[@attri != 'y']|jd:bam
Test:
(name()='foo' and
(preceding-sibling::*[local-name()='bar']
or preceding-sibling::*[local-name()='baz' and @attri != 'y']
or preceding-sibling::*[local-name()='bam']
)
) or
(name()='bar' and
(preceding-sibling::*[local-name()='baz' and @attri != 'y']
or preceding-sibling::*[local-name()='bam']
)
) or
(name()='baz' and
(preceding-sibling::*[local-name()='bam']
)
)
解決
Your context excludes not only baz
elements that contain @attri
equal to y
, but also baz
elements that contain no atrribute. This:
jd:baz[@attri != 'y']
says that baz
must have an @attri
attribute, and it also must not be y
. So a node such as <baz>...</baz>
will not be part of the context because it does not contain the attribute. This will include in the context elements like <baz attri='z'>
.
If you wish to exclude only the <baz attri='y'>
elements (but not the ones that don't have that attribute), you have to use:
baz[not(@attri = 'y')]
which selects all baz
elements that have @attri='y'
and negates that set. This will include elements that do not have a @attri
attribute.
The same applies to your test, if elements without the attribute are to be considered.