Add PureXML.XPath: a compiled location-path query engine reimplemented from the XPath 1.0 specification; no upstream code copied. - Compiler parses a path into steps; Evaluator walks the tree returning a Selection node-set (node or attribute) in document order. - Forward axes only (child, descendant //, self ., attribute @), since the value model has no parent pointers. Node tests: name, *, text(), node(), comment(). Predicates: [n], [@a], [@a='v'], [child], [child='v']. - Public surface: XPath.Query (init compiles, evaluate/elements/strings), the Selection enum (element / stringValue), QueryError, and the one-shot PureXML.xpath(_:over:). Upward/sibling axes and the full expression language are explicitly out of scope. - 12 XPath tests; 61 total.
91 lines
3.8 KiB
Swift
91 lines
3.8 KiB
Swift
public extension PureXML {
|
|
/// Parses an XML document into a ``Model/Node`` tree.
|
|
///
|
|
/// The parser is still being built; this entry point currently raises
|
|
/// ``Parsing/ParseError/notImplemented(_:)``. The serializer and model are
|
|
/// usable today for building and emitting trees.
|
|
static func parse(_ xml: String, limits: Parsing.Limits = .default) throws -> Model.Node {
|
|
try Parsing.Parser().parse(xml, limits: limits)
|
|
}
|
|
|
|
/// Parses an XML document from an incremental character source into a
|
|
/// ``Model/Node`` tree. The closure returns the next character or nil at end
|
|
/// of input, so the document can arrive in chunks and is never held whole.
|
|
static func parse(
|
|
pulling pull: @escaping () -> Character?,
|
|
limits: Parsing.Limits = .default,
|
|
) throws -> Model.Node {
|
|
try Parsing.Parser().parse(pulling: pull, limits: limits)
|
|
}
|
|
|
|
/// Parses an XML document from raw bytes, detecting the encoding (UTF-8 or
|
|
/// UTF-16, with or without a byte-order mark) before parsing.
|
|
static func parse(bytes: [UInt8], limits: Parsing.Limits = .default) throws -> Model.Node {
|
|
try Parsing.Parser().parse(bytes: bytes, limits: limits)
|
|
}
|
|
|
|
/// Returns a streaming ``Parsing/EventReader`` over an XML string. Pull events
|
|
/// one at a time with `next()` to process documents without building a tree.
|
|
static func events(_ xml: String, limits: Parsing.Limits = .default) -> Parsing.EventReader {
|
|
Parsing.EventReader(xml, limits: limits)
|
|
}
|
|
|
|
/// Returns a streaming ``Parsing/EventReader`` over raw bytes, detecting the
|
|
/// encoding before streaming events.
|
|
static func events(bytes: [UInt8], limits: Parsing.Limits = .default) throws -> Parsing.EventReader {
|
|
try Parsing.EventReader(Parsing.ByteDecoder.decode(bytes), limits: limits)
|
|
}
|
|
|
|
/// Returns a streaming ``Parsing/EventReader`` over an incremental character
|
|
/// source, for processing arbitrarily large or chunked input.
|
|
static func events(
|
|
pulling pull: @escaping () -> Character?,
|
|
limits: Parsing.Limits = .default,
|
|
) -> Parsing.EventReader {
|
|
Parsing.EventReader(pulling: pull, limits: limits)
|
|
}
|
|
|
|
/// Parses an XML document from an incremental byte source, decoding the
|
|
/// encoding on the fly so the bytes are never fully buffered.
|
|
static func parse(
|
|
pullingBytes pull: @escaping () -> UInt8?,
|
|
limits: Parsing.Limits = .default,
|
|
) throws -> Model.Node {
|
|
try Parsing.Parser().parse(pullingBytes: pull, limits: limits)
|
|
}
|
|
|
|
/// Returns a streaming ``Parsing/EventReader`` over an incremental byte
|
|
/// source, decoding the encoding on the fly.
|
|
static func events(
|
|
pullingBytes pull: @escaping () -> UInt8?,
|
|
limits: Parsing.Limits = .default,
|
|
) -> Parsing.EventReader {
|
|
var decoder = Parsing.StreamingDecoder(pullingBytes: pull)
|
|
return Parsing.EventReader(pulling: { decoder.next() }, limits: limits)
|
|
}
|
|
|
|
/// Compiles and evaluates an XPath query over a node, returning the selected
|
|
/// node-set. Compile once with ``XPath/Query`` to reuse a query.
|
|
static func xpath(_ path: String, over node: Model.Node) throws -> [XPath.Selection] {
|
|
try XPath.Query(path).evaluate(over: node)
|
|
}
|
|
|
|
/// Serializes a ``Model/Node`` tree into XML with the selected options.
|
|
static func serialize(
|
|
_ node: Model.Node,
|
|
options: Emitting.Options = .default,
|
|
) -> String {
|
|
Emitting.Serializer(options: options).serialize(node)
|
|
}
|
|
|
|
/// Validates a parsed XML node with the default validation rules.
|
|
@discardableResult
|
|
static func validate(
|
|
_ node: Model.Node,
|
|
using validator: Validation.Validator = .init(),
|
|
strict: Bool = true,
|
|
) throws -> [Validation.Issue] {
|
|
try validator.validate(node, strict: strict)
|
|
}
|
|
}
|