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 #589

Merged
merged 1 commit into from
Jul 22, 2023

Conversation

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>

@amitguptagwl amitguptagwl merged commit a04cd1e into NaturalIntelligence:master Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants