Packages

  • package root
    Definition Classes
    root
  • package eu
    Definition Classes
    root
  • package cdevreeze
    Definition Classes
    eu
  • package yaidom

    Yaidom is yet another Scala immutable DOM-like XML API.

    Yaidom is yet another Scala immutable DOM-like XML API. The best known Scala immutable DOM-like API is the standard scala.xml API. It:

    • attempts to offer an XPath-like querying experience, thus somewhat blurring the distinction between nodes and node collections
    • lacks first-class support for XML namespaces
    • has limited (functional) update support

    Yaidom takes a different approach, avoiding XPath-like query support in its query API, and offering good namespace and decent (functional) update support. Yaidom is also characterized by almost mathematical precision and clarity. Still, the API remains practical and pragmatic. In particular, the API user has much configuration control over parsing and serialization, because yaidom exposes the underlying JAXP parsers and serializers, which can be configured by the library user.

    Yaidom chooses its battles. For example, given that DTDs do not know about namespaces, yaidom offers good namespace support, but ignores DTDs entirely. Of course the underlying XML parser may still validate XML against a DTD, if so desired. As another example, yaidom tries to leave the handling of the gory details of XML processing (such as whitespace handling) as much as possible to JAXP (and JAXP parser/serializer configuration). As yet another example, yaidom knows nothing about (XML Schema) types of elements and attributes.

    As mentioned above, yaidom tries to treat basic XML processing with almost mathematical precision, even if this is "incorrect". At the same time, yaidom tries to be useful in practice. For example, yaidom compromises "correctness" in the following ways:

    • Yaidom does not generally consider documents to be nodes (called "document information items" in the XML Infoset), thus introducing fewer constraints on DOM-like node construction
    • Yaidom does not consider attributes to be (non-child) nodes (called "attribute information items" in the XML Infoset), thus introducing fewer constraints on DOM-like node construction
    • Yaidom does not consider namespace declarations to be attributes, thus facilitating a clear theory of namespaces
    • Yaidom tries to keep the order of the attributes (for better round-tripping), although attribute order is irrelevant according to the XML Infoset
    • Very importantly, yaidom clearly distinguishes between qualified names (QNames) and expanded names (ENames), which is essential in facilitating a clear theory of namespaces

    Yaidom, and in particular the eu.cdevreeze.yaidom.core, eu.cdevreeze.yaidom.queryapi, eu.cdevreeze.yaidom.resolved and eu.cdevreeze.yaidom.simple sub-packages, contains the following layers:

    • basic concepts, such as (qualified and expanded) names of elements and attributes (in the core package)
    • the uniform query API traits, to query elements for child, descendant and descendant-or-self elements (in the queryapi package)
    • some of the specific element implementations, mixing in those uniform query API traits (e.g. in the resolved and simple packages)

    It makes sense to read this documentation, because it helps in getting up-to-speed with yaidom.

    Basic concepts

    In real world XML, elements (and sometimes attributes) tend to have names within a certain namespace. There are 2 kinds of names at play here:

    • qualified names: prefixed names, such as book:Title, and unprefixed names, such as Edition
    • expanded names: having a namespace, such as {http://bookstore/book}Title (in James Clark notation), and not having a namespace, such as Edition

    They are represented by immutable classes eu.cdevreeze.yaidom.core.QName and eu.cdevreeze.yaidom.core.EName, respectively.

    Qualified names occur in XML, whereas expanded names do not. Yet qualified names have no meaning on their own. They need to be resolved to expanded names, via the in-scope namespaces. Note that the term "qualified name" is often used for what yaidom (and the Namespaces specification) calls "expanded name", and that most XML APIs do not distinguish between the 2 kinds of names. Yaidom has to clearly make this distinction, in order to model namespaces correctly.

    To resolve qualified names to expanded names, yaidom distinguishes between:

    • namespace declarations
    • in-scope namespaces

    They are represented by immutable classes eu.cdevreeze.yaidom.core.Declarations and eu.cdevreeze.yaidom.core.Scope, respectively.

    Namespace declarations occur in XML, whereas in-scope namespaces do not. The latter are the accumulated effect of the namespace declarations of the element itself, if any, and those in ancestor elements.

    Note: in the code examples below, we assume the following import:

    import eu.cdevreeze.yaidom.core._

    To see the resolution of qualified names in action, consider the following sample 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>

    Consider the last element with qualified name QName("book:Book"). To resolve this qualified name as expanded name, we need to know the namespaces in scope at that element. To compute the in-scope namespaces, we need to accumulate the namespace declarations of the last book:Book element and of its ancestor element(s), starting with the root element.

    The start Scope is "parent scope" Scope.Empty. Then, in the root element we find namespace declarations:

    Declarations.from("book" -> "http://bookstore/book", "auth" -> "http://bookstore/author")

    This leads to the following namespaces in scope at the root element:

    Scope.Empty.resolve(Declarations.from("book" -> "http://bookstore/book", "auth" -> "http://bookstore/author"))

    which is equal to:

    Scope.from("book" -> "http://bookstore/book", "auth" -> "http://bookstore/author")

    We find no other namespace declarations in the last book:Book element or its ancestor(s), so the computed scope is also the scope of the last book:Book element.

    Then QName("book:Book") is resolved as follows:

    Scope.from("book" -> "http://bookstore/book", "auth" -> "http://bookstore/author").resolveQNameOption(QName("book:Book"))

    which is equal to:

    Some(EName("{http://bookstore/book}Book"))

    This namespace support in yaidom has mathematical rigor. The immutable classes QName, EName, Declarations and Scope have precise definitions, reflected in their implementations, and they obey some interesting properties. For example, if we correctly define Scope operation relativize (along with resolve), we get:

    scope1.resolve(scope1.relativize(scope2)) == scope2

    This may not sound like much, but by getting the basics right, yaidom succeeds in offering first-class support for XML namespaces, without the magic and namespace-related bugs often found in other XML libraries.

    There are 2 other basic concepts in this package, representing paths to elements:

    • path builders
    • paths

    They are represented by immutable classes eu.cdevreeze.yaidom.core.PathBuilder and eu.cdevreeze.yaidom.core.Path, respectively.

    Path builders are like canonical XPath expressions, yet they do not contain the root element itself, and indexing starts with 0 instead of 1.

    For example, the last name of the first author of the last book element has path:

    Path.from(
      EName("{http://bookstore/book}Book") -> 1,
      EName("{http://bookstore/book}Authors") -> 0,
      EName("{http://bookstore/author}Author") -> 0,
      EName("{http://bookstore/author}Last_Name") -> 0
    )

    This path could be written as path builder as follows:

    PathBuilder.from(QName("book:Book") -> 1, QName("book:Authors") -> 0, QName("auth:Author") -> 0, QName("auth:Last_Name") -> 0)

    Using the Scope mentioned earlier, the latter path builder resolves to the path given before that, by invoking method PathBuilder.build(scope). In order for this to work, the Scope must be invertible. That is, there must be a one-to-one correspondence between prefixes ("" for the default namespace) and namespace URIs, because otherwise the index numbers may differ. Also note that the prefixes book and auth in the path builder are arbitrary, and need not match with the prefixes used in the XML tree itself.

    Uniform query API traits

    Yaidom provides a relatively small query API, to query an individual element for collections of child elements, descendant elements or descendant-or-self elements. The resulting collections are immutable Scala collections, that can further be manipulated using the Scala Collections API.

    This query API is uniform, in that different element implementations share (most of) the same query API. It is also element-centric (unlike standard Scala XML).

    For example, consider the XML example given earlier, as a Scala XML literal named bookstore. We can wrap this Scala XML Elem into a yaidom wrapper of type ScalaXmlElem, named bookstoreElem. Then we can query for all books, that is, all descendant-or-self elements with resolved (or expanded) name EName("{http://bookstore/book}Book"), as follows:

    bookstoreElem filterElemsOrSelf (elem => elem.resolvedName == EName("{http://bookstore/book}Book"))

    The result would be an immutable IndexedSeq of ScalaXmlElem instances, holding 2 book elements.

    We could instead have written:

    bookstoreElem.filterElemsOrSelf(EName("{http://bookstore/book}Book"))

    with the same result, due to an implicit conversion from expanded names to element predicates.

    Instead of searching for appropriate descendant-or-self elements, we could have searched for descendant elements only, without altering the result in this case:

    bookstoreElem filterElems (elem => elem.resolvedName == EName("{http://bookstore/book}Book"))

    or:

    bookstoreElem.filterElems(EName("{http://bookstore/book}Book"))

    We could even have searched for appropriate child elements only, without altering the result in this case:

    bookstoreElem filterChildElems (elem => elem.resolvedName == EName("{http://bookstore/book}Book"))

    or:

    bookstoreElem.filterChildElems(EName("{http://bookstore/book}Book"))

    or, knowing that all child elements are books:

    bookstoreElem.findAllChildElems

    We could find all authors of the Scala book as follows:

    for {
      bookElem <- bookstoreElem filterChildElems (elem => elem.resolvedName == EName("{http://bookstore/book}Book"))
      if bookElem.attributeOption(EName("ISBN")).contains("978-0981531649")
      authorElem <- bookElem filterElems (elem => elem.resolvedName == EName("{http://bookstore/author}Author"))
    } yield authorElem

    or:

    for {
      bookElem <- bookstoreElem.filterChildElems(EName("{http://bookstore/book}Book"))
      if bookElem.attributeOption(EName("ISBN")).contains("978-0981531649")
      authorElem <- bookElem.filterElems(EName("{http://bookstore/author}Author"))
    } yield authorElem

    We could even use operator notation, as follows:

    for {
      bookElem <- bookstoreElem \ (elem => elem.resolvedName == EName("{http://bookstore/book}Book"))
      if (bookElem \@ EName("ISBN")).contains("978-0981531649")
      authorElem <- bookElem \\ (elem => elem.resolvedName == EName("{http://bookstore/author}Author"))
    } yield authorElem

    or:

    for {
      bookElem <- bookstoreElem \ EName("{http://bookstore/book}Book")
      if (bookElem \@ EName("ISBN")).contains("978-0981531649")
      authorElem <- bookElem \\ EName("{http://bookstore/author}Author")
    } yield authorElem

    where \\ stands for filterElemsOrSelf.

    There is no explicit support for filtering on the "self" element itself. In the example above, we might want to check if the root element has the expected EName, for instance. That is easy to express using a simple idiom, however. The last example then becomes:

    for {
      bookstoreElem <- Vector(bookstoreElem)
      if bookstoreElem.resolvedName == EName("{http://bookstore/book}Bookstore")
      bookElem <- bookstoreElem \ EName("{http://bookstore/book}Book")
      if (bookElem \@ EName("ISBN")).contains("978-0981531649")
      authorElem <- bookElem \\ EName("{http://bookstore/author}Author")
    } yield authorElem

    Now suppose the same XML is stored in a (org.w3c.dom) DOM tree, wrapped in a DomElem bookstoreElem. Then the same queries would use exactly the same code as above! The result would be a collection of DomElem instances instead of ScalaXmlElem instances, however. There are many more element implementations in yaidom, and they share (most of) the same query API. Therefore this query API is called a uniform query API.

    The last example, using operator notation, looks a bit more "XPath-like". It is more verbose than queries in Scala XML, however, partly because in yaidom these operators cannot be chained. Yet this is with good reason. Yaidom does not blur the distinction between elements and element collections, and therefore does not offer any XPath experience. The small price paid in verbosity is made up for by precision. The yaidom query API traits have very precise definitions of their operations, as can be seen in the corresponding documentation.

    The uniform query API traits turn minimal APIs into richer APIs, where each richer API is defined very precisely in terms of the minimal API. The most important (partly concrete) query API trait is eu.cdevreeze.yaidom.queryapi.ElemLike. It needs to be given a method implementation to query for child elements (not child nodes in general, but just child elements!), and it offers methods to query for some or all child elements, descendant elements, and descendant-or-self elements. That is, the minimal API consists of abstract method findAllChildElems, and it offers methods such as filterChildElems, filterElems and filterElemsOrSelf. This trait has no knowledge about elements at all, other than the fact that elements can have child elements.

    Trait eu.cdevreeze.yaidom.queryapi.HasEName needs minimal knowledge about elements themselves, viz. that elements have a "resolved" (or expanded) name, and "resolved" attributes (mapping attribute expanded names to attribute values). That is, it needs to be given implementations of abstract methods resolvedName and resolvedAttributes, and then offers methods to query for individual attributes or the local name of the element.

    It is important to note that yaidom does not consider namespace declarations to be attributes themselves. Otherwise, there would have been circular dependencies between both concepts, because attributes with namespaces require in-scope namespaces and therefore namespace declarations for resolving the names of these attributes.

    Many traits, such as eu.cdevreeze.yaidom.queryapi.HasEName, are just "capabilities", and need to be combined with trait eu.cdevreeze.yaidom.queryapi.ElemLike in order to offer a useful element querying API.

    Note that trait eu.cdevreeze.yaidom.queryapi.ElemLike only knows about elements, not about other kinds of nodes. Of course the actual element implementations mixing in this query API know about other node types, but that knowledge is outside the uniform query API. Note that the example queries above only use the minimal element knowledge that traits ElemLike and HasEName together have about elements. Therefore the query code can be used unchanged for different element implementations.

    Trait eu.cdevreeze.yaidom.queryapi.IsNavigable is used to navigate to an element given a Path.

    Trait eu.cdevreeze.yaidom.queryapi.UpdatableElemLike (which extends trait IsNavigable) offers functional updates at given paths. Whereas the traits mentioned above know only about elements, this trait knows that elements have some node super-type.

    Instead of functional updates at given paths, elements can also be "transformed" functionally without specifying any paths. This is offered by trait eu.cdevreeze.yaidom.queryapi.TransformableElemLike. The Scala XML and DOM wrappers above do not mix in this trait.

    Three uniform query API levels

    Above, several individual query API traits were mentioned. There are, however, 3 query API levels which are interesting for those who extend yaidom with new element implementations, but also for most users of the yaidom query API. These levels are represented by "combination traits" that combine several of the query API traits mentioned (or not mentioned) above.

    The most basic level is eu.cdevreeze.yaidom.queryapi.ClarkNodes.Elem. It combines traits such as eu.cdevreeze.yaidom.queryapi.ElemApi and eu.cdevreeze.yaidom.queryapi.HasENameApi. Object eu.cdevreeze.yaidom.queryapi.ClarkNodes also contains types for non-element nodes. All element implementations that extend trait ClarkNodes.Elem should have a node hierarchy with all its kinds of nodes extending the appropriate ClarkNodes member type.

    All element implementation directly or indirectly implement the ClarkNodes.Elem trait. The part of the yaidom query API that knows about ElemApi querying and about ENames is the ClarkNodes query API level. It does not know about QNames, in-scope namespaces, ancestor elements, base URIs, etc.

    The next level is eu.cdevreeze.yaidom.queryapi.ScopedNodes.Elem. It extends the ClarkNodes.Elem trait, but offers knowledge about QNames and in-scope namespaces as well. Many element implementations offer at least this query API level. The remarks about non-element nodes above also apply here, and apply below.

    The third level is eu.cdevreeze.yaidom.queryapi.BackingNodes.Elem. It extends the ScopedNodes.Elem trait, but offers knowledge about ancestor elements and document/base URIs as well. This is the level typically used for "backing elements" in "yaidom dialects", thus allowing for multiple "XML backends" to be used behind "yaidom dialects". Yaidom dialects are specific "XML dialect" type-safe yaidom query APIs, mixing in and leveraging trait eu.cdevreeze.yaidom.queryapi.SubtypeAwareElemApi (often in combination with eu.cdevreeze.yaidom.queryapi.ScopedNodes.Elem).

    To get to know the yaidom query API and its 3 levels, it pays off to study the API documentation of traits eu.cdevreeze.yaidom.queryapi.ClarkNodes.Elem, eu.cdevreeze.yaidom.queryapi.ScopedNodes.Elem and eu.cdevreeze.yaidom.queryapi.BackingNodes.Elem.

    Some element implementations

    In package simple there are 2 immutable element implementations, eu.cdevreeze.yaidom.simple.ElemBuilder and eu.cdevreeze.yaidom.simple.Elem. Arguably, ElemBuilder is not an element implementation. Indeed, it does not even offer the ClarkNodes.Elem query API.

    Class eu.cdevreeze.yaidom.simple.Elem is the default element implementation of yaidom. It extends class eu.cdevreeze.yaidom.simple.Node. The latter also has sub-classes for text nodes, comments, entity references and processing instructions. Class eu.cdevreeze.yaidom.simple.Document contains a document Elem, but is not a Node sub-class itself. This node hierarchy offers the ScopedNodes query API, so simple elements offer the ScopedNodes.Elem query API.

    The eu.cdevreeze.yaidom.simple.Elem class has the following characteristics:

    • It is immutable, and thread-safe
    • These elements therefore cannot be queried for their parent elements
    • It mixes in query API trait eu.cdevreeze.yaidom.queryapi.ScopedNodes.Elem, eu.cdevreeze.yaidom.queryapi.UpdatableElemApi and eu.cdevreeze.yaidom.queryapi.TransformableElemApi
    • Besides the element name, attributes and child nodes, it keeps a Scope, but no Declarations
    • This makes it easy to compose these elements, as long as scopes are passed explicitly throughout the element tree
    • Equality is reference equality, because it is hard to come up with a sensible equality for this element class
    • Roundtripping cannot be entirely lossless, but this class does try to retain the attribute order (although irrelevant according to XML Infoset)
    • Packages parse and print offer DocumentParser and DocumentPrinter classes for parsing/serializing these default Elem (and Document) instances

    Creating such Elem trees by hand is a bit cumbersome, partly because scopes have to be passed to each Elem in the tree. The latter is not needed if we use class eu.cdevreeze.yaidom.simple.ElemBuilder to create element trees by hand. When the tree has been fully created as ElemBuilder, invoke method ElemBuilder.build(parentScope) to turn it into an Elem.

    Like their super-classes Node and NodeBuilder, classes Elem and ElemBuilder have very much in common. Both are immutable, easy to compose (ElemBuilder instances even more so), equality is reference equality, etc. The most important differences are as follows:

    • Instead of a Scope, an ElemBuilder contains a Declarations
    • This makes an ElemBuilder easier to compose than an Elem, because no Scope needs to be passed around throughout the tree
    • Class ElemBuilder uses a minimal query API, mixing in almost only traits ElemLike and TransformableElemLike
    • After all, an ElemBuilder neither keeps nor knows about Scopes, so does not know about resolved element/attribute names

    The Effective Java book element in the XML example above could have been written as ElemBuilder (without the inter-element whitespace) as follows:

    import NodeBuilder._
    
    elem(
      qname = QName("book:Book"),
      attributes = Vector(QName("ISBN") -> "978-0321356680", QName("Price") -> "35", QName("Edition") -> "2"),
      children = Vector(
        elem(
          qname = QName("book:Title"),
          children = Vector(
            text("Effective Java (2nd Edition)")
          )
        ),
        elem(
          qname = QName("book:Authors"),
          children = Vector(
            elem(
              qname = QName("auth:Author"),
              children = Vector(
                elem(
                  qname = QName("auth:First_Name"),
                  children = Vector(
                    text("Joshua")
                  )
                ),
                elem(
                  qname = QName("auth:Last_Name"),
                  children = Vector(
                    text("Bloch")
                  )
                )
              )
            )
          )
        )
      )
    )

    This ElemBuilder (say, eb) lacks namespace declarations for prefixes book and auth. So, the following returns false:

    eb.canBuild(Scope.Empty)

    while the following returns true:

    eb.canBuild(Scope.from("book" -> "http://bookstore/book", "auth" -> "http://bookstore/author"))

    Indeed,

    eb.build(Scope.from("book" -> "http://bookstore/book", "auth" -> "http://bookstore/author"))

    returns the element tree as Elem.

    Note that the distinction between ElemBuilder and Elem "solves" the mismatch that immutable ("functional") element trees are constructed in a bottom-up manner, while namespace scoping works in a top-down manner. (See also Anti-XML issue 78, in https://github.com/djspiewak/anti-xml/issues/78).

    There are many more element implementations in yaidom, most of them in sub-packages of this package. Yaidom is extensible in that new element implementations can be invented, for example elements that are better "roundtrippable" (at the expense of "composability"), or yaidom wrappers around other DOM-like APIs (such as XOM or JDOM2). The current element implementations in yaidom are for example:

    • Immutable class eu.cdevreeze.yaidom.simple.Elem, the default (immutable) element implementation. See above.
    • Immutable class eu.cdevreeze.yaidom.simple.ElemBuilder for creating an Elem by hand. See above.
    • Immutable class eu.cdevreeze.yaidom.resolved.Elem, which takes namespace prefixes out of the equation, and therefore makes useful (namespace-aware) equality comparisons feasible. It offers the ClarkNodes.Elem query API (as well as update/transformation support).
    • Immutable class eu.cdevreeze.yaidom.indexed.Elem, which offers views on default Elems that know the ancestry of each element. It offers the BackingNodes.Elem query API, so knows its ancestry, despite being immutable! This element implementation is handy for querying XML schemas, for example, because in schemas the ancestry of queried elements typically matters.

    One yaidom wrapper that is very useful is a Saxon tiny tree yaidom wrapper, namely SaxonElem (JVM-only). Like "indexed elements", it offers all of the BackingNodes.Elem query API. This element implementation is very efficient, especially in memory footprint (when using the default tree model, namely tiny trees). It is therefore the most attractive element implementation to use in "enterprise" production code, but only on the JVM. In combination with Saxon-EE (instead of Saxon-HE) the underlying Saxon NodeInfo objects can even carry interesting type information.

    For ad-hoc element creation, consider using "resolved" elements. They are easy to create, because there is no need to worry about namespace prefixes. Once created, they can be converted to "simple" elements, given an appropriate Scope (without default namespace).

    Packages and dependencies

    Yaidom has the following packages, and layering between packages (mentioning the lowest layers first):

    • Package eu.cdevreeze.yaidom.core, with the core concepts described above. It depends on no other yaidom packages.
    • Package eu.cdevreeze.yaidom.queryapi, with the query API traits described above. It only depends on the core package.
    • Package eu.cdevreeze.yaidom.resolved, with a minimal "James Clark" element implementation. It only depends on the core and queryapi packages.
    • Package eu.cdevreeze.yaidom.simple, with the default element implementation described above. It only depends on the core and queryapi packages.
    • Package eu.cdevreeze.yaidom.indexed, supporting "indexed" elements. It only depends on the core, queryapi and simple packages.
    • Package convert. It contains conversions between default yaidom nodes on the one hand and DOM, Scala XML, etc. on the other hand. The convert package depends on the yaidom core, queryapi, resolved and simple packages.
    • Package eu.cdevreeze.yaidom.saxon, with the Saxon wrapper element implementation described above. It only depends on the core, queryapi and convert packages.
    • Packages eu.cdevreeze.yaidom.parse and eu.cdevreeze.yaidom.print, for parsing/printing Elems. They depend on the packages mentioned above, except for indexed and saxon.
    • The other packages (except utils), such as dom and scalaxml. They depend on (some of) the packages mentioned above, but not on each other.
    • Package eu.cdevreeze.yaidom.utils, which depends on all the packages above.

    Indeed, all yaidom package dependencies are uni-directional.

    Notes on performance

    Yaidom can be quite memory-hungry. One particular cause of that is the possible creation of very many duplicate EName and QName instances. This can be the case while parsing XML into yaidom documents, or while querying yaidom element trees.

    The user of the library can reduce memory consumption to a large extent, and yaidom facilitates that.

    As for querying, prefer:

    import HasENameApi._
    
    bookstoreElem filterElemsOrSelf withEName("http://bookstore/book", "Book")

    to:

    bookstoreElem.filterElemsOrSelf(EName("http://bookstore/book", "Book"))

    to avoid unnecessary (large scale) EName object creation.

    To reduce the memory footprint of parsed XML trees, see eu.cdevreeze.yaidom.core.ENameProvider and eu.cdevreeze.yaidom.core.QNameProvider.

    For example, during the startup phase of an application, we could set the global ENameProvider as follows:

    ENameProvider.globalENameProvider.become(new ENameProvider.ENameProviderUsingImmutableCache(knownENames))

    Note that the global ENameProvider or QNameProvider can typically be configured rather late during development, but the memory cost savings can be substantial once configured. Also note that the global ENameProvider or QNameProvider can be used implicitly in application code, by writing:

    bookstoreElem filterElemsOrSelf getEName("http://bookstore/book", "Book")

    using an implicit ENameProvider, whose members are in scope. Still, for querying the first alternative using withEName is better, but there are likely many scenarios in yaidom client code where an implicit ENameProvider or QNameProvider makes sense.

    The bottom line is that yaidom can be configured to be far less memory-hungry, and that yaidom client code can also take some responsibility in reducing memory usage. Again, the Saxon wrapper implementation is an excellent and efficient choice (but only on the JVM).

    Definition Classes
    cdevreeze
  • package scalaxml

    Wrapper around class scala.xml.Elem, adapting it to the eu.cdevreeze.yaidom.queryapi.ElemLike API.

    Wrapper around class scala.xml.Elem, adapting it to the eu.cdevreeze.yaidom.queryapi.ElemLike API.

    This wrapper brings the uniform yaidom query API to Scala XML literals (and Scala XML Elems in general).

    For some namespace-related pitfalls and peculiarities, see eu.cdevreeze.yaidom.scalaxml.ScalaXmlElem.

    Definition Classes
    yaidom
  • CanBeScalaXmlDocumentChild
  • ScalaXmlAtom
  • ScalaXmlCData
  • ScalaXmlComment
  • ScalaXmlDocument
  • ScalaXmlElem
  • ScalaXmlEntityRef
  • ScalaXmlNode
  • ScalaXmlProcessingInstruction
  • ScalaXmlText

final class ScalaXmlElem extends CanBeScalaXmlDocumentChild with Elem with ScopedElemLike

Wrapper around scala.xml.Elem, conforming to the eu.cdevreeze.yaidom.queryapi.ElemApi API.

See the documentation of the mixed-in query API trait(s) for more details on the uniform query API offered by this class.

Keep in mind that the HasENameApi specific part of the API is a broken abstraction. If the wrapped Scala XML element misses some namespace declarations for used element or attribute names, these element and/or attribute names cannot be resolved, and exceptions are thrown when querying for them! The ElemApi part of the API does not suffer from this broken abstraction, so is less dangerous to use.

The wrapper instances are very light-weight, and typically very short-lived. On the other hand, each query may create many wrapper instances for the query results. By design, the only state of each wrapper instance is the wrapped Scala XML Elem.

Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. ScalaXmlElem
  2. ScopedElemLike
  3. ClarkElemLike
  4. HasText
  5. HasEName
  6. IsNavigable
  7. ElemLike
  8. Elem
  9. ScopedElemApi
  10. HasScopeApi
  11. HasQNameApi
  12. Elem
  13. HasChildNodesApi
  14. AnyElemNodeApi
  15. ClarkElemApi
  16. HasTextApi
  17. HasENameApi
  18. IsNavigableApi
  19. ElemApi
  20. AnyElemApi
  21. Elem
  22. CanBeScalaXmlDocumentChild
  23. CanBeDocumentChild
  24. CanBeDocumentChild
  25. CanBeDocumentChild
  26. ScalaXmlNode
  27. Node
  28. Node
  29. Node
  30. AnyRef
  31. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new ScalaXmlElem(wrappedNode: Elem)

Type Members

  1. type DomType = Elem
    Definition Classes
    ScalaXmlElemScalaXmlNode
  2. type ThisElem = ScalaXmlElem

    The element type itself.

    The element type itself. It must be restricted to a sub-type of the query API trait in question.

    Concrete element classes will restrict this type to that element class itself.

    Definition Classes
    ScalaXmlElemScopedElemLikeClarkElemLikeIsNavigableElemLikeElemScopedElemApiElemHasChildNodesApiClarkElemApiIsNavigableApiElemApiAnyElemApi
  3. type ThisNode = ScalaXmlNode

    The node type, that is a super-type of the element type, but also of corresponding text node types etc.

    The node type, that is a super-type of the element type, but also of corresponding text node types etc.

    Definition Classes
    ScalaXmlElemElemElemAnyElemNodeApi

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def \(p: (ThisElem) ⇒ Boolean): IndexedSeq[ThisElem]

    Shorthand for filterChildElems(p).

    Shorthand for filterChildElems(p). Use this shorthand only if the predicate is a short expression.

    Definition Classes
    ElemLikeElemApi
  5. final def \@(expandedName: EName): Option[String]

    Shorthand for attributeOption(expandedName).

    Shorthand for attributeOption(expandedName).

    Definition Classes
    HasENameHasENameApi
  6. final def \\(p: (ThisElem) ⇒ Boolean): IndexedSeq[ThisElem]

    Shorthand for filterElemsOrSelf(p).

    Shorthand for filterElemsOrSelf(p). Use this shorthand only if the predicate is a short expression.

    Definition Classes
    ElemLikeElemApi
  7. final def \\!(p: (ThisElem) ⇒ Boolean): IndexedSeq[ThisElem]

    Shorthand for findTopmostElemsOrSelf(p).

    Shorthand for findTopmostElemsOrSelf(p). Use this shorthand only if the predicate is a short expression.

    Definition Classes
    ElemLikeElemApi
  8. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  9. final def attribute(expandedName: EName): String

    Returns the value of the attribute with the given expanded name, and throws an exception otherwise.

    Returns the value of the attribute with the given expanded name, and throws an exception otherwise.

    Definition Classes
    HasENameHasENameApi
  10. final def attributeAsQName(expandedName: EName): QName

    Returns the QName value of the attribute with the given expanded name, and throws an exception otherwise

    Returns the QName value of the attribute with the given expanded name, and throws an exception otherwise

    Definition Classes
    ScopedElemLikeScopedElemApi
  11. final def attributeAsQNameOption(expandedName: EName): Option[QName]

    Returns the QName value of the attribute with the given expanded name, if any, wrapped in an Option.

    Returns the QName value of the attribute with the given expanded name, if any, wrapped in an Option. If the attribute exists, but its value is not a QName, an exception is thrown.

    Definition Classes
    ScopedElemLikeScopedElemApi
  12. final def attributeAsResolvedQName(expandedName: EName): EName

    Returns the resolved QName value (as EName) of the attribute with the given expanded name, and throws an exception otherwise

    Returns the resolved QName value (as EName) of the attribute with the given expanded name, and throws an exception otherwise

    Definition Classes
    ScopedElemLikeScopedElemApi
  13. final def attributeAsResolvedQNameOption(expandedName: EName): Option[EName]

    Returns the resolved QName value (as EName) of the attribute with the given expanded name, if any, wrapped in an Option.

    Returns the resolved QName value (as EName) of the attribute with the given expanded name, if any, wrapped in an Option. None is returned if the attribute does not exist. If the QName value cannot be resolved given the scope of the element, an exception is thrown.

    Definition Classes
    ScopedElemLikeScopedElemApi
  14. final def attributeOption(expandedName: EName): Option[String]

    Returns the value of the attribute with the given expanded name, if any, wrapped in an Option.

    Returns the value of the attribute with the given expanded name, if any, wrapped in an Option.

    Definition Classes
    HasENameHasENameApi
  15. def attributes: IndexedSeq[(QName, String)]

    The attributes of the element as mapping from QNames to values

    The attributes of the element as mapping from QNames to values

    Definition Classes
    ScalaXmlElemHasQNameApi
  16. def children: IndexedSeq[ScalaXmlNode]

    Returns all child nodes, of any kind of node (element node, text node etc.).

    Returns all child nodes, of any kind of node (element node, text node etc.).

    Definition Classes
    ScalaXmlElemHasChildNodesApi
  17. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @throws(classOf[java.lang.CloneNotSupportedException])
  18. def commentChildren: IndexedSeq[ScalaXmlComment]

    Returns the comment children

  19. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  20. final def equals(obj: Any): Boolean
    Definition Classes
    ScalaXmlNode → AnyRef → Any
  21. final def filterChildElems(p: (ThisElem) ⇒ Boolean): IndexedSeq[ThisElem]

    Returns the child elements obeying the given predicate.

    Returns the child elements obeying the given predicate. This method could be defined as:

    def filterChildElems(p: ThisElem => Boolean): immutable.IndexedSeq[ThisElem] =
      this.findAllChildElems.filter(p)
    Definition Classes
    ElemLikeElemApi
  22. final def filterElems(p: (ThisElem) ⇒ Boolean): IndexedSeq[ThisElem]

    Returns the descendant elements obeying the given predicate, in document order.

    Returns the descendant elements obeying the given predicate, in document order. This method could be defined as:

    this.findAllChildElems flatMap (_.filterElemsOrSelf(p))
    Definition Classes
    ElemLikeElemApi
  23. final def filterElemsOrSelf(p: (ThisElem) ⇒ Boolean): IndexedSeq[ThisElem]

    Returns the descendant-or-self elements obeying the given predicate, in document order.

    Returns the descendant-or-self elements obeying the given predicate, in document order. This method could be defined as:

    def filterElemsOrSelf(p: ThisElem => Boolean): immutable.IndexedSeq[ThisElem] =
      Vector(this).filter(p) ++ (this.findAllChildElems flatMap (_.filterElemsOrSelf(p)))

    It can be proven that the result is equivalent to findAllElemsOrSelf filter p.

    Definition Classes
    ElemLikeElemApi
  24. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  25. def findAllChildElems: IndexedSeq[ScalaXmlElem]

    Core method that returns all child elements, in the correct order.

    Core method that returns all child elements, in the correct order. Other operations can be defined in terms of this one.

    Definition Classes
    ScalaXmlElemElemLikeElemApi
  26. final def findAllChildElemsWithPathEntries: IndexedSeq[(ThisElem, Entry)]

    Returns all child elements paired with their path entries.

    Returns all child elements paired with their path entries.

    This method is final, so more efficient implementations for sub-types are not supported. This implementation is only efficient if finding all child elements as well as computing their resolved names is efficient. That is not the case for DOM wrappers or Scala XML Elem wrappers (due to their expensive Scope computations). On the other hand, those wrapper element implementations are convenient, but not intended for heavy use in production. Hence, this method should typically be fast enough.

    Definition Classes
    ClarkElemLikeIsNavigableIsNavigableApi
  27. final def findAllElems: IndexedSeq[ThisElem]

    Returns all descendant elements (not including this element), in document order.

    Returns all descendant elements (not including this element), in document order. This method could be defined as filterElems { e => true }. Equivalent to findAllElemsOrSelf.drop(1).

    Definition Classes
    ElemLikeElemApi
  28. final def findAllElemsOrSelf: IndexedSeq[ThisElem]

    Returns this element followed by all descendant elements (that is, the descendant-or-self elements), in document order.

    Returns this element followed by all descendant elements (that is, the descendant-or-self elements), in document order. This method could be defined as filterElemsOrSelf { e => true }.

    Definition Classes
    ElemLikeElemApi
  29. final def findAttributeByLocalName(localName: String): Option[String]

    Returns the first found attribute value of an attribute with the given local name, if any, wrapped in an Option.

    Returns the first found attribute value of an attribute with the given local name, if any, wrapped in an Option. Because of differing namespaces, it is possible that more than one such attribute exists, although this is not often the case.

    Definition Classes
    HasENameHasENameApi
  30. final def findChildElem(p: (ThisElem) ⇒ Boolean): Option[ThisElem]

    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.

    Definition Classes
    ElemLikeElemApi
  31. final def findChildElemByPathEntry(entry: Entry): Option[ThisElem]

    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.

    This method is final, so more efficient implementations for sub-types are not supported. This implementation is only efficient if finding all child elements as well as computing their resolved names is efficient. That is not the case for DOM wrappers or Scala XML Elem wrappers (due to their expensive Scope computations). On the other hand, those wrapper element implementations are convenient, but not intended for heavy use in production. Hence, this method should typically be fast enough.

    Definition Classes
    ClarkElemLikeIsNavigableIsNavigableApi
  32. final def findElem(p: (ThisElem) ⇒ Boolean): Option[ThisElem]

    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.

    Definition Classes
    ElemLikeElemApi
  33. final def findElemOrSelf(p: (ThisElem) ⇒ Boolean): Option[ThisElem]

    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.

    Definition Classes
    ElemLikeElemApi
  34. final def findElemOrSelfByPath(path: Path): Option[ThisElem]

    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(_.findElemOrSelfByPath(path.withoutFirstEntry))
    Definition Classes
    IsNavigableIsNavigableApi
  35. final def findReverseAncestryOrSelfByPath(path: Path): Option[IndexedSeq[ThisElem]]

    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.

    Definition Classes
    IsNavigableIsNavigableApi
  36. final def findTopmostElems(p: (ThisElem) ⇒ Boolean): IndexedSeq[ThisElem]

    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))
    Definition Classes
    ElemLikeElemApi
  37. final def findTopmostElemsOrSelf(p: (ThisElem) ⇒ Boolean): IndexedSeq[ThisElem]

    Returns the descendant-or-self elements obeying the given predicate, such that no ancestor obeys the predicate.

    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: ThisElem => Boolean): immutable.IndexedSeq[ThisElem] =
      if (p(this)) Vector(this)
      else (this.findAllChildElems flatMap (_.findTopmostElemsOrSelf(p)))
    Definition Classes
    ElemLikeElemApi
  38. final def getChildElem(p: (ThisElem) ⇒ Boolean): ThisElem

    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.

    Definition Classes
    ElemLikeElemApi
  39. final def getChildElemByPathEntry(entry: Entry): ThisElem

    Returns (the equivalent of) findChildElemByPathEntry(entry).get

    Returns (the equivalent of) findChildElemByPathEntry(entry).get

    Definition Classes
    IsNavigableIsNavigableApi
  40. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  41. final def getElemOrSelfByPath(path: Path): ThisElem

    Returns (the equivalent of) findElemOrSelfByPath(path).get

    Returns (the equivalent of) findElemOrSelfByPath(path).get

    Definition Classes
    IsNavigableIsNavigableApi
  42. final def getReverseAncestryOrSelfByPath(path: Path): IndexedSeq[ThisElem]

    Returns (the equivalent of) findReverseAncestryOrSelfByPath(path).get

    Returns (the equivalent of) findReverseAncestryOrSelfByPath(path).get

    Definition Classes
    IsNavigableIsNavigableApi
  43. final def hashCode(): Int
    Definition Classes
    ScalaXmlNode → AnyRef → Any
  44. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  45. final def localName: String

    The local name, that is, the local part of the EName

    The local name, that is, the local part of the EName

    Definition Classes
    HasENameHasENameApi
  46. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  47. final def nodeKind: NodeKind
    Definition Classes
    ElemNode
  48. final def normalizedText: String

    Returns XmlStringUtils.normalizeString(text).

    Returns XmlStringUtils.normalizeString(text).

    Definition Classes
    HasTextHasTextApi
  49. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  50. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  51. def qname: QName

    The QName of the element

    The QName of the element

    Definition Classes
    ScalaXmlElemHasQNameApi
  52. def resolvedAttributes: IndexedSeq[(EName, String)]

    Returns the "resolved attributes".

    Returns the "resolved attributes". Note that there is no guarantee that the attributes names can be resolved!

    Definition Classes
    ScalaXmlElemHasENameApi
  53. def resolvedName: EName

    Returns the resolved name of the element.

    Returns the resolved name of the element. Note that there is no guarantee that the element name can be resolved!

    Definition Classes
    ScalaXmlElemHasENameApi
  54. def scope: Scope

    Returns the scope of the element.

    Returns the scope of the element. Note that there is no guarantee that this scope is complete!

    Definition Classes
    ScalaXmlElemHasScopeApi
  55. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  56. def text: String

    Returns the concatenation of the texts of text children, including whitespace and CData.

    Returns the concatenation of the texts of text children, including whitespace and CData. Non-text children are ignored. If there are no text children, the empty string is returned.

    Definition Classes
    ScalaXmlElemHasTextApi
  57. final def textAsQName: QName

    Returns QName(text.trim)

    Returns QName(text.trim)

    Definition Classes
    ScopedElemLikeScopedElemApi
  58. final def textAsResolvedQName: EName

    Returns the equivalent of scope.resolveQNameOption(textAsQName).get

    Returns the equivalent of scope.resolveQNameOption(textAsQName).get

    Definition Classes
    ScopedElemLikeScopedElemApi
  59. def textChildren: IndexedSeq[ScalaXmlText]

    Returns the text children

  60. def thisElem: ThisElem

    This element itself.

    This element itself.

    Definition Classes
    ScalaXmlElemAnyElemApi
  61. final def toString(): String
    Definition Classes
    ScalaXmlNode → AnyRef → Any
  62. final def trimmedText: String

    Returns text.trim.

    Returns text.trim.

    Definition Classes
    HasTextHasTextApi
  63. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  64. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  65. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws(classOf[java.lang.InterruptedException])
  66. val wrappedNode: Elem
    Definition Classes
    ScalaXmlElemScalaXmlNode

Inherited from ScopedElemLike

Inherited from ClarkElemLike

Inherited from HasText

Inherited from HasEName

Inherited from IsNavigable

Inherited from ElemLike

Inherited from Elem

Inherited from ScopedElemApi

Inherited from HasScopeApi

Inherited from HasQNameApi

Inherited from Elem

Inherited from HasChildNodesApi

Inherited from AnyElemNodeApi

Inherited from ClarkElemApi

Inherited from HasTextApi

Inherited from HasENameApi

Inherited from IsNavigableApi

Inherited from ElemApi

Inherited from AnyElemApi

Inherited from Elem

Inherited from CanBeDocumentChild

Inherited from CanBeDocumentChild

Inherited from CanBeDocumentChild

Inherited from ScalaXmlNode

Inherited from Node

Inherited from Node

Inherited from Node

Inherited from AnyRef

Inherited from Any

Ungrouped