Comments (9)
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.
from xml-js.
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.
from xml-js.
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.
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.
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.
Any update on this?
{resolveNamespace: true}
sounds great!
from xml-js.
Close to 6 years later and the XML library with still no namespace support ...
from xml-js.
Related Issues (20)
- Wrong Handling of Whitespaces & Line breaks HOT 1
- "'" being changed to single quote HOT 2
- How to traverse through every tag
- `attributeNameFn` callback receives an undefined attributeValue
- CRLF line endings cause scripts failures HOT 1
- _attributes gets added to _attributes?
- Empty String of nativeTypeAttributes converted zero HOT 3
- Removing all _cdata _attributes text
- XML-JS Empty xml files being made after conversion from .json file HOT 1
- Typescript: `_comment` does not accept string[]
- attributes escape character?
- Consider type "string" as a default for all the values
- Analysis: 100% of dependency updates in this repository can be merged. HOT 1
- XML > JSON: How do I 'intercept' processing of a specific node?
- JSON -> XML convert with CDATA support
- json to xml: Auto convert < to <
- return parsed object instead of string ? HOT 1
- No nativeTypeAttributes in index.d.ts
- Assignment I was given,kindly give me both the XML code to build the app interface and the java code to execute it
- Module not found: Error: Can't resolve 'buffer'
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xml-js.