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.56k stars 307 forks source link

Grouped attributes with a prefix don't get renamed #217

Closed Jasperrr91 closed 2 years ago

Jasperrr91 commented 4 years ago

Checklist

### Input
<scriptedContentFragments>
    <scriptedContentFragment name="${resource:Achievements_AchievementList_Name}" version="11.1.0.9731">
    </scriptedContentFragment>
</scriptedContentFragments>

Code

\Note: parserOptions.xml and parserOptions.json are equal to the sample configurations

const tObj = xmlParser.getTraversalObj(xmlData, parserOptions.xml);
const jsonObj = xmlParser.convertToJson(tObj, parserOptions.xml);
const jsonToXml = new jsonParser(parserOptions.json);
const parsedXml = jsonToXml.parse(jsonObj);

Output

<scriptedContentFragments>
    <scriptedContentFragment @_name="${resource:Achievements_AchievementList_Name}" @_version="11.1.0.9731">
    </scriptedContentFragment>
</scriptedContentFragments>

expected data

<scriptedContentFragments>
    <scriptedContentFragment name="${resource:Achievements_AchievementList_Name}" version="11.1.0.9731">
    </scriptedContentFragment>
</scriptedContentFragments>

The issues seems to be that if this.options.attrNodeName is set (which it is, because the attributes are grouped under the default attr key) the entire prefix gets ignored.

Would you like to work on this issue?

Bookmark this repository for further updates.

github-actions[bot] commented 4 years ago

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

amitguptagwl commented 2 years ago

This works fine for me

const XMLdata = `
      <car>
        <color alpha="7">purple</color>
        <type>minivan</type>
        <registration>2020-02-03</registration>
        <capacity>7</capacity>
      </car>`;

      const options = {
        ignoreAttributes: false,
        attributeNamePrefix: "",
        attributesGroupName:        "@@",
        format: true
      };
      const parser = new XMLParser(options);
      let result = parser.parse(XMLdata);
      console.log(JSON.stringify(result, null,4));

      const builder = new XMLBuilder(options);
      const output = builder.build(result);
      console.log(output);

Please reopen the issue if still exists