Returns all child elements, in the correct order.
Returns all child elements, in the correct order. Other operations can be defined in terms of this one.
Shorthand for filterChildElems(p)
.
Shorthand for filterChildElems(p)
. Use this shorthand only if the predicate is a short expression.
Shorthand for filterElemsOrSelf(p)
.
Shorthand for filterElemsOrSelf(p)
. Use this shorthand only if the predicate is a short expression.
Shorthand for findTopmostElemsOrSelf(p)
.
Shorthand for findTopmostElemsOrSelf(p)
. Use this shorthand only if the predicate is a short expression.
Core method that returns the child elements obeying the given predicate.
Core method that returns the child elements obeying the given predicate. This method could be defined as:
def filterChildElems(p: E => Boolean): immutable.IndexedSeq[E] = this.findAllChildElems.filter(p)
Returns the descendant elements obeying the given predicate.
Returns the descendant elements obeying the given predicate. This method could be defined as:
this.findAllChildElems flatMap (_.filterElemsOrSelf(p))
Core method that returns the descendant-or-self elements obeying the given predicate.
Core method that returns the descendant-or-self elements obeying the given predicate. This method could be defined as:
def filterElemsOrSelf(p: E => Boolean): immutable.IndexedSeq[E] = Vector(this).filter(p) ++ (this.findAllChildElems flatMap (_.filterElemsOrSelf(p)))
It can be proven that the result is equivalent to findAllElemsOrSelf filter p
.
Returns all descendant elements (not including this element).
Returns all descendant elements (not including this element). This method could be defined as filterElems { e => true }
.
Equivalent to findAllElemsOrSelf.drop(1)
.
Returns this element followed by all descendant elements (that is, the descendant-or-self elements).
Returns this element followed by all descendant elements (that is, the descendant-or-self elements).
This method could be defined as filterElemsOrSelf { e => true }
.
Returns the first found child element obeying the given predicate, if any, wrapped in an Option
.
Returns the first found child element obeying the given predicate, if any, wrapped in an Option
.
This method could be defined as filterChildElems(p).headOption
.
Returns the first found (topmost) descendant element obeying the given predicate, if any, wrapped in an Option
.
Returns the first found (topmost) descendant element obeying the given predicate, if any, wrapped in an Option
.
This method could be defined as filterElems(p).headOption
.
Returns the first found (topmost) descendant-or-self element obeying the given predicate, if any, wrapped in an Option
.
Returns the first found (topmost) descendant-or-self element obeying the given predicate, if any, wrapped in an Option
.
This method could be defined as filterElemsOrSelf(p).headOption
.
Returns the descendant elements obeying the given predicate that have no ancestor obeying the predicate.
Returns the descendant elements obeying the given predicate that have no ancestor obeying the predicate. This method could be defined as:
this.findAllChildElems flatMap (_.findTopmostElemsOrSelf(p))
Core method that returns the descendant-or-self elements obeying the given predicate, such that no ancestor obeys the predicate.
Core method that returns the descendant-or-self elements obeying the given predicate, such that no ancestor obeys the predicate. This method could be defined as:
def findTopmostElemsOrSelf(p: E => Boolean): immutable.IndexedSeq[E] = if (p(this)) Vector(this) else (this.findAllChildElems flatMap (_.findTopmostElemsOrSelf(p)))
Returns the single child element obeying the given predicate, and throws an exception otherwise.
Returns the single child element obeying the given predicate, and throws an exception otherwise.
This method could be defined as findChildElem(p).get
.
API and implementation trait for elements as containers of elements, as element nodes in a node tree. This trait knows very little about elements. It does not know about names, attributes, etc. All it knows about elements is that elements can have element children (other node types are entirely out of scope in this trait).
Most users of the yaidom API do not use this trait directly, so may skip the documentation of this trait.
Based on an abstract method returning the child elements, this trait offers query methods to find descendant-or-self elements, topmost descendant-or-self elements obeying a predicate, and so on.
Concrete element classes, such as eu.cdevreeze.yaidom.Elem and eu.cdevreeze.yaidom.resolved.Elem (and even eu.cdevreeze.yaidom.ElemBuilder), indeed mix in this trait (directly or indirectly), thus getting an API and implementation of many such query methods.
Subtraits like eu.cdevreeze.yaidom.ElemLike implement many more methods on elements, based on more knowledge about elements, such as element names and attributes. It is subtrait
UpdatableElemLike
that is typically mixed in by element classes. The distinction betweenParentElemLike
and its subtraits is still useful, because this trait implements methods that only need knowledge about elements as parent nodes of other elements. In an abstract sense, this trait could even be seen as an API that has nothing to do with elements in particular, but that deals with trees (XML or not) in general (if we were to rename the trait, its methods and the type parameter).The concrete element classes that mix in this trait (or a sub-trait) have knowledge about all child nodes of an element, whether these child nodes are elements or not (such as text, comments etc.). Hence, this simple element-centric
ParentElemLike
API can be seen as a good basis for querying arbitrary nodes and their values, even if this trait itself knows only about element nodes.For example, using class eu.cdevreeze.yaidom.Elem, which also mixes in trait eu.cdevreeze.yaidom.HasText, all normalized text in a tree with document element
root
can be found as follows:root.findAllElemsOrSelf map { e => e.normalizedText }
or:
As another example (also using the
HasText
trait as mixin), all text containing the string "query" can be found as follows:or:
ParentElemLike more formally
The only abstract method is
findAllChildElems
. Based on this method alone, this trait offers a rich API for querying elements. This is entirely consistent with the semantics defined in theParentElemApi
trait. Indeed, the implementation of the methods follows the semantics defined there.In the
ParentElemApi
trait, some (simple) provable laws were mentioned. Some proofs follow below.1. Proving property about filterElemsOrSelf
Below follows a proof by structural induction of one of the laws mentioned in the documentation of trait
ParentElemApi
.First we make a few assumptions, for this proof, and (implicitly) for the other proofs:
Based on these assumptions, we prove by induction that:
Base case
If
elm
has no child elements, then the LHS can be rewritten as follows:which is the RHS.
Inductive step
For the inductive step, we use the following (general) properties:
(xs.filter(p) ++ ys.filter(p)) == ((xs ++ ys) filter p) // referred to below as property (a)
and:
If
elm
does have child elements, the LHS can be rewritten as:which is the RHS.
This completes the proof. Other above-mentioned properties can be proven by induction in a similar way.
2. Proving property about filterElems
From the preceding proven property it easily follows (without using a proof by induction) that:
After all, the LHS can be rewritten as follows:
which is the RHS.
3. Proving property about findTopmostElemsOrSelf
Given the above-mentioned assumptions, we prove by structural induction that:
Base case
If
elm
has no child elements, andp(elm)
holds, then LHS and RHS evaluate toimmutable.IndexedSeq(elm)
.If
elm
has no child elements, andp(elm)
does not hold, then LHS and RHS evaluate toimmutable.IndexedSeq()
.Inductive step
For the inductive step, we introduce the following additional (general) property, if
f
andg
have the same types:If
elm
does have child elements, andp(elm)
holds, the LHS can be rewritten as:which is the RHS. In this case, we did not even need the induction hypothesis.
If
elm
does have child elements, andp(elm)
does not hold, the LHS can be rewritten as:which is the RHS.
4. Proving property about findTopmostElems
From the preceding proven property it easily follows (without using a proof by induction) that:
After all, the LHS can be rewritten to:
which is the RHS.
5. Properties used in the proofs above
There are several (unproven) properties that were used in the proofs above:
No proofs are offered here, but we could prove them ourselves. It would help to regard
xs
andys
above asList
instances, and use structural induction for Lists (!) as proof method. First we would need some defining clauses forfilter
,++
andflatMap
:Property (a) could then be proven by structural induction (for lists), by using only defining clauses and no other proven properties. Property (b) could then be proven by structural induction as well, but (possibly) requiring property (a) in its proof. Property (c) could be proven by structural induction, if we would first prove the distribution law for
flatMap
over concatenation. The proof by structural induction of the latter property would (possibly) depend on the property that concatenation is associative. These proofs are all left as exercises for the reader, as they say. Yaidom considers these properties as theorems based on which "yaidom properties" were proven above.Implementation notes
Methods
findAllElemsOrSelf
,filterElemsOrSelf
,findTopmostElemsOrSelf
andfindElemOrSelf
use recursion in their implementations, but not tail-recursion. The lack of tail-recursion should not be a problem, due to limited XML tree depths in practice. It is comparable to an "idiomatic" Scala quicksort implementation in its lack of tail-recursion. Also in the case of quicksort, the lack of tail-recursion is acceptable due to limited recursion depths. If we want tail-recursive implementations of the above-mentioned methods (in particular the first 3 ones), we either lose the ordering of result elements in document order (depth-first), or we lose performance and/or clarity. That just is not worth it.The captured element subtype