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

Allow to remove namespace prefixes from xml nodes #87

Closed
shadeglare opened this issue May 20, 2013 · 10 comments

Comments

@shadeglare
Copy link

commented May 20, 2013

Could you provide the ability to remove namespace prefixes from nodes.

For example:

Translate the xml below

<f:table xmlns:f="http://www.w3schools.com/furniture"> 
  <f:name>African Coffee Table</f:name>                
  <f:width>80</f:width>                                
  <f:length>120</f:length>                             
</f:table>

to a json like:

{
  "table": {
    "$": {
      "xmlns:f": "http://www.w3schools.com/furniture"
    },
    "name": "African Coffee Table",
    "width": "80",
    "length": "120"
  }
}

Now it looks like:

{
  "f:table": {
    "$": {
      "xmlns:f": "http://www.w3schools.com/furniture"
    },
    "f:name": "African Coffee Table",
    "f:width": "80",
    "f:length": "120"
  }
}
@Leonidas-from-XIV

This comment has been minimized.

Copy link
Owner

commented May 20, 2013

Well, it actually changes the tags, so I am not really thrilled about adding something like this, since f:name and name are completely different tags.

@shadeglare

This comment has been minimized.

Copy link
Author

commented May 20, 2013

Prefixes just refer to specific namespaces and they can be different for the same namespace. So it's not very easy to select elements after json convertation. For example:

<collection>

  <foo:item xmlns:foo="http://me.com/namespaces/foofoo">
    <foo:item>item1</foo:item>
  </foo:item>

  <bar:item xmlns:bar="http://me.com/namespaces/foofoo">
    <bar:item>item2</bar:item>
  </bar:item>

<collection>

The item tags are the same but in json it becomes hard to select these nodes. I think they must be placed in the same array.

The worst case is when a web service changes prefixes in its xml response so it becomes hard to maintain the code.

Would be nice to put namespace prefixes to a special field like you did with attributes.

@shadeglare

This comment has been minimized.

Copy link
Author

commented May 21, 2013

So could you provide this functionality?

@Leonidas-from-XIV

This comment has been minimized.

Copy link
Owner

commented May 21, 2013

You could write a patch and unittests, I'd include it if you like.

@MorganLindqvist

This comment has been minimized.

Copy link

commented Sep 21, 2013

Hi,

How do one access the value if the key uses an namespace? i.e. how to solve the access of the below data until the patch above is in place?

"African Coffee Table" below can be access using foo.name. But how do you access "80"? foo.f:width is not possible.

{
    "name": "African Coffee Table",
    "f:width": "80"
}
@Leonidas-from-XIV

This comment has been minimized.

Copy link
Owner

commented Sep 21, 2013

It's an ordinary JavaScript object, so you can just do foo["f:width"].

@wisotzky

This comment has been minimized.

Copy link

commented Apr 6, 2016

I am just running into the same issue. Looks like a bug to me. You never know, what prefix place-holders are used in a XML document. So it might be "f:table" in one document and "xyz:table" in another or just "table" in the next. So this makes parsing the JSON output very complex. The preferred method should be, to remove all xmlns prefixes from the JSON - but to add the individual resolved xmlns to individual elements.

To be not misunderstood - this is not really a bug. But you (as a programmer) need to understand how the XML document itself is rendered - and you must avoid any xmlns prefixes to be used. Using xmlns prefixes basically increases the scanning complexity so massively, that xml2js can not meaningful be used anymore. So it is what I would call a "usability bug".

@mike-feldmeier

This comment has been minimized.

Copy link

commented Jul 1, 2016

I realize this is an old issue, but since it's open, there is a recent comment, and Google led me here, I'll add to the discussion.

If you're just looking to strip the namespace prefixes from the document, as is useful in cases where only a single namespace is being used, then that is already supported through the use of the tagNameProcessors option and the stripPrefix method included in the processors module:

var parseString = require('xml2js').parseString;
var stripPrefix = require('xml2js').processors.stripPrefix;

parseString(xml, { tagNameProcessors: [ stripPrefix ] }, function(err, js) {
    if(err) throw err;
    console.dir(js, { depth: null });
});

Hope this helps someone

@jcsahnwaldt

This comment has been minimized.

Copy link
Contributor

commented Jun 15, 2018

I guess this can be closed. stripPrefix is mentioned in a short sentence in README.md, and while the last comment here by @mike-feldmeier is a great addition to that short sentence, the comment will probably still show up in Google results even when this issue is closed.

@munimanjunath

This comment has been minimized.

Copy link

commented Jan 2, 2019

When I defined the parser as var xml2jsParser = new xml2js.Parser({explicitArray : false});
above code is getting stuck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.