Code Monkey home page Code Monkey logo

Comments (14)

nashwaan avatar nashwaan commented on May 30, 2024 1

@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.

nashwaan avatar nashwaan commented on May 30, 2024 1

Ooops.
I have published it now.

from xml-js.

nashwaan avatar nashwaan commented on May 30, 2024

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.

misitoth avatar misitoth commented on May 30, 2024

!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.

misitoth avatar misitoth commented on May 30, 2024

<?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.

nashwaan avatar nashwaan commented on May 30, 2024

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.

DenisCarriere avatar DenisCarriere commented on May 30, 2024

👍 +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 "&#xA0;">
<!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 "&#xA0;"'},
      {_comment: 'writer "Writer: Donald Duck."'},
      {_comment: 'copyright "Copyright: W3Schools."'},
    ]
  },
}
const xml = convert.js2xml(json, { compact: true })

CC: @misitoth @nashwaan Any thoughts?

from xml-js.

DenisCarriere avatar DenisCarriere commented on May 30, 2024

@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.

nashwaan avatar nashwaan commented on May 30, 2024

I have published v1.1.0 to support _doctype.

Please let me know whether this is good or not.

from xml-js.

DenisCarriere avatar DenisCarriere commented on May 30, 2024

Woot 🎉 Looks like only 1.0.2 is published.

https://www.npmjs.com/package/xml-js

from xml-js.

DenisCarriere avatar DenisCarriere commented on May 30, 2024

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.

nashwaan avatar nashwaan commented on May 30, 2024

@DenisCarriere Have implemented your suggestion and improved other things (see release notes). These are now published in v1.2.0.

from xml-js.

DenisCarriere avatar DenisCarriere commented on May 30, 2024

+1 awesome stuff

from xml-js.

nashwaan avatar nashwaan commented on May 30, 2024

@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)

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.