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.54k stars 306 forks source link

Unpaired Tags unexpected output #534

Closed croghostrider closed 1 year ago

croghostrider commented 1 year ago

Description

Hi if I try the Unpaired Tags example with version 4.0.13 I don't receive the same output as in the example.

Code

const xmlData = `
    <rootNode>
      <tag>value</tag>
      <empty />
      <unpaired>
      <unpaired />
      <unpaired></unpaired>
    </rootNode>`

const options = {
  format: true,
  unpairedTags: ['unpaired']
}
const parser = new fxparser.XMLParser(options)
const result = parser.parse(xmlData)

const builder = new fxparser.XMLBuilder(options)
const output = builder.build(result)

Output

<rootNode>
  <tag>value</tag>
  <empty></empty>
  <unpaired></unpaired>
  <unpaired></unpaired>
  <unpaired></unpaired>
</rootNode>

expected data

<rootNode>
  <tag>value</tag>
  <empty></empty>
  <unpaired/>
  <unpaired/>
  <unpaired/>
</rootNode>

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.

amitguptagwl commented 1 year ago

Seems like docs need to be updated. For the time, this is the solution.

amitguptagwl commented 1 year ago

fixed

croghostrider commented 1 year ago

hi @amitguptagwl unfortunately the parameter "suppressUnpairedNode" does not help.

Code

const xmlData = `
    <rootNode>
      <tag>value</tag>
      <empty />
      <unpaired>
      <unpaired />
      <unpaired></unpaired>
    </rootNode>`

const options = {
  format: true,
  suppressUnpairedNode: false,
  unpairedTags: ['unpaired']
}
const parser = new fxparser.XMLParser(options)
const result = parser.parse(xmlData)

const builder = new fxparser.XMLBuilder(options)
const output = builder.build(result)

Output

<rootNode>
  <tag>value</tag>
  <empty></empty>
  <unpaired></unpaired>
  <unpaired></unpaired>
  <unpaired></unpaired>
</rootNode>

expected data

<rootNode>
  <tag>value</tag>
  <empty></empty>
  <unpaired/>
  <unpaired/>
  <unpaired/>
</rootNode>
amitguptagwl commented 1 year ago

what version are you using?

croghostrider commented 1 year ago

4.0.13

amitguptagwl commented 1 year ago

This is working completely fine to me

        const xmlData = `
        <rootNode>
          <tag>value</tag>
          <empty />
          <unpaired>
          <unpaired />
          <unpaired></unpaired>
        </rootNode>`

        const options = {
        format: true,
        suppressUnpairedNode: false,
        unpairedTags: ['unpaired']
        }
        const parser = new XMLParser(options)
        const result = parser.parse(xmlData)

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

Output

<rootNode>
  <tag>value</tag>
  <empty></empty>
  <unpaired/>
  <unpaired/>
  <unpaired/>
</rootNode>
croghostrider commented 1 year ago

Strange, here on Codepen you can see it doesn't work.

amitguptagwl commented 1 year ago

I have noticed that CDN sometime hold old library. So I'm not sure if that is correct.

croghostrider commented 1 year ago

Hi @amitguptagwl

The file from the CDN is identical to the file from Github, but I noticed that the last change was more than 1 year ago. image

croghostrider commented 1 year ago

I found the mistake I will open a new issue.