Skip to content

transformTagName with allowBooleanAttributes adds an unnecessary attribute #775

@levensta

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions