NaturalIntelligence / fast-xml-parser

Validate XML, Parse XML and Build XML rapidly without C/C++ based libraries and no callback.
https://naturalintelligence.github.io/fast-xml-parser/
MIT License
2.45k stars 296 forks source link

jPath for self-closing XML tag includes slash #564

Closed m-radzikowski closed 1 year ago

m-radzikowski commented 1 year ago

Description

jPath in `updateTag()` callback for self closing tag ends with `/`, which I doubt is intended. Because of this, you need to check two different jPaths, with and without `/`, to accurately process tags that may be empty. ### Input
<doc>
    <foo/>
    <foo>bar</foo>
    <lorem>ipsum</lorem>
</doc>

Code

const skip = ["doc.foo"];
const parser = new XMLParser({
    updateTag: (tagName, jPath) => {
        if (skip.includes(jPath)) {
            return false;
        }
        return tagName;
    },
});

Output

{
  "doc": {
    "foo": "",
    "lorem": "ipsum"
  }
}

expected data

{
  "doc": {
    "lorem": "ipsum"
  }
}

additional info

Code works if the skip array is changed to: ["doc.foo", "doc.foo/"] because for the self-closing tag jPath ends with /. But I think that's easy to miss and not intended to be so? If you write code wanting to skip particular path and you don't test it with both self-closing and not-self closing tag you will be surprised.

Another workaround is to do:

    updateTag: (tagName, jPath) => {
        if (
            skip.includes(jPath) ||
            (jPath.endsWith("/") && skip.includes(jPath.slice(0, -1)))
        ) {
            return false;
        }
        return tagName;
    },

Would you like to work on this issue?

Bookmark this repository for further updates. Visit SoloThought to know about recent features.

github-actions[bot] commented 1 year ago

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 commented 1 year ago

I believe this problem exists with not only jPath but tagName as well. I believe it should be a small fix.

m-radzikowski commented 1 year ago

No, actually, the tagName does not contain a trailing slash from what I've checked.