Adding locations to XML names and attribute values

How can I add a (first, last) span to each qualified name and attribute value when parsing a XML using the xml crate?

I've tried forking it, but the first location is the same as the last location, pointing to after the last character.

impl PullParser {
    fn read_qualified_name<F>(&mut self, t: Token, target: QualifiedNameTarget, on_name: F) -> Option<Result>
      where F: Fn(&mut PullParser, Token, (OwnedName, TextSpan)) -> Option<Result> {
        let start = self.lexer.position();

        ...

        let invoke_callback = move |this: &mut PullParser, t| {
            let name = this.take_buf();
            match name.parse() {
                Ok(name) => on_name(this, t, (name, TextSpan { start, end: this.lexer.position() })),
                Err(()) => Some(this.error(SyntaxError::InvalidQualifiedName(name.into()))),
            }
        };

        ...
    }
}

Debugging:

#[test]
fn f1() {
    use crate::reader::XmlEvent;
    let my_xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<s:Application
    xmlns:fx="http://ns.adobe.com/flex"
    xmlns:s="library://ns.adobe.com/flex/spark"
    creationComplete="initialize()">
</s:Application>
    "#;
    let mut reader = EventReader::new(my_xml.as_bytes());
    while let Ok(event) = reader.next() {
        match event {
            XmlEvent::StartElement { name, attributes, .. } => {
                println!("{}", name.1);
                for attr in attributes {
                    println!("{}={}", attr.name.1, attr.value.1);
                }
            },
            XmlEvent::EndDocument => {
                break;
            },
            _ => {},
        }
    }
}

Issue:

Fork:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.