eu.cdevreeze.yaidom.queryapi

TransformableElemApi

trait TransformableElemApi[N, E <: N with TransformableElemApi[N, E]] extends AnyRef

This is the element transformation part of the yaidom query and update API. 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 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.TransformableElemLike. That trait only knows how to transform child elements. Using this minimal knowledge, the trait offers methods to transform descendant elements and descendant-or-self elements. Indeed, the trait is similar to ElemLike, except that it transforms elements instead of querying for elements.

The big conceptual difference with "updatable" elements (in trait UpdatableElemLike[N, E]) is that "transformations" are about applying some transforming function to an element tree, while "(functional) updates" are about "updates" at given paths.

TransformableElemApi examples

To illustrate the use of this API, consider the following example XML:

<book:Bookstore xmlns:book="http://bookstore/book" xmlns:auth="http://bookstore/author">
  <book:Book ISBN="978-0321356680" Price="35" Edition="2">
    <book:Title>Effective Java (2nd Edition)</book:Title>
    <book:Authors>
      <auth:Author>
        <auth:First_Name>Joshua</auth:First_Name>
        <auth:Last_Name>Bloch</auth:Last_Name>
      </auth:Author>
    </book:Authors>
  </book:Book>
  <book:Book ISBN="978-0981531649" Price="35" Edition="2">
    <book:Title>Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition</book:Title>
    <book:Authors>
      <auth:Author>
        <auth:First_Name>Martin</auth:First_Name>
        <auth:Last_Name>Odersky</auth:Last_Name>
      </auth:Author>
      <auth:Author>
        <auth:First_Name>Lex</auth:First_Name>
        <auth:Last_Name>Spoon</auth:Last_Name>
      </auth:Author>
      <auth:Author>
        <auth:First_Name>Bill</auth:First_Name>
        <auth:Last_Name>Venners</auth:Last_Name>
      </auth:Author>
    </book:Authors>
  </book:Book>
</book:Bookstore>

Suppose this XML has been parsed into eu.cdevreeze.yaidom.simple.Elem variable named bookstoreElem. Then we can combine author first and last names as follows:

val authorNamespace = "http://bookstore/author"

bookstoreElem = bookstoreElem transformElems {
  case elem: Elem if elem.resolvedName == EName(authorNamespace, "Author") =>
    val firstName = (elem \ (_.localName == "First_Name")).headOption.map(_.text).getOrElse("")
    val lastName = (elem \ (_.localName == "Last_Name")).headOption.map(_.text).getOrElse("")
    val name = (firstName + " " + lastName).trim
    Node.textElem(QName("auth:Author"), elem.scope ++ Scope.from("auth" -> authorNamespace), name)
  case elem: Elem => elem
}
bookstoreElem = bookstoreElem.prettify(2)

When using the TransformableElemApi API, keep the following in mind:

Top-down transformations are still possible, by combining recursion with method transformChildElems (or transformChildElemsToNodeSeq). For example:

def removePrefixedNamespaceUndeclarations(elem: Elem): Elem = {
elem transformChildElems { e =>
  val newE = e.copy(scope = elem.scope.withoutDefaultNamespace ++ e.scope)
  removePrefixedNamespaceUndeclarations(newE)
}
}
N

The node supertype of the element subtype

E

The captured element subtype

Self Type
E
Linear Supertypes
AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. TransformableElemApi
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def transformChildElems(f: (E) ⇒ E): E

    Returns the same element, except that child elements have been replaced by applying the given function.

    Returns the same element, except that child elements have been replaced by applying the given function. Non-element child nodes occur in the result element unaltered.

    That is, returns the equivalent of:

    val newChildren =
      children map {
        case e: E => f(e)
        case n: N => n
      }
    withChildren(newChildren)
  2. abstract def transformChildElemsToNodeSeq(f: (E) ⇒ IndexedSeq[N]): E

    Returns the same element, except that child elements have been replaced by applying the given function.

    Returns the same element, except that child elements have been replaced by applying the given function. Non-element child nodes occur in the result element unaltered.

    That is, returns the equivalent of:

    val newChildren =
      children flatMap {
        case e: E => f(e)
        case n: N => Vector(n)
      }
    withChildren(newChildren)
  3. abstract def transformElems(f: (E) ⇒ E): E

    Transforms the element by applying the given function to all its descendant elements, in a bottom-up manner.

    Transforms the element by applying the given function to all its descendant elements, in a bottom-up manner.

    That is, returns the equivalent of:

    transformChildElems (e => e.transformElemsOrSelf(f))
  4. abstract def transformElemsOrSelf(f: (E) ⇒ E): E

    Transforms the element by applying the given function to all its descendant-or-self elements, in a bottom-up manner.

    Transforms the element by applying the given function to all its descendant-or-self elements, in a bottom-up manner.

    That is, returns the equivalent of:

    f(transformChildElems (e => e.transformElemsOrSelf(f)))

    In other words, returns the equivalent of:

    f(transformElems(f))
  5. abstract def transformElemsOrSelfToNodeSeq(f: (E) ⇒ IndexedSeq[N]): IndexedSeq[N]

    Transforms each descendant element to a node sequence by applying the given function to all its descendant-or-self elements, in a bottom-up manner.

    Transforms each descendant element to a node sequence by applying the given function to all its descendant-or-self elements, in a bottom-up manner.

    That is, returns the equivalent of:

    f(transformChildElemsToNodeSeq(e => e.transformElemsOrSelfToNodeSeq(f)))

    In other words, returns the equivalent of:

    f(transformElemsToNodeSeq(f))
  6. abstract def transformElemsToNodeSeq(f: (E) ⇒ IndexedSeq[N]): E

    Transforms each descendant element to a node sequence by applying the given function to all its descendant elements, in a bottom-up manner.

    Transforms each descendant element to a node sequence by applying the given function to all its descendant elements, in a bottom-up manner. The function is not applied to this element itself.

    That is, returns the equivalent of:

    transformChildElemsToNodeSeq(e => e.transformElemsOrSelfToNodeSeq(f))

    It is equivalent to the following expression:

    transformElemsOrSelf { e => e.transformChildElemsToNodeSeq(che => f(che)) }

Concrete Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  7. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  12. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  13. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  14. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  15. final def notify(): Unit

    Definition Classes
    AnyRef
  16. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  17. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  18. def toString(): String

    Definition Classes
    AnyRef → Any
  19. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped