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.49k stars 302 forks source link

transformTagName and transformAttributeName aren't typed for Builder. #524

Closed raarts closed 1 year ago

raarts commented 1 year ago

Description

TypeScript gives an error when trying to add transformTagName or transformAttributeName to the options

Side issue: there a small bug in OrderedObjsParser, there's a blank between this. and options. JS is forgiving so doesn't break anything, but still.

Input

n/a

Code

  const builder = new XMLBuilder({
    ignoreAttributes: false,
    attributeNamePrefix: '',
    attributesGroupName: 'attrs',
    transformTagName: (tag) => {
    // ^^^ Object literal may only specify known properties, and 'transformTagName' does not exist in type 'Partial<XmlBuilderOptions>'.
      const newName = json2xml.get(tag);
      if (newName) return newName;
      return tag;
    },
    transformAttributeName: false,
  });

Output

expected data

Would you like to work on this issue?

github-actions[bot] commented 1 year ago

I'm glad you find this repository helpful. I'll try to address your issue ASAP. You can watch the repo for new changes or star it.

raarts commented 1 year ago

I found another bug: textnodes are suddenly being created when I use transformTagName. Tracing what happens, a question arises: why aren't transformTagName (and removeNSPrefix) implemented in compress(), just before the JSON property is created? That would be the logical location.

amitguptagwl commented 1 year ago

@raarts transformAttributeName and transformTagName are not the options for builder but for parser only. compress is an internal method the purpose of that to prettify the ordered json into normal json. Because ordered json is big list of nodes.

Can you please add more detail on "I found another bug: textnodes are suddenly being created when I use transformTagName. "?

raarts commented 1 year ago

Indeed. I found it in the source code in the Builder options, but it doesn't do anything.

About the other bug: I was using parseTagValue, but when I added transformTagName suddenly #text nodes again were generated. I can't reproduce it any more, since I switched to xmls-js, I needed to import and then re-export while translating tags and attribute names. Sorry for wasting your time, I should have closed this bug report.

amitguptagwl commented 1 year ago

no issue. I have commented unnecessary function in the recent release. Thanks