Skip to content

Not all attributes of tag in .attributes() #299

Closed

Description

@swi2012

I'm trying quick-xml and i found a strnage behavior on my xml file (a sample attached). When i parse event Begin on tag SECURITY i get only two first attributes. But on tag RECORDS i get all attributes successfully.
My code like

Ok(Event::Start(ref e)) => {
                        if let b"SECURITY" = e.name() {
                            // cycle through attrs
                            for el in e.attributes() {
                                let e = el?;
                                print!("{}, ", std::str::from_utf8(e.key)?);
                            }
                            println!();
                        };
[1.xml.gz](https://github.com/tafia/quick-xml/files/6868035/1.xml.gz)

Activity

  1. tafia commented on Aug 10, 2021

    @tafia
    Owner

    I can't get your file, are you still having some issue?

  2. swi2012 commented on Aug 10, 2021

    @swi2012
    Author

    Hello. Yes issue is still here.
    Here is file content

    <?xml version="1.0" encoding="windows-1251"?>
    <MICEX_DOC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <SECURITY SecurityId="PLZL" ISIN="RU000A0JNAA8" SecShortName="Short Name" PriceType="CASH">
        <RECORDS RecNo="1" TradeNo="1111" TradeDate="2021-07-08" TradeTime="15:00:00" BuySell="S" SettleCode="Y1Dt" Decimals="3" Price="13057.034" Quantity="766" Value="10001688.29" AccInt="0" Amount="10001688.29" Balance="766" TrdAccId="X0011" ClientDetails="2222" CPFirmId="3333" CPFirmShortName="Firm Short Name" Price2="13057.034" RepoPart="2" ReportTime="16:53:27" SettleTime="17:47:06" ClientCode="4444" DueDate="2021-07-09" EarlySettleStatus="N" RepoRate="5.45" RateType="FIX"/>
      </SECURITY>
    </MICEX_DOC>
    
  3. tafia commented on Aug 11, 2021

    @tafia
    Owner

    I haven't tried but your encoding is windows-1251 not utf8, could you try using unescape_and_decode ?

  4. swi2012 commented on Aug 11, 2021

    @swi2012
    Author

    I use feature 'encoding'. Even so with utf8 file it the same:

    <?xml version="1.0" encoding="utf-8"?>
    <MICEX_DOC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <SECURITY SecurityId="PLZL" ISIN="RU000A0JNAA8" SecShortName="Short Name" PriceType="CASH">
        <RECORDS RecNo="1" TradeNo="1111" TradeDate="2021-07-08" TradeTime="15:00:00" BuySell="S" SettleCode="Y1Dt" Decimals="3" Price="13057.034" Quantity="766" Value="10001688.29" AccInt="0" Amount="10001688.29" Balance="766" TrdAccId="X0011" ClientDetails="2222" CPFirmId="3333" CPFirmShortName="Firm Short Name" Price2="13057.034" RepoPart="2" ReportTime="16:53:27" SettleTime="17:47:06" ClientCode="4444" DueDate="2021-07-09" EarlySettleStatus="N" RepoRate="5.45" RateType="FIX"/>
      </SECURITY>
    </MICEX_DOC>

    output:
    read file 1.xml reader opened some SECURITY: SecurityId, ISIN, RECORDS: RecNo,TradeNo,TradeDate,TradeTime,BuySell,SettleCode,Decimals,Price,Quantity,Value,AccInt,Amount,Balance,TrdAccId,ClientDetails,CPFirmId,CPFirmShortName,Price2,RepoPart,ReportTime,SettleTime,ClientCode,DueDate,EarlySettleStatus,RepoRate,RateType,

  5. tafia commented on Aug 14, 2021

    @tafia
    Owner

    Hi,
    I've tried this test and it seems to work as expected. Closing for now, feel free to reopen and provide more information.

    #[test]
    fn test_issue299() -> Result<(), Error> {
        let xml = r#"
    <?xml version="1.0" encoding="utf8"?>
    <MICEX_DOC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <SECURITY SecurityId="PLZL" ISIN="RU000A0JNAA8" SecShortName="Short Name" PriceType="CASH">
        <RECORDS RecNo="1" TradeNo="1111" TradeDate="2021-07-08" TradeTime="15:00:00" BuySell="S" SettleCode="Y1Dt" Decimals="3" Price="13057.034" Quantity="766" Value="10001688.29" AccInt="0" Amount="10001688.29" Balance="766" TrdAccId="X0011" ClientDetails="2222" CPFirmId="3333" CPFirmShortName="Firm Short Name" Price2="13057.034" RepoPart="2" ReportTime="16:53:27" SettleTime="17:47:06" ClientCode="4444" DueDate="2021-07-09" EarlySettleStatus="N" RepoRate="5.45" RateType="FIX"/>
      </SECURITY>
    </MICEX_DOC>
    "#;
        let mut reader = Reader::from_str(xml);
        loop {
            match reader.read_event_unbuffered()? {
                Start(e) | Empty(e) => {
                    let attr_count = match e.name() {
                        b"MICEX_DOC" => 1,
                        b"SECURITY" => 4,
                        b"RECORDS" => 26,
                        _ => unreachable!(),
                    };
                    assert_eq!(
                        attr_count,
                        e.attributes().filter(Result::is_ok).count(),
                        "mismatch att count on '{}'",
                        reader.decoder().decode(e.name())?
                    );
                }
                Eof => break,
                _ => (),
            }
        }
        Ok(())
    }

    (I'll add it to the tests).

  6. added a commit that references this issue on Sep 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions