Skip to content

Functionality to imitate function json-to-xml of XPath 3.1 #258

@sjehuda

Description

@sjehuda

Is your feature request related to a problem? Please describe.

This is a request to comply with the function "json-to-xml" of XPath 3.1 version.

https://w3.org/TR/xpath-functions-31/#func-json-to-xml

Describe the solution

I have created this code to create XML files which comply with the function "json-to-xml" of XPath 3.1.

https://git.xmpp-it.net/sch/Focus/commit/5c3fdf9dfd4ee0c8c3ded9138240e88ea470bb82

Additional context

python xpath-functions-31-json-to-xml.py ; cat file.json ; xmllint file.xml --format
{
        "content": [
          {
            "id": 70805774,
            "value": "1001",
            "position": [1004.0,288.0,1050.0,324.0]
          }
        ]
}
<?xml version="1.0"?>
<map>
  <array key="content">
    <map>
      <number key="id">70805774</number>
      <string key="value">1001</string>
      <array key="position">
        <number>1004.0</number>
        <number>288.0</number>
        <number>1050.0</number>
        <number>324.0</number>
      </array>
    </map>
  </array>
</map>

There is more to add, such as escaped="true".

The expression json-to-xml('{"x": "\\", "y": "\u0025"}', map{'escape': true()}) returns (with whitespace added for legibility):

<map xmlns="http://www.w3.org/2005/xpath-functions">
  <string escaped="true" key="x">\\</string>
  <string key="y">%</string>
</map>

Demonstration (XSLT Fiddle):

https://martin-honnen.github.io/xslt3fiddle/?xslt=%3C%3Fxml+version%3D%221.0%22%3F%3E%0D%0A%3Cxsl%3Astylesheet%0D%0A%09xmlns%3Axsl%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2FXSL%2FTransform%22%0D%0A%09xmlns%3Amath%3D%22http%3A%2F%2Fwww.w3.org%2F2005%2Fxpath-functions%2Fmath%22%0D%0A%09xmlns%3Axs%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema%22%0D%0A%09exclude-result-prefixes%3D%22xs+math%22+version%3D%223.0%22%3E%0D%0A%09%3Cxsl%3Aoutput+indent%3D%22yes%22+omit-xml-declaration%3D%22yes%22+%2F%3E%0D%0A%0D%0A%09%3Cxsl%3Atemplate+match%3D%22data%22%3E%0D%0A%09++++%3Cxsl%3Acopy-of+select%3D%22json-to-xml%28.%29%22%2F%3E%0D%0A%09%3C%2Fxsl%3Atemplate%3E%0D%0A%3C%2Fxsl%3Astylesheet%3E&input=%3Cdata%3E%7B%0D%0A%09%22content%22%3A+%5B%0D%0A%09++%7B%0D%0A%09++++%22id%22%3A+70805774%2C%0D%0A%09++++%22value%22%3A+%221001%22%2C%0D%0A%09++++%22position%22%3A+%5B1004.0%2C288.0%2C1050.0%2C324.0%5D%0D%0A%09++%7D%0D%0A%09%5D%0D%0A%7D%3C%2Fdata%3E&input-type=XML

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions