Node
public protocol Node: Visitable
The Node
protocol is the primary data type for the entire Document Object
Model. It represents a single node in the document tree. While all objects
implementing the Node
protocol expose functionality related to children, not
all objects implementing the Node
protocol may have children. To address this
distinction, there are two additional protocols implemented by node types:
ParentNode
provides a getter and setter on thechildren
propertyLeafNode
provides a getter on thechildren
property that always returns an empty array.
This is a departure from the standard DOM which would throw an error when
attempting to modify the children
array of a leaf node.
The attributes nodeName
, nodeValue
, and attributes
are included as a
mechanism to get at node information without casting down to the specific
derived type. In cases where there is no obvious mapping of these attributes
for a specific nodeType
(e.g., nodeValue
for an Element or attributes
for
a Comment), this returns nil
.
The values of nodeName
, nodeValue
, and attributes
vary according to the
node type.
-
nodeType
Default implementationIndicates which type of node this is.
Default Implementation
Convenience accessor for the static
nodeType
property.Declaration
Swift
static var nodeType: NodeType
-
The value of this node, depending on its type.
See also
Text.nodeValue
See also
Comment.nodeValue
See also
CDATASection.nodeValue
Declaration
Swift
var nodeValue: String?
-
The children of this node.
See also
Text.children
See also
ProcessingInstruction.children
See also
Comment.children
See also
CDATASection.children
Declaration
Swift
var children: [Node]
-
Puts all
Text
nodes in the full depth of the sub-tree underneath thisNode
, into anormal
form where only structure (e.g., elements, comments, processing instructions, and CDATA sections) separatesText
nodes, i.e., there are neither adjacentText
nodes nor emptyText
nodes. This can be used to ensure that the DOM view of a document is the same as if it were saved and re-loaded.Declaration
Swift
mutating func normalize()
-
evaluate(path:)
Extension methodSelects nodes based on a path relative to this node. For example, in the following document:
<a id="1"> <b id="2"> <c id="3"/> </b> <c id="4"> <d id="5"/> </c> </a>
evaluating the path
["a", "b", "c"]
relative to the document would select the<c>
element withid="3"
but not the<c>
element withid="4"
.In this example, starting at the document object (the parent of the root
<a>
element), select all children withnodeName == "a"
. From that set of nodes (withnodeName == "a"
), select all children withnodeName == "b"
. Finally, from that set of nodes (withnodeName == "b"
that are children of nodes withnodeName == "a"
), select all children withnodeName == "c"
.Declaration
Swift
public final func evaluate(path: [String]) -> [Node]
Parameters
path
An array of strings, each representing a
nodeName
in the path.Return Value
An array of nodes corresponding to the specified path, relative to this node.
-
prettyPrint(indentWith:)
Extension methodGenerates a formatted XML string representation of this node and its descendants.
Declaration
Swift
public func prettyPrint(indentWith: String = "\t") -> String?
Parameters
indentWith
The string used to indent the formatted string.
Return Value
A formatted XML string representation of this node and its descendants.
-
dump()
Extension methodGenerates an unformatted XML string representation of this node and its descendants.
Declaration
Swift
public func dump() -> String?
Return Value
A formatted XML string representation of this node and its descendants.
-
children(ofType:)
Extension methodFilters the
children
array, keeping only nodes of the specified type. Casts the nodes in the resulting array to the specified type.Declaration
Swift
public final func children<T: Node>(ofType type: T.Type) -> [T]
Parameters
type
Include children of this type in the resulting array
Return Value
The nodes in the
children
array of the specified type -
hasChildren
Extension methodA Boolean value indicating whether the
children
array is not empty.Declaration
Swift
public final var hasChildren: Bool
-
firstChild
Extension methodThe first node in the
children
array.Declaration
Swift
public final var firstChild: Node?
-
lastChild
Extension methodThe last node in the
children
array.Declaration
Swift
public final var lastChild: Node?
-
children(withName:)
Extension methodReturns an array of children with the given
nodeName
.Declaration
Swift
public final func children(withName name: String) -> [Node]
Parameters
name
The node name to find.
Return Value
The children with the given node name.
-
childElements
Extension method -
hasChildElements
Extension methodA Boolean value indicating whether the
childElements
array is not emptyDeclaration
Swift
public final var hasChildElements: Bool
-
firstChildElement
Extension methodThe first element in the
childElements
array.Declaration
Swift
public final var firstChildElement: Element?
-
lastChildElement
Extension methodThe last element in the
childElements
array.Declaration
Swift
public final var lastChildElement: Element?
-
childElements(withName:)
Extension method