XML + HTML to JSON
Thursday, 14 December 2017 · 3 messages
MG
Maximilian Gärber14 Dec 2017 16:16 #
Hi,
we've content similar to:
<info>
<name>Foo</name>
<xhtml>
<p>Some para </p>
</xhtml>
</info>How can I tell the JSON serializer to treat the content of <xhtml> as html?
Br,
Max
MS
Michael Seiferle14 Dec 2017 17:09 #
Hi Max,
I am not aware of any direct way, but you might consider the following:
https://gist.github.com/micheee/c5a55ca8b49435934c006cfc5df7ba0f
What it does:
1. Replace the contents of xhtml with its string serialization
2. Wrap the info node in an element named json, according to our own JSON serialization format
3. Serialize the contents as JSON.
…to go back:
4. Serialize the JSON as XML
5. Remove our JSON-Mapping Attributes
6. Replace the value of the element with its (parse:html() in case it is a valid document with a single root node) parse-xml-fragment() contents.
Hope this kind of helps ;)
Michael
--
Michael Seiferle, BaseX GmbH, http://www.basexgmbh.de
|-- Firmensitz: Obere Laube 73, 78462 Konstanz
|-- Registergericht Freiburg, HRB: 708285, Geschäftsführer:
| Dr. Christian Grün, Dr. Alexander Holupirek, Michael Seiferle `-- Tel: +49 7531 916 82 77
Quoted text · Maximilian Gärber · 19 lines
> Am 14.12.2017 um 16:16 schrieb Maximilian Gärber <mgaerber at arcor.de>: > > Hi, > > we've content similar to: > > <info> > <name>Foo</name> > <xhtml> > <p>Some para </p> > </xhtml> > </info> > > How can I tell the JSON serializer to treat the content of <xhtml> as html? > > > Br, > > Max
MG
Maximilian Gärber14 Dec 2017 17:18 #
Hi,
yes, thanks!
I guess this would be a rather common case when you try to call JSON APIs. So a type declaration for the serializer might be useful.
Br,
Max
2017-12-14 17:09 GMT+01:00 Michael Seiferle <ms at basex.org>:
Quoted text · Michael Seiferle · 53 lines
> Hi Max, > > I am not aware of any direct way, but you might consider the following: > > https://gist.github.com/micheee/c5a55ca8b49435934c006cfc5df7ba0f > > > What it does: > > 1. Replace the contents of xhtml with its string serialization > 2. Wrap the info node in an element named json, according to our own JSON > serialization format > 3. Serialize the contents as JSON. > …to go back: > 4. Serialize the JSON as XML > 5. Remove our JSON-Mapping Attributes > 6. Replace the value of the element with its (parse:html() in case it is a > valid document with a single root node) parse-xml-fragment() contents. > > > Hope this kind of helps ;) > > Michael > > > -- > Michael Seiferle, BaseX GmbH, http://www.basexgmbh.de > |-- Firmensitz: Obere Laube 73, 78462 Konstanz > |-- Registergericht Freiburg, HRB: 708285, Geschäftsführer: > | Dr. Christian Grün, Dr. Alexander Holupirek, Michael Seiferle > `-- Tel: +49 7531 916 82 77 > > Am 14.12.2017 um 16:16 schrieb Maximilian Gärber <mgaerber at arcor.de>: > > Hi, > > we've content similar to: > > <info> > <name>Foo</name> > <xhtml> > <p>Some para </p> > </xhtml> > </info> > > How can I tell the JSON serializer to treat the content of <xhtml> as html? > > > Br, > > Max > >
Written for BaseX 8.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery