Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only set textNodeName, build xml is error #590

Open
joneqian opened this issue Jun 19, 2023 · 1 comment
Open

Only set textNodeName, build xml is error #590

joneqian opened this issue Jun 19, 2023 · 1 comment
Labels
Pending Pending to be confirmed by user/author for some check/update/implementation

Comments

@joneqian
Copy link
Contributor

Purpose / Goal

Only set textNodeName, and attributeNamePrefix is empty string, build result is error.

Example:

const schema_obj = {
      field: {
        values: {
          value: {
            '#text': 10061001,
            size: '5',
          },
        },
        id: 'skuCombineContent',
        type: 'multiInput',
      },
    };

    const parse_options = {
      ignoreAttributes: false,
      attributeNamePrefix: '',
      textNodeName: '#text',
    };
    const builder = new XMLBuilder(parse_options);
    const schema_xml = builder.build(schema_obj);

Type

Please mention the type of PR

  • [x]Bug Fix
  • [ ]Refactoring / Technology upgrade
  • [ ]New Feature

Test code:

it('xml build test', async () => {
    const schema_obj = {
      field: {
        values: {
          value: {
            '#text': 10061001,
            size: '5',
          },
        },
        id: 'skuCombineContent',
        name: 'skuProduct',
        type: 'multiInput',
      },
    };

    const parse_options = {
      ignoreAttributes: false,
      attributeNamePrefix: '',
      textNodeName: '#text',
    };
    const builder = new XMLBuilder(parse_options);
    const schema_xml = builder.build(schema_obj);
    console.log(schema_xml);
  });

The current code execution results:

<field id="skuCombineContent" name="skuProduct" type="multiInput"><values><value #text="10061001" size="5"></value></values></field>

Modify isAttribute in json2xml.js

function isAttribute(name /*, options*/) {
  if (name.startsWith(this.options.attributeNamePrefix) && name !== this.options.textNodeName) {
    return name.substr(this.attrPrefixLen);
  } else {
    return false;
  }
}

Get the accurate results:

 <field id="skuCombineContent" name="skuProduct" type="multiInput"><values><value size="5">10061001</value></values></field>
@github-actions
Copy link

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.

@amitguptagwl amitguptagwl added the Pending Pending to be confirmed by user/author for some check/update/implementation label Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pending Pending to be confirmed by user/author for some check/update/implementation
Projects
None yet
Development

No branches or pull requests

2 participants