Module: Perfect-XML
class XNode: CustomStringConvertible
Base class for all XML nodes.
This is intended to track the DOM Core level 2 specification as much as is practically possible.
http://www.w3.org/TR/DOM-Level-2-Core/core.html
var nodeName: String
The name of this node, depending on its type.
var nodeValue: String?
The value of this node, depending on its type. When it is defined to be null, setting it has no effect.
var nodeType: XNodeType
A code representing the type of the underlying object.
var parentNode: XNode?
The parent of this node. All nodes, except Attr, Document, DocumentFragment, Entity, and Notation may have a parent. However, if a node has just been created and not yet added to the tree, or if it has been removed from the tree, this is null.
var childNodes: [XNode]
A NodeList that contains all children of this node. If there are no children, this is a NodeList containing no nodes.
var firstChild: XNode?
The first child of this node. If there is no such node, this returns null.
var lastChild: XNode?
The last child of this node. If there is no such node, this returns null.
var previousSibling: XNode?
The node immediately preceding this node. If there is no such node, this returns null.
var nextSibling: XNode?
The node immediately following this node. If there is no such node, this returns null.
var ownerDocument: XDocument?
The Document object associated with this node. This is also the Document object used to create new nodes. When this node is a Document or a DocumentType which is not used with any Document yet, this is null.
var attributes: XNamedNodeMap?
A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.
var namespaceURI: String?
The namespace URI of this node, or null if it is unspecified.
This is not a computed value that is the result of a namespace lookup based on an examination of the namespace declarations in scope. It is merely the namespace URI given at creation time.
For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and nodes created with a DOM Level 1 method, such as createElement from the Document interface, this is always null.
var prefix: String?
The namespace prefix of this node, or null if it is unspecified.
var localName: String?
Returns the local part of the qualified name of this node.
For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and nodes created with a DOM Level 1 method, such as createElement from the Document interface, this is always null.
func string(pretty: Bool = false) -> String
Convert the node tree to String. Optionally pretty-print.
var description: String
The non-pretty printed string value.
class XDocument: XNode
An XML document.
var documentElement: XElement?
This is a convenience attribute that allows direct access to the child node that is the root element of the document.
func init?(fromSource: String)
Parse the XML source string and create the document, if possible.
func getElementsByTagName(_ name: String) -> [XElement]
Returns a NodeList of all the Elements with a given tag name in the order in which they are encountered in a preorder traversal of the Document tree.
func getElementsByTagNameNS(namespaceURI: String, localName: String) -> [XElement]
Returns a NodeList of all the Elements with a given local name and namespace URI in the order in which they are encountered in a preorder traversal of the Document tree.
func getElementById(_ elementId: String) -> XElement?
Returns the Element whose ID is given by elementId. If no such element exists, returns null. Behavior is not defined if more than one element has this ID.
Note that this implimentation looks explicitly for an "id" attribute.
class HTMLDocument: XDocument
func init?(fromSource: String, encoding: String = "UTF-8")
Parse the HTML source string and create the document, if possible.
class XElement: XNode
An XML element node.
var tagName: String
The name of the element.
func getAttribute(name: String) -> String?
Retrieves an attribute value by name.
func getAttributeNode(name: String) -> XAttr?
Retrieves an attribute node by name.
To retrieve an attribute node by qualified name and namespace URI, use the getAttributeNodeNS method.
func getAttributeNodeNS(namespaceURI: String, localName: String) -> XAttr?
Retrieves an Attr node by local name and namespace URI. HTML-only DOM implementations do not need to implement this method.
func hasAttribute(name: String) -> Bool
Returns true when an attribute with a given name is specified on this element or has a default value, false otherwise.
func hasAttributeNS(namespaceURI: String, localName: String) -> Bool
Returns true when an attribute with a given local name and namespace URI is specified on this element or has a default value, false otherwise.
func getElementsByTagName(_ name: String) -> [XElement]
Returns a NodeList of all descendant Elements with a given tag name, in the order in which they are encountered in a preorder traversal of this Element tree.
func getElementsByTagNameNS(namespaceURI: String, localName: String) -> [XElement]
Returns a NodeList of all the descendant Elements with a given local name and namespace URI in the order in which they are encountered in a preorder traversal of this Element tree.
class XAttr: XNode
A single XML element attribute node.
var name: String
Returns the name of this attribute.
var value: String
On retrieval, the value of the attribute is returned as a string. Character and general entity references are replaced with their values. See also the method getAttribute on the Element interface.
var ownerElement: XElement?
The Element node this attribute is attached to or null if this attribute is not in use.
extension XNode
func extract(path: String, namespaces: [(String, String)] = [(String, String)]()) -> XPathObject
Execute the XPath and return the result(s).
Accepts and array of tuples holding namespace prefixes and uris.
func extractOne(path: String, namespaces: [(String, String)] = [(String, String)]()) -> XNode?
Execute the XPath and return a single resul tnode or nil.
Accepts and array of tuples holding namespace prefixes and uris.