David Carlisle
> I am unsure about the correct form of the negated expression to
> mandate that no child may have that attribute. Which variant can or
> must be used, and why?
>
> every $child in * satisfies $child[not(@my_attribute)]
The simplest way of negating an expression is to put not around it so if
every $child in * satisfies $child[@my_attribute]
is the positive test
not(every $child in * satisfies $child[@my_attribute]) is the negative test, and if that's the simplest for you to grock later
than that's what I'd use. Personally I'd have probably written the "positive test" in negative
form, rather than saying every chiuld has teh attribute, say no child
doesn't have the attribute
not(*[not(@my_attribute)])
which is also valid xpath 1 of course.
This would make your "negative" test
exists(*[not(@my_attribute)]
Your other two expressions are valid but mean something else.
every $child in * satisfies $child[not(@my_attribute)]
every $child in * satisfies not($child/@my_attribute) are true if every element does not have the attribute.
However none of the tests seem to capture what you say in english
> all of its element children carry a specific attribute or if they don't. A
> mixed situation is to be considered as a fatal error, caught in an
> xsl:otherwise clause.
the tests you show test that all the elements have or dont have the
attribute, but only test for one or the other, they don't test for the
mixed case.
The most direct test for the mixed test is just to test
test="*[@my_attribute] and *[not(@my_attribute)]"
> Thank you very much for this hint - I actually need a test that returns
> true if all children carry the attribute AND if there are element
> children at all - maybe David's suggestion might help? (See below.)
there are always many ways of phrasing these things. It's best just to
say them in english (or German or any other language of choice) in a way
that makes most sense to you, and then write the xpath the same
way. It's possible that some xpath variants are more efficient than
others, but it's also possible that the difference isn't measurable, or
that the compiler re-writes the expressions to the same internal form
whatever you do, so I wouldn't worry too much about which form you use. |