Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: option to ignore attributes by name (by array of string | regex) #666

Open
mav-rik opened this issue Aug 4, 2024 · 7 comments

Comments

@mav-rik
Copy link
Contributor

mav-rik commented Aug 4, 2024

Description

Please consider adding an option to the parser/builder to exclude certain attributes by name. This could be implemented as an array of attribute names or a RegExp pattern, similar to existing include/exclude API patters for paths (e.g. typescript.json > exclude).

Use Case

We need to compare two XML documents by parsing and converting them into normalized XML outputs. Sometimes, one XML might contain extra attributes that are irrelevant for our comparison. If we exclude specific attributes during parsing, we'll easily compare the results then.

Suggested API:

const parser = new XMLParser({
  ignoreAttributes: false,
  excludeAttributes: [
    'attr-to-skip',
    /^ns:/,
    // more patterns or attributes here...
  ]
});

Expected Result:

The parser should ignore the specified attributes, ensuring they do not appear in the resulting XML tree.

Copy link

github-actions bot commented Aug 4, 2024

We're glad you find this project helpful. We'll try to address this issue ASAP. You can vist https://solothought.com to know recent features. Don't forget to star this repo.

@amitguptagwl
Copy link
Member

this is a good suggestion. We can probably replace ignoreAttributes with this new property.

@mav-rik
Copy link
Contributor Author

mav-rik commented Aug 19, 2024

this is a good suggestion. We can probably replace ignoreAttributes with this new property.

Do you need any help with PR? I can work on ignoreAttributes to accept an array of regex/strings and send a PR

@amitguptagwl
Copy link
Member

Thanks @mav-rik . I'm busy with a new open source project. So it'll be helpful if you can raise a PR.

I'm just thinking from release perspective if we support both properties for sometime with deprecation warning otherwise it'll be a breaking change.

@mav-rik
Copy link
Contributor Author

mav-rik commented Aug 19, 2024

I'm just thinking from release perspective if we support both properties for sometime with deprecation warning otherwise it'll be a breaking change.

Why don't just keep backwards compatible ignoreAttributes prop like this:

ignoreAttributes: boolean | (string | Regex)[]

So when it is true or false - the current logic works.
And when it is an array of strings/regexp - then we parse attrs and filter those out based on the list.

In this case it won't be a breaking change.

mav-rik added a commit to mav-rik/fast-xml-parser that referenced this issue Aug 21, 2024
@mav-rik
Copy link
Contributor Author

mav-rik commented Aug 31, 2024

@amitguptagwl hey can you please take a look PR #668

Codeclimate fails with error "Avoid deeply nested control flow statements". The thing is I didn't add nor remove any of the if/else statements, I only added a few commands inside. In order to resolve Codeclimate error I'll have to refactor the whole thing which I'd like to avoid.

@amitguptagwl
Copy link
Member

Thanks for the PR @mav-rik. It looks good to me. However, I still need to check thoroughly.

amitguptagwl pushed a commit that referenced this issue Sep 1, 2024
* feat #666: add selective ignoreAttributes by pattern or callback

* chore: resolve codeclimate issues
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants