Returns the child node index of the child element at the given path entry, if any, and -1 otherwise.
Returns the child node index of the child element at the given path entry, if any, and -1 otherwise. The faster this method is, the better.
Returns the child nodes of this element, in the correct order
Finds the child element with the given Path.Entry
(where this element is the root), if any, wrapped in an Option
.
Finds the child element with the given Path.Entry
(where this element is the root), if any, wrapped in an Option
.
Typically this method must be very efficient, in order for methods like findElemOrSelfByPath to be efficient.
Finds the element with the given Path
(where this element is the root), if any, wrapped in an Option
.
Finds the element with the given Path
(where this element is the root), if any, wrapped in an Option
.
That is, returns:
findReverseAncestryOrSelfByPath(path).map(_.last)
Note that for each non-empty Path, we have:
findElemOrSelfByPath(path) == findChildElemByPathEntry(path.firstEntry) flatMap (e => e.findElemOrSelfByPath(path.withoutFirstEntry))
Finds the reversed ancestry-or-self of the element with the given Path
(where this element is the root),
wrapped in an Option.
Finds the reversed ancestry-or-self of the element with the given Path
(where this element is the root),
wrapped in an Option. None is returned if no element can be found at the given Path.
Hence, the resulting element collection, if any, starts with this element and ends with the element at the given Path, relative to this element.
This method comes in handy for (efficiently) computing base URIs, where the (reverse) ancestry-or-self is needed as input.
Returns (the equivalent of) findChildElemByPathEntry(entry).get
Returns (the equivalent of) findChildElemByPathEntry(entry).get
Returns (the equivalent of) findElemOrSelfByPath(path).get
Returns (the equivalent of) findElemOrSelfByPath(path).get
Returns (the equivalent of) findReverseAncestryOrSelfByPath(path).get
Returns (the equivalent of) findReverseAncestryOrSelfByPath(path).get
Returns a copy in which the child at the given position (0-based) has been removed.
Returns a copy in which the child at the given position (0-based) has been removed.
Throws an exception if index >= children.size
.
Returns a copy in which the given child has been inserted at the end
Returns a copy in which the given child has been inserted at the given position (0-based).
Returns a copy in which the given child has been inserted at the given position (0-based).
If index == children.size
, adds the element at the end. If index > children.size
, throws an exception.
Afterwards, the resulting element indeed has the given child at position index
(0-based).
Returns a copy in which the given child, if any, has been inserted at the end.
Returns a copy in which the given child, if any, has been inserted at the end.
That is, returns plusChild(childOption.get)
if the given optional child element is non-empty.
Returns a copy in which the given child, if any, has been inserted at the given position (0-based).
Returns a copy in which the given child, if any, has been inserted at the given position (0-based).
That is, returns plusChild(index, childOption.get)
if the given optional child element is non-empty.
Returns updated(path) { e => newElem }
Method that "functionally updates" the tree with this element as root element, by applying the passed function to the element that has the given eu.cdevreeze.yaidom.core.Path (compared to this element as root).
Method that "functionally updates" the tree with this element as root element, by applying the passed function to the element that has the given eu.cdevreeze.yaidom.core.Path (compared to this element as root).
The method throws an exception if no element is found with the given path.
It can be defined (recursively) as follows:
if (path == Path.Root) f(self) else updated(path.firstEntry) { e => e.updated(path.withoutFirstEntry)(f) }
Core method that "functionally updates" the tree with this element as root element, by applying the passed function to the element that has the given eu.cdevreeze.yaidom.core.Path.Entry (compared to this element as root).
Core method that "functionally updates" the tree with this element as root element, by applying the passed function to the element that has the given eu.cdevreeze.yaidom.core.Path.Entry (compared to this element as root).
The method throws an exception if no element is found with the given path entry.
It can be defined as follows:
val idx = self.childNodeIndex(pathEntry)
self.withUpdatedChildren(idx, f(children(idx).asInstanceOf[E]))
Method that "functionally updates" the tree with this element as root element, by applying the passed function to all child elements with the given path entries (compared to this element as root).
Method that "functionally updates" the tree with this element as root element, by applying the passed function to all child elements with the given path entries (compared to this element as root).
It can be defined as follows (ignoring exceptions):
val newChildren = pathEntries.toSeq.map(entry => (entry -> childNodeIndex(entry))).sortBy(_._2).reverse.foldLeft(children) { case (acc, (pathEntry, idx)) => acc.updated(idx, f(acc(idx).asInstanceOf[E], pathEntry)) } withChildren(newChildren)
Method that "functionally updates" the tree with this element as root element, by applying the passed function to all descendant-or-self elements with the given paths (compared to this element as root).
Method that "functionally updates" the tree with this element as root element, by applying the passed function to all descendant-or-self elements with the given paths (compared to this element as root).
It can be defined (recursively) as follows (ignoring exceptions):
def updatedAtPaths(paths: Set[Path])(f: (E, Path) => E): E = { val pathsByPathEntries = paths.filter(path => !path.isRoot).groupBy(path => path.firstEntry) val resultWithoutSelf = self.updatedAtPathEntries(pathsByPathEntries.keySet) { (che, pathEntry) => val newChe = che.updatedAtPaths(paths.map(_.withoutFirstEntry)) { (elem, relativePath) => f(elem, relativePath.prepend(pathEntry)) } newChe } if (paths.contains(Path.Root)) f(resultWithoutSelf, Path.Root) else resultWithoutSelf }
For simple elements, it is also equivalent to:
val pathsReversed = indexed.Elem(this).findAllElemsOrSelf.map(_.path).filter(p => paths.contains(p)).reverse pathsReversed.foldLeft(self) { case (acc, path) => acc.updated(path) { e => f(e, path) } }
Returns updatedWithNodeSeq(path) { e => newNodes }
"Functionally updates" the tree with this element as root element, by applying the passed function to the element that has the given eu.cdevreeze.yaidom.core.Path (compared to this element as root).
"Functionally updates" the tree with this element as root element, by applying the passed function to the element that has the given eu.cdevreeze.yaidom.core.Path (compared to this element as root). If the given path is the root path, this element itself is returned unchanged.
This function could be defined as follows:
// First define function g as follows: def g(e: Elem): Elem = { if (path == Path.Root) e else { e.withPatchedChildren( e.childNodeIndex(path.lastEntry), f(e.findChildElemByPathEntry(path.lastEntry).get), 1) } } // Then the function updatedWithNodeSeq(path)(f) could be defined as: updated(path.parentPathOption.getOrElse(Path.Root))(g)
After all, this is just a functional update that replaces the parent element, if it exists.
The method throws an exception if no element is found with the given path.
Method that "functionally updates" the tree with this element as root element, by applying the passed function to all child elements with the given path entries (compared to this element as root).
Method that "functionally updates" the tree with this element as root element, by applying the passed function to all child elements with the given path entries (compared to this element as root).
It can be defined as follows (ignoring exceptions):
val indexesByPathEntries = pathEntries.toSeq.map(entry => (entry -> childNodeIndex(entry))).sortBy(_._2).reverse val newChildGroups = indexesByPathEntries.foldLeft(self.children.map(n => immutable.IndexedSeq(n))) { case (acc, (pathEntry, idx)) => acc.updated(idx, f(acc(idx).head.asInstanceOf[E], pathEntry)) } withChildren(newChildGroups.flatten)
Method that "functionally updates" the tree with this element as root element, by applying the passed function to all descendant elements with the given paths (compared to this element as root), but ignoring the root path.
Method that "functionally updates" the tree with this element as root element, by applying the passed function to all descendant elements with the given paths (compared to this element as root), but ignoring the root path.
It can be defined as follows (ignoring exceptions):
val pathsByParentPaths = paths.filter(path => !path.isRoot).groupBy(path => path.parentPath) self.updatedAtPaths(pathsByParentPaths.keySet) { (elem, path) => val childPathEntries = pathsByParentPaths(path).map(_.lastEntry) elem.updatedWithNodeSeqAtPathEntries(childPathEntries) { (che, pathEntry) => f(che, path.append(pathEntry)) } }
Returns an element with the same name, attributes and scope as this element, but with the given child nodes
Shorthand for withChildren(children.patch(from, newChildren, replace))
Shorthand for withChildren(children.updated(index, newChild))
This is the functional update part of the yaidom uniform query API. It is a sub-trait of trait eu.cdevreeze.yaidom.queryapi.IsNavigableApi. Only a few DOM-like element implementations in yaidom mix in this trait (indirectly, because some implementing sub-trait is mixed in), thus sharing this query API.
This trait typically does not show up in application code using yaidom, yet its (uniform) API does. Hence, it makes sense to read the documentation of this trait, knowing that the API is offered by multiple element implementations.
This trait is purely abstract. The most common implementation of this trait is eu.cdevreeze.yaidom.queryapi.UpdatableElemLike. The trait has all the knowledge of its super-trait, but in addition to that knows the following:
Obviously methods
,children
andwithChildren
must be consistent with methods such aschildNodeIndex
.findAllChildElems
Using this minimal knowledge alone, trait
not only offers the methods of its parent trait, but also:UpdatableElemLike
For the conceptual difference with "transformable" elements, see trait eu.cdevreeze.yaidom.queryapi.TransformableElemApi.
This query API leverages the Scala Collections API. Query results can be manipulated using the Collections API, and the query API implementation (in
) uses the Collections API internally.UpdatableElemLike
UpdatableElemApi examples
To illustrate the use of this API, consider the following example XML:
Suppose this XML has been parsed into eu.cdevreeze.yaidom.simple.Elem variable named
. Then we can add a book as follows, where we "forget" the 2nd author for the moment:bookstoreElem
Note that the namespace declarations for prefixes
andbook
had to be repeated in the Scala XML literal for the added book, because otherwise theauth
method would throw an exception (sinceconvertToElem
instances cannot be created unless all element and attribute QNames can be resolved as ENames).Elem
The resulting bookstore seems ok, but if we print
, the result does not look pretty. This can be fixed if the last assignment is replaced by:convertElem(bookstoreElem)
bookstoreElem = bookstoreElem.plusChild(fpBookElem).prettify(2)
knowing that an indentation of 2 spaces has been used throughout the original XML. Method
is expensive, so it is best not to invoke it within a tight loop. As an alternative, formatting can be left to theprettify
, of course.DocumentPrinter
The assignment above is the same as the following one:
bookstoreElem = bookstoreElem.withChildren(bookstoreElem.children :+ fpBookElem).prettify(2)
There are several methods to functionally update the children of an element. For example, method
is overloaded, and the other variant can insert a child at a given 0-based position. Other "children update" methods areplusChild
,minusChild
andwithPatchedChildren
.withUpdatedChildren
Let's now turn to functional update methods that take
instances or collections thereof. In the example above the second author of the added book is missing. Let's fix that:Path
Clearly the resulting bookstore element is nicely formatted, but there was another possible issue that was taken into account. See the line of code transforming the "raw result". That line was added in order to prevent namespace undeclarations, which for XML version 1.0 are not allowed (with the exception of the default namespace). After all, the XML for the second author was created with only the
namespace declared. Without the above-mentioned line of code, a namespace undeclaration for prefixauth
would have occurred in the resulting XML, thus leading to an invalid XML 1.0 element tree.book
To illustrate functional update methods taking collections of paths, let's remove the added book from the book store. Here is one (somewhat inefficient) way to do that:
There are very many ways to write this functional update, using different functional update methods in trait
, or even only using transformation methods in traitUpdatableElemApi
(thus not using paths).TransformableElemApi
The example code above is enough to get started using the
methods, but it makes sense to study the entire API, and practice with it. Always keep in mind that functional updates typically mess up formatting and/or namespace (un)declarations, unless these aspects are taken into account.UpdatableElemApi
The node supertype of the element subtype
The captured element subtype