Visitar URL original
`transformTagName` with `allowBooleanAttributes` adds an unnecessary attribute · Issue #775 · NaturalIntelligence/fast-xml-parser · GitHub
Skip to content

transformTagName with allowBooleanAttributes adds an unnecessary attribute #775

Closed

Description

@levensta
  • Are you running the latest version?
  • Have you included sample input, output, error, and expected output?
  • Have you checked if you are using correct configuration?
  • Did you try online tool?
  • Have you checked the docs for helpful APIs and examples?

Description

If two parameters are used in XMLParser: transformTagName and allowBooleanAttributes, then a boolean attribute with the original tag name is added to tags that have been renamed and do not contain attributes.

In my example, it works like this: I rename list-item to li and get the following result: <list-item></list-item> -> <li list-item=""></li>. To rule out other factors, I tried renaming tags that contained attributes, including boolean attribute. I also tried using updateTag option instead of transformTagName`, and the problem did not recur.

I also came across an issue with a similar description, but it seems to have been abandoned and the author did not provide any details #523

Input

Code

const tagMap = { 'list-item': 'li' };

const xmlParser = new XMLParser({
  preserveOrder: true,
  allowBooleanAttributes: true,
  ignoreAttributes: false,
  transformTagName: (tagName) => tagMap[tagName] ?? tagName,
})

const jObj = xmlParser.parse(`<?xml version="1.0"?>
<root>
<ul>
  <list-item>foo</list-item>
  <list-item checked>bar</list-item>
  <list-item attr="value">bar</list-item>
</ul>
</root>`)

console.log(JSON.stringify(jObj, null, 2));

Output

With preserveOrder
[
  {
    "?xml": [
      {
        "#text": ""
      }
    ],
    ":@": {
      "@_version": "1.0"
    }
  },
  {
    "root": [
      {
        "ul": [
          {
            "li": [
              {
                "#text": "foo"
              }
            ],
            ":@": {
              "@_list-item": true
            }
          },
          {
            "li": [
              {
                "#text": "bar"
              }
            ],
            ":@": {
              "@_checked": true
            }
          },
          {
            "li": [
              {
                "#text": "bar"
              }
            ],
            ":@": {
              "@_attr": "value"
            }
          }
        ]
      }
    ]
  }
]
Without preserveOrder
{
  "root": {
    "ul": {
      "li": [
        {
          "#text": "foo",
          "@_list-item": true
        },
        {
          "#text": "bar",
          "@_checked": true
        },
        {
          "#text": "bar",
          "@_attr": "value"
        }
      ],
      "#text": "\n  \n  \n  \n"
    }
  }
}

expected data

With preserveOrder
[
  {
    "?xml": [
      {
        "#text": ""
      }
    ],
    ":@": {
      "@_version": "1.0"
    }
  },
  {
    "root": [
      {
        "ul": [
          {
            "li": [
              {
                "#text": "foo"
              }
            ],
          },
          {
            "li": [
              {
                "#text": "bar"
              }
            ],
            ":@": {
              "@_checked": true
            }
          },
          {
            "li": [
              {
                "#text": "bar"
              }
            ],
            ":@": {
              "@_attr": "value"
            }
          }
        ]
      }
    ]
  }
]
Without preserveOrder
{
  "root": {
    "ul": {
      "li": [
        {
          "#text": "foo"
        },
        {
          "#text": "bar",
          "@_checked": true
        },
        {
          "#text": "bar",
          "@_attr": "value"
        }
      ],
      "#text": "\n  \n  \n  \n"
    }
  }
}

Would you like to work on this issue?

  • Yes
  • No

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

Activity

  1. github-actions commented on Dec 2, 2025

    @github-actions

    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.

  2. levensta commented on Dec 11, 2025

    @levensta
    Author

    @amitguptagwl please note

  3. amitguptagwl commented on Dec 12, 2025

    @amitguptagwl
    Member

    The issue is fixed. will be publishing soon

  4. added a commit that references this issue on Dec 15, 2025
    fcedd31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions