Code Monkey home page Code Monkey logo

Comments (9)

nashwaan avatar nashwaan commented on May 31, 2024

I am not sure if I understand the issue correctly.
Are you facing the problem when converting from xml → json or from json → xml?
Or is it from xml→json→xml?

Or is the xml input changing sometimes so the elements get prepended with d: namespace?

I really would like to help you overcoming the issue you are facing, but can you clarify more whether this library is not doing conversion as per your expectation or you are looking for a new feature to be implemented to support your use case?

from xml-js.

ultimate-tester avatar ultimate-tester commented on May 31, 2024

from xml-js.

nashwaan avatar nashwaan commented on May 31, 2024

Here is my understanding of the problem:

sample1.xml:

<d:foo xmlns="DAV:">
    <bar>something</bar>
</d:foo>

converting sample1.xml using convert.xml2js(sample1, {compact: true}) results (using compact form for the sake of brevity):

{
  "d:foo": {
    "_attributes": {
      "xmlns": "DAV:"
    },
    "bar": {
      "_text": "something"
    }
  }
}

sample2.xml:

<d:foo xmlns:d="DAV:">
    <d:bar>something</d:bar>
</d:foo>

converting sample2.xml using convert.xml2js(sample2, {compact: true}) results:

{
  "d:foo": {
    "_attributes": {
      "d:xmlns": "DAV:"
    },
    "d:bar": {
      "_text": "something"
    }
  }
}

But, what you want is to have the following output for both sample1.xml and sample2.xml:

{
  "d:foo": {
    "_attributes": {
      "d:xmlns": "DAV:"
    },
    "DAV:bar": {
      "_text": "something"
    }
  }
}

If this is what you want, then a new option, say {resolveNamespace: true}, should be implemented to support this scenario. Is my understanding correct?

from xml-js.

ultimate-tester avatar ultimate-tester commented on May 31, 2024

from xml-js.

nashwaan avatar nashwaan commented on May 31, 2024

I certainly would accept pull requests, but not at the moment. Right now I am working on adding a major feature to the library: the ability to supply callback functions as additional processing to names, values, attributes, ...etc. Which can partly help to solve your problem like this:

function addPrefix(name) {
  return 'DAV:' + name.substring(name.indexOf(":") + 1);
}

convert.xml2js(sample1, {compact: true, elementNameFn: addPrefix})
convert.xml2js(sample2, {compact: true, elementNameFn: addPrefix})

But it will generate the following result (notice DAV:foo which is not what you want):

{
  "DAV:foo": {
    "_attributes": {
      "d:xmlns": "DAV:"
    },
    "DAV:bar": {
      "_text": "something"
    }
  }
}

This might not be a clean solution to deal with namespace, but I think it will help for other cases.

Just give me couple of days until I finish releasing it, and then you can attempt to add this feature {resolveNamespace: true} if you want. I think another complementary option {stripNamespace: true} is also needed to remove namespace prefix.

from xml-js.

ultimate-tester avatar ultimate-tester commented on May 31, 2024

Although I thank you for your effort, I guess we might run into an issue here for people with an other use case than mine.

I just looked at the spec (https://www.w3.org/TR/1999/REC-xml-names-19990114/#ns-decl), where it clearly reads that originally namespaces are bound to URI's. Resolving those namespaces (d: to an URI) wouldn't make sense and the element names will get very long (or is that no problem at all?)

Removing the namespaces also gives conflicts. It's perfectly acceptable to have an element of d:foo and also e:foo. They are both called foo but are from different namespaces making them effectively a different element. Removing the namespaces will result in the elements becoming of the same namespaces which will give trouble when you are converting back to XML (from XML -> JSON -> XML).

Lastly, to remember: The element in which a namespace is defined, can also prefix the element with that namespace and be valid, as the example in the spec shows:

<x xmlns:edi='http://ecommerce.org/schema'>
  <!-- the "edi" prefix is bound to http://ecommerce.org/schema
       for the "x" element and contents -->
</x>

Meaning the first line in the xml (the x element) could also have been edi:x instead of x, but that's something for later!

from xml-js.

nashwaan avatar nashwaan commented on May 31, 2024

Hi @ultimate-tester, sorry for late response. Just published v1.6.0 of this library which now supports adding custom functions for additional processing. Using elementNameFn should work now.

To solve namespace issue properly, we need to implement {resolveNamespace: true}. I am not familiar with namespace thing and I need to spend some time learning it. But if you are willing to contribute, then feel free to submit a pull request as I have completed adding Custom functions feature. I will try to accommodate your changes as soon as I can. Thanks.

from xml-js.

Lonzak avatar Lonzak commented on May 31, 2024

Any update on this?

{resolveNamespace: true}

sounds great!

from xml-js.

loganmzz avatar loganmzz commented on May 31, 2024

Close to 6 years later and the XML library with still no namespace support ...

from xml-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.