Paligo/xee

Getting namespace declarations

Offen

#71 geöffnet am 30.03.2025

 (6 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)Rust (40 Forks)auto 404
help wanted

Repository-Metriken

Stars
 (484 Sterne)
PR-Merge-Metriken
 (PR-Metriken ausstehend)

Beschreibung

My end goal is to be able to query metadata elements that have namespace prefixes, so at the moment I'm trying to get the namespace declarations on a certain element. However, I'm getting an error that indicates the types aren't correct in the AST (or something like that).

Am I holding it wrong?

Here's an example test:

#[test]
fn gets_namespaces() {
    let mut docs = Documents::new();
    let xml_str = std::fs::read_to_string(test_xml_named("epub_package_doc.xml")).unwrap();
    let doc = docs.add_string_without_uri(&xml_str).unwrap();
    let queries = Queries::default();
    let q = queries
        .one("//metadata", |_, item| {
            eprintln!("item: {item:?}");
            Ok(item.to_node()?)
        })
        .unwrap();
    let node = q.execute(&mut docs, doc).unwrap();
    let namespaces = docs.xot().namespace_declarations(node);
    eprintln!("namespaces: {namespaces:?}");
}

The error I'm getting (the line number is the where the execute happens):

thread 'xml::tests::gets_namespaces' panicked at vellichor-epub/src/xml.rs:124:46:
called `Result::unwrap()` on an `Err` value: SpannedError { error: XPTY0004, span: None }

A sample XML document to use (same one I'm using in the test with extra stuff stripped out):

<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="pub-id" version="3.0" xml:lang="en">
  <metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
    <dc:title>Deep Work: Rules for Focused Success in a Distracted World</dc:title>
    <dc:creator id="creator01">Cal Newport</dc:creator>
    <meta property="role" refines="#creator01" scheme="marc:relators">aut</meta>
    <meta property="file-as" refines="#creator01">NEWPORT, CAL</meta>
    <meta property="display-seq" refines="#creator01">1</meta>
    <dc:publisher>Grand Central Publishing</dc:publisher>
    <dc:rights>Copyright © 2016 by Cal Newport</dc:rights>
    <dc:date>2016-01-05</dc:date>
    <dc:subject>Business &amp; Economics / Time Management</dc:subject>
    <dc:description>Georgetown University professor Cal Newport argues that the ability to apply sustained attention to a valuable task---to engage in what he terms ‘deep work’---is the new key to achieving real success and true meaning in one’s professional life.</dc:description>
    <meta property="dcterms:modified">2015-10-31T11:30:15Z</meta>
    <dc:language>en</dc:language>
    <meta content="cover-image" name="cover"/>
    <dc:identifier id="pub-id">urn:uuid:6c2a8d92-09e8-4187-8221-69feda17116a</dc:identifier>
  </metadata>
</package>

Contributor Guide