oozcitak / xmlbuilder-js

An XML builder for node.js
MIT License
919 stars 108 forks source link

Add element before root not working through instruction #226

Closed Code-Crash closed 4 years ago

Code-Crash commented 5 years ago

Hi Team,

I'm using this library for more than 2 years for my personal project, suddenly I found an issue in the new version('13.0.2') for creating an instruction before the root element.

Earlier, I was using '2.6.4' and it was working fine.

kindly go through the following example:

The codebase is the same as always:

var qbXML = xmlBuilder.create('QBXML', {
    version: '1.0',
    encoding: 'utf-8'
}).instruction('qbxml', 'version="13.0"');

let qbXMLMsgsRq = qbXML.e('QBXMLMsgsRq').a('onError', 'continueOnError');
strRequestXML = qbXML.end({
    'pretty': true
});

// And more code for the same root element

in version 2.6.4 I was getting the output XML as below:

<?xml version="1.0" encoding="utf-8"?>
<?qbxml version="13.0"?>
<QBXML>
  <QBXMLMsgsRq onError="continueOnError">
    <CustomerQueryRq >

in version 13.0.2, I was getting the output XML as below:

<?xml version="1.0" encoding="utf-8"?>
<QBXML>
<?qbxml version="13.0"?>
  <QBXMLMsgsRq onError="continueOnError">
    <CustomerQueryRq >

Please see the difference for <?qbxml version="13.0"?>, I want this after <?xml version="1.0" encoding="utf-8"?> and before <QBXML>.

It is working fine when I did change the version back to '2.6.4'.

Thanks

oozcitak commented 5 years ago

This was introduced in v7.0. The ’instruction‘ function now inserts the processing instruction node as a child of its parent node. For the old behavior you need to use the ‘instructionBefore‘ function. Please see: https://github.com/oozcitak/xmlbuilder-js/wiki#processing-instructions

Code-Crash commented 5 years ago

@oozcitak, Thanks for the doc link and details, I will check it today.

Code-Crash commented 5 years ago

@oozcitak, Yes it worked. Thanks.

Can we also add one example in the doc?

If yes, I can create a small PR for the same?

oozcitak commented 5 years ago

@Code-Crash Yes sure. Thank you in advance.

Code-Crash commented 4 years ago

@oozcitak Okay, I will submit the PR in some time.

Code-Crash commented 4 years ago

Hi @oozcitak,

I cloned and created a new branch of wiki repo, but unable to push due to permission issue.

please add the following code as here to update the doc or If I get permission, I will do the same.

`

Processing instructions

XML processing instructions are created with the instruction function (can also be abbreviated to ins or i).

ele.ins('xml-stylesheet', 'type="text/xsl" href="style.xsl"');

There are also the instructionBefore and instructionAfter functions for processing instructions. They accept the same arguments as the instruction function.

ins, instructionBefore and instructionAfter will return their parent node.

using instructionBefore, will add an instruction before the root element.


var foo = xmlbuilder.create('Foo', {
    version: '1.0',
    encoding: 'utf-8'
}).instructionBefore('bar', 'version="13.0"');

var xml = foo.end({
    'pretty': true
});

// xml string output

<?xml version="1.0" encoding="utf-8"?>
<?bar version="13.0"?>
<Foo/>

using instruction, will add an instruction immediately inside after the root element.


var foo = xmlbuilder.create('Foo', {
    version: '1.0',
    encoding: 'utf-8'
}).instruction('bar', 'version="13.0"');

var xml = foo.end({
    'pretty': true
});

// xml string output

<?xml version="1.0" encoding="utf-8"?>
<Foo>
  <?bar version="13.0"?>
</Foo>

using instructionAfter, will add a instruction after the root element.


var foo = xmlbuilder.create('Foo', {
    version: '1.0',
    encoding: 'utf-8'
}).instructionAfter('bar', 'version="13.0"');

var xml = foo.end({
    'pretty': true
});

// xml string output

<?xml version="1.0" encoding="utf-8"?>
<Foo/>
<?bar version="13.0"?>

`

oozcitak commented 4 years ago

I changed the formatting a bit and added to the wiki: https://github.com/oozcitak/xmlbuilder-js/wiki/Home/_compare/4fc917165e0ffadeeba5b80bc148f2b3159c3bdf...6f85e2fe86b2e96818eb9dd71ec9c9f4c19503eb

Thank you

Code-Crash commented 4 years ago

@oozcitak Thank you too.