Skip to content

Unexpected results when I use transformTagName alongside allowBooleanAttributes. #523

@facorreg

Description

@facorreg
  • 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?

Description

Typescript: 4.9.4
fast-xml-parser: ^4.0.12

Unexpected results when I use transformTagName alongside allowBooleanAttributes.

Additionally (but off-topic), transformAttributeName, although mentionned in the documentation, is not a valid X2jOptions property.

Input

Code

import fxp from "fast-xml-parser";

const entry = `
  <entry>
    <ent_seq>1358280</ent_seq>
    <k_ele>
      <keb>食べる</keb>
      <ke_pri>ichi1</ke_pri>
      <ke_pri>news2</ke_pri>
      <ke_pri>nf25</ke_pri>
    </k_ele>
    <k_ele>
      <keb>喰べる</keb>
      <ke_inf>&iK;</ke_inf>
    </k_ele>
    <r_ele>
      <reb>たべる</reb>
      <re_pri>ichi1</re_pri>
      <re_pri>news2</re_pri>
      <re_pri>nf25</re_pri>
    </r_ele>
  </entry>
`;

const parser = new fxp.XMLParser({
  ignoreAttributes: false,
  removeNSPrefix: true,
  allowBooleanAttributes: true,
  attributesGroupName: "@_",
  attributeNamePrefix: "",
  transformTagName: (tagName) => tagName.toUpperCase(),
});

const parsed = parser.parse(entry);

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

Output

{
  "ENTRY": {
    "ENT_SEQ": {
      "#text": 1358280,
      "@_": {
        "ent_seq": true
      }
    },
    "K_ELE": [
      {
        "KEB": {
          "#text": "食べる",
          "@_": {
            "keb": true
          }
        },
        "KE_PRI": [
          {
            "#text": "ichi1",
            "@_": {
              "ke_pri": true
            }
          },
          {
            "#text": "news2",
            "@_": {
              "ke_pri": true
            }
          },
          {
            "#text": "nf25",
            "@_": {
              "ke_pri": true
            }
          }
        ],
        "@_": {
          "k_ele": true
        }
      },
      {
        "KEB": {
          "#text": "喰べる",
          "@_": {
            "keb": true
          }
        },
        "KE_INF": {
          "#text": "&iK;",
          "@_": {
            "ke_inf": true
          }
        },
        "@_": {
          "k_ele": true
        }
      }
    ],
    "R_ELE": {
      "REB": {
        "#text": "たべる",
        "@_": {
          "reb": true
        }
      },
      "RE_PRI": [
        {
          "#text": "ichi1",
          "@_": {
            "re_pri": true
          }
        },
        {
          "#text": "news2",
          "@_": {
            "re_pri": true
          }
        },
        {
          "#text": "nf25",
          "@_": {
            "re_pri": true
          }
        }
      ],
      "@_": {
        "r_ele": true
      }
    },
    "@_": {
      "entry": true
    }
  }
}

expected data

{
  "ENTRY": {
    "ENT_SEQ": 1358280,
    "K_ELE": [
      {
        "KEB": "食べる",
        "KE_PRI": [
          "ichi1",
          "news2",
          "nf25"
        ]
      },
      {
        "KEB": "喰べる",
        "KE_INF": "&iK;"
      }
    ],
    "R_ELE": {
      "REB": "たべる",
      "RE_PRI": [
        "ichi1",
        "news2",
        "nf25"
      ]
    }
  }
}

Would you like to work on this issue?

  • Yes
  • No

Bookmark this repository for further updates.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions