Comments (14)
@DenisCarriere , Thank you for your valuable feedback.
I don't think using _comment
for any DTD content is the best thing to do, because _comment
should be used for XML comments only (i.e <!-- some comment -->
). Also, comments can exists in DOCTYPE as well.
I still haven't studied DTD, but my impression is it requires a lot of parsing because of the scenarios it can support. Also, again from my shallow understanding, I think DTD influence how to parse rest of XML contents as it contains some instructions and variables (right?).
What I am planning to do is to put the content of DTD in _doctype
like I showed before:
{"_doctype": " svc_init SYSTEM \"MLP_SVC_INIT_300.DTD\" [<!ENTITY % extension SYSTEM \"PIF_EXTENSION_100.DTD\">%extension;]"
}
And if any needs further processing, then he can use DTD parser like https://github.com/calibr/dtd-file.
Or if you are satisfied with this parser or can recommend me a good parser then I can integrate it with this xml-js
library.
from xml-js.
Ooops.
I have published it now.
from xml-js.
Sorry, I don't understand the issue here properly.
The first line %extension;]>
in your example is not a proper xml format.
Also, do you need ignoreDoctype
to keep the <!DOCTYPE>
? What do you expect the output json to look like?
from xml-js.
!DOCTYPE is valid XML tag, but during the convert.xml2json call parser doesn't parse it and after the conversation we lost the information from that tag. Can you keep !DOCTYPE field in JSON and after json2xml? I propose ingoreDoctype in the options and parser can convert !DOCTYPE like ![CDATA].
Example xml was't visible in my first comment.
'
So the second line is doctype of MLP(Mobile Location Protocol), I would like to keep in the conversation.
My proposal in JSON (compact mode) is following:
{ "_doctype" : "scv_init SYSTEM MLP_SVC_INIT_300.DTD"}
from xml-js.
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svc_init SYSTEM "MLP_SVC_INIT_300.DTD" [<!ENTITY % extension SYSTEM "PIF_EXTENSION_100.DTD">%extension;]>
from xml-js.
Ok now I see the issue. Thanks for providing clear example.
I am working on adding support for !DOCTYPE. Currently, I get this output (not published):
{"_doctype": " svc_init SYSTEM \"MLP_SVC_INIT_300.DTD\" [<!ENTITY % extension SYSTEM \"PIF_EXTENSION_100.DTD\">%extension;]"
}
As you can see, ENTITY is not parsed.
I prefer to produce more meaningful result. Something like:
{"_doctype": {
"name_": "svc_init SYSTEM \"MLP_SVC_INIT_300.DTD\"",
"extension" : "SYSTEM \"PIF_EXTENSION_100.DTD\""
}
}
It seems the content of DOCTYPE is DTD which I am not very familiar with. I need to find time to study it in order to generate meaningful output.
from xml-js.
👍 +1 This issue, I've ran into the same problem.
@nashwaan your proposal by adding _doctype
would solve this, except an easier solution would be to extend it using the _comment
property since it's the closest one to an inner text without adding any new properties to xml-js
.
DOCTYPE (proposed)
Here's my XML example I would like to convert from JSON to XML.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Note.dtd">
const json = {
_declaration: {_attributes: { version: '1.0', encoding: 'utf-8' }},
_doctype: {
_comment: 'note SYSTEM "Note.dtd"'
},
}
const xml = convert.js2xml(json, { compact: true })
DOCTYPE + ENTITY (proposed)
There is also !ENTITY
in !DOCTYPE
which is described here: https://www.w3schools.com/xml/xml_dtd.asp. Might need to include an extra params (_entity
) to support that as well. Entities are typically (always?) used in a list.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ENTITY nbsp " ">
<!ENTITY writer "Writer: Donald Duck.">
<!ENTITY copyright "Copyright: W3Schools.">
]>
const json = {
_declaration: {_attributes: { version: '1.0', encoding: 'utf-8' }},
_doctype: {
_comment: 'note',
_entity: [
{_comment: 'nbsp " "'},
{_comment: 'writer "Writer: Donald Duck."'},
{_comment: 'copyright "Copyright: W3Schools."'},
]
},
}
const xml = convert.js2xml(json, { compact: true })
CC: @misitoth @nashwaan Any thoughts?
from xml-js.
@nashwaan Awesome stuff, agreed, maybe the _comment
is out of place and trying to solve the DTD implementation might be too complex for what it's worth.
👍 I like the idea of having a simple _doctype
property, I think that solves 99% of the issue.
The JS example would be even easier:
const json = {
_declaration: {_attributes: { version: '1.0', encoding: 'utf-8' }},
_doctype: 'note SYSTEM "Note.dtd"'
}
const xml = convert.js2xml(json, { compact: true })
Equals
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Note.dtd">
from xml-js.
I have published v1.1.0 to support _doctype
.
Please let me know whether this is good or not.
from xml-js.
Woot 🎉 Looks like only 1.0.2
is published.
https://www.npmjs.com/package/xml-js
from xml-js.
Woohoo! Works like a charm.
const json = {
_declaration: {_attributes: { version: '1.0', encoding: 'utf-8' }},
ServiceExceptionReport: {
_attributes: {version: '1.1.1'},
_doctype: ' ServiceExceptionReport SYSTEM "http://schemas.opengis.net/wms/1.1.1/exception_1_1_1.dtd"',
ServiceException: {
_text: message
}
}
}
const xml = convert.js2xml(json, { compact: true, spaces: spaces })
Results
<?xml version="1.0" encoding="utf-8"?>
<ServiceExceptionReport version="1.1.1">
<!DOCTYPE ServiceExceptionReport SYSTEM "http://schemas.opengis.net/wms/1.1.1/exception_1_1_1.dtd">
<ServiceException>foo</ServiceException>
</ServiceExceptionReport>
Only odd thing I've noticed is you need to include a blank space in your text field, if not it immediately places the text right after <!DOCTYPE
(don't know if that was intentional or not).
6a6090e#commitcomment-21926878
from xml-js.
@DenisCarriere Have implemented your suggestion and improved other things (see release notes). These are now published in v1.2.0.
from xml-js.
+1 awesome stuff
from xml-js.
@misitoth What do you think of the new approach published in the latest version?
Do you have any comment or feedback before I close this issue?
from xml-js.
Related Issues (20)
- how to use createReadStream instead of readFileSync
- are we able to manipulate none compact JS like compact JS? HOT 1
- Nested XML HOT 2
- use it in Vite will error HOT 1
- 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 <
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.