xpath-evaluator

    A specification-driven XPath 1.0 compiler and evaluator for MoonBit.

    xpath
    xml
    query
    parser
    interoperability
    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    3 months ago
    Downloads
    41

    #XPath Evaluator

    面向 MoonBit 的 XPath 1.0 编译与求值库。它将 XPath 表达式编译为可复用的私有 IR,在自有、不可变的 XML/XPath 数据模型上执行查询;XML 词法解析由 Milky2018/xml@0.4.0 提供,但 XPath 语法、轴、谓词、转换、函数和诊断均为本项目 原创 MoonBit 实现。

    #已实现的完整功能范围

    • XPath 1.0 语法、运算符优先级、缩写、UTF-16 源码区间和静态 QName 校验;
    • XPath root、element、attribute、namespace、text、PI、comment 七类节点;
    • 13 个 XPath 轴、QName/通配符/节点类型测试、谓词和反向轴上下文顺序;
    • node-set、boolean、number、string 的转换、比较、算术与 union;
    • 27 个 XPath 1.0 核心函数、静态/动态上下文、变量和求值资源上限;
    • namespace-aware XML adapter、compile/evaluate/select/evaluate_xml API;
    • 原生 xpath eval CLI,支持文件或 STDIN、命名空间、文本/JSON 输出和 XML 输入限制。

    库核心在 Wasm、Wasm-GC、JavaScript、Native target 上测试;CLI 仅支持 Native。

    #安装

    该库已发布到 mooncakes.io。在使用方项目根目录添加 v0.1.0:

    moon add cravan371/xpath-evaluator@0.1.0

    在 MoonBit 包的 moon.pkg 中导入根 facade:

    import {
    "cravan371/xpath-evaluator" @xpath,
    }

    #快速开始

    在项目根目录执行:

    moon run src/cmd/xpath --target native -- eval \ --expr "//book[@category='fiction']/title/text()" examples/books.xml

    输出:

    1984

    查询 WebDAV 的命名空间 XML 并得到结构化 JSON:

    moon run src/cmd/xpath --target native -- eval \ --expr "//d:response/d:href/text()" \ --namespace d=DAV: --format json examples/webdav-multistatus.xml

    也可用 - 从标准输入读取 XML。CLI 的完整形式如下:

    xpath eval --expr <XPath> [--namespace prefix=uri] [--format text|json] [--max-input-units N] [--max-nodes N] [--max-depth N] [xml-file|-]

    #库 API

    let static_context = StaticContext::new()
    let compiled = compile("//book/title/text()", static_context)
    let document = parse_document("<library><book><title>1984</title></book></library>")
    let context = DynamicContext::new(document, document.root())
    let titles = select(compiled, context)

    // 一次性场景:先编译表达式,随后解析 XML 并求值。
    let result = evaluate_xml("count(//book)", "<library><book/></library>")

    对带前缀的 XPath 名称,必须在 StaticContext 或 CLI --namespace 中绑定前缀。 XPath 中未加前缀的名称测试始终匹配 null namespace,符合 XPath 1.0。

    #验证

    moon fmt --check moon check --target all --deny-warn moon test --target all --warn-list -68-1+73 moon build src/cmd/xpath --target native moon info --target all bash scripts/cli_e2e.sh

    当前测试覆盖数据模型与值转换、lexer/parser/static validation、13 个轴、谓词、 函数、变量、资源限制、XML adapter、facade 和 CLI 参数解析。示例 XML 与测试 fixture 均由本项目原创,详见 fixture manifest。

    #设计边界

    • 规范范围是 XPath 1.0;不实现 XPath 2.0/3.x、XQuery、XSLT 或 extension functions;
    • XML 输入使用严格 XML 解析,不提供 HTML5 容错解析;
    • XML adapter 不可获得 DTD 属性类型或默认属性,因此 id() 只能依赖 DocumentBuilder::add_attribute(..., is_id=true) 提供的显式 typed-ID 元数据;
    • 一个 NodeSet 只能属于一个 Document;跨文档变量会返回 checked error;
    • 当前模块名为 cravan371/xpath-evaluator,稳定发布版为 v0.1.0。

    完整限制见 LIMITATIONS.md。

    #文档

    #许可证

    本项目采用 MIT License。

    CompiledXPath

    Public compiled-expression and context types.

    Document

    Public XPath document model and adapter builder.

    DocumentBuilder

    Public XPath document model and adapter builder.

    NodeId

    Public XPath document model and adapter builder.

    NodeKind

    Public XPath document model and adapter builder.

    NodeSet

    Public XPath values.

    QName

    Public XPath document model and adapter builder.

    SourceSpan

    Public diagnostics.

    XPathError

    Public diagnostics.

    XPathPhase

    Public diagnostics.

    XPathValue

    Public XPath values.

    evaluate_xml

    Parse XML, compile an expression and evaluate it once.

    Prefer the separate APIs when one expression or document is reused. The expression is compiled before XML parsing so expression errors win when both inputs are invalid.

    parse_document

    fn parse_document(xml : String, max_input_units? : Int, max_nodes? : Int, max_depth? : Int) ->
    Document
    raise
    XPathError

    Parse XML with the pinned Milky2018/xml adapter and build an immutable XPath document. The parser dependency remains hidden from public types.

    Source Files