[go: nahoru, domu]

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

Processing Instructions are ignored #17

Closed
Arlen22 opened this issue May 18, 2017 · 9 comments
Closed

Processing Instructions are ignored #17

Arlen22 opened this issue May 18, 2017 · 9 comments

Comments

@Arlen22
Copy link
Contributor
Arlen22 commented May 18, 2017

I have an XML document that includes a processing instruction. But when I run it through xml2js, the processing instruction gets left behind.

<?xml version="1.0"?>
<?myapi version="13.0"?>
@Arlen22
Copy link
Contributor Author
Arlen22 commented May 18, 2017

The relevant line is here: https://github.com/nashwaan/xml-js/blob/master/lib/xml2js.js#L82

@Arlen22
Copy link
Contributor Author
Arlen22 commented May 18, 2017

And here is a quick modification that I threw together in xml2js.js

function onDeclaration(declaration) {
    if (options.ignoreDeclaration) {
        return;
    }
    var key = options.declarationKey;
    var type = "decl";
    if (currentElement[key]) {
        key = options.processingKey;
        currentElement[key] = [];
        type = "proc";
    } else {
        currentElement[key] = {};
    }
    var item = {};
    item[options.nameKey] = declaration.name;
    while (declaration.body) {
        var attribute = declaration.body.match(/([\w:-]+)\s*=\s*"([^"]*)"|'([^']*)'|(\w+)\s*/);
        if (!attribute) {
            break;
        }
        if (!item[options.attributesKey]) {
            item[options.attributesKey] = {};
        }
        item[options.attributesKey][attribute[1]] = attribute[2];
        declaration.body = declaration.body.slice(attribute[0].length); // advance the string
    }
    if (type === "proc") currentElement[key].push(item);
    else currentElement[key] = item;
    if (options.addParent) {
        currentElement[options.declarationKey][options.parentKey] = currentElement;
    }
}

@nashwaan
Copy link
Owner

Hi @Arlen22 , thanks for reporting this issue. Can you provide more examples and the expected output, please?

@nashwaan
Copy link
Owner
nashwaan commented May 21, 2017

@Arlen22
XML specifies that processing instruction contains just text not necessarily attributes.

This means both of the following are valid Processing Instructions:

<?stylesheet some random text?>

Output json will be (in compact-form):

{"_instruction": {"stylesheet": "some random text"}}

and

<?stylesheet href="style.css"?>

Output json will be (in compact-form):

{"_instruction": {"stylesheet": "href=\"style.css\""}}

Note that "_attributes" will be not generated.

@Arlen22
Copy link
Contributor Author
Arlen22 commented May 22, 2017

OK, well I guess that could work. It would be nice if the parser could parse any valid attributes found, and also include the text, since it has the capability built in. Or is that not the intent of this library? Could it be added as a helper method?

Could we do "stylesheet": { _attributes: {}, _value: "" }?

But what you wrote above will be enough if you don't want to add this. Because then at least it will not get lost.

@nashwaan
Copy link
Owner
nashwaan commented May 23, 2017

@Arlen22

"stylesheet": { "_attributes": {}, "_value": "" }}

is a good solution, but I am thinking of adding an option like {instructionHasAttributes: true} which will parse content of the Processing Instruction as attributes. This mean the output will be:

{"_instruction": {"stylesheet": {"href": "style.css"}}}
{"_instruction": {"stylesheet": {"_attributes": {"href": "style.css"}}}}   // or this?

otherwise if the flag is not set, then we get this:

{"_instruction": {"stylesheet": "href=\"style.css\""}}

@Arlen22
Copy link
Contributor Author
Arlen22 commented May 23, 2017 via email

@nashwaan
Copy link
Owner

Published v1.3.2 which supports processing instruction and {instructionHasAttributes: true} flag.

converting this xml:

<?go to="there"?>

will produce in compact mode:

{"_instruction":{"go":"to=\"there\""}}

in non-compact (expanded) mode:

{"elements":[{"type":"instruction", "name":"go", "instruction":"to=\"there\""}]}

and when {instructionHasAttributes: true} flag is set, it will produce the following in compact mode:

{"_instruction":{"go":{"_attributes":{"to":"there"}}}}

and this in non-compact (expanded) mode:

{"elements":[{"type":"instruction", "name":"go", "attributes":{"to":"there"}}]}

@Arlen22 Please let me know if this works for you or not.

@Arlen22
Copy link
Contributor Author
Arlen22 commented Jun 3, 2017

Seems to be working well. Thank you :)

@Arlen22 Arlen22 closed this as completed Jun 3, 2017
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

No branches or pull requests

2 participants