Open JacobSandin opened 4 years ago
Glad I'm not the only one who tripped over XML namespaces. You'll have to set up a Context and assign the appropriate namespace(s) to it. Something like the following should work:
use sxd_document::parser;
use sxd_xpath::{Context, Factory};
fn main() {
let package = parser::parse(r#"<?xml version="1.0" encoding="UTF-8"?>
<record
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.loc.gov/MARC21/slim
http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
xmlns="http://www.loc.gov/MARC21/slim">
<leader>00446nim a22001813 45 </leader>
</record>"#).expect("failed to parse XML");
let document = package.as_document();
let factory = Factory::new();
let xpath = factory.build("/ns:record/ns:leader").unwrap().unwrap();
let mut context = Context::new();
context.set_namespace("ns", "http://www.loc.gov/MARC21/slim");
let value = xpath.evaluate(&context, document.root()).unwrap();
println!("Found: {}({:?})", value.string(), value);
}
That returns
Found: 00446nim a22001813 45 (Nodeset(Nodeset { nodes: {Element(Element { name: QName { namespace_uri: Some("http://www.loc.gov/MARC21/slim"), local_part: "leader" } })} }))
Ok so im trying to parse MARC 21 XML reccords. And I might mention I do this to learn rust, and MARC wich both are quite complicated.
However Ive got tvo examples one that work and one where I change this:
to simply
The later works and the first one just return empty :
Example1:
Output:
Example2:
Returns:
Im not sure why it dont lake the part of record, it just dont :)
Kind regards /Jacob