How to use XPath/XSLT fn:json-to-xml

2019-08-01 05:10发布

I need to convert a JSON string to a XML string. The tags do contain attributes. From the answer in this topic I started using XSLT.

There exists a function fn:json-to-xml. I understand that it should convert the JSON into an XML without attributes(which I format using XSLT).

How do I use this function?

Because it's implemented in XSLT I would guess in the .xsl file, but I can't find any examples.

Many thanks in advance!

2条回答
对你真心纯属浪费
2楼-- · 2019-08-01 05:51

The function is defined in XSLT 3.0, in XPath 3.1, and in XQuery 3.1.

The simplest way to use it is probably to install Saxon-HE 9.7.0.7 and then run it from XQuery on the command line like this:

java -cp /dddd/9.7.0.7/he/saxon9he.jar net.sf.saxon.Query -t -qs:"json-to-xml(unparsed-text('/eeee/test.json'))" -o:output.xml
查看更多
你好瞎i
3楼-- · 2019-08-01 05:54

Here is a simple example taken from the XSLT 3.0 spec https://www.w3.org/TR/xslt-30/#func-json-to-xml, the sample input can be any string in the JSON format, below I use an XML document containing a data element with JSON:

<root>
    <data>{
        "desc"    : "Distances between several cities, in kilometers.",
        "updated" : "2014-02-04T18:50:45",
        "uptodate": true,
        "author"  : null,
        "cities"  : {
        "Brussels": [
        {"to": "London",    "distance": 322},
        {"to": "Paris",     "distance": 265},
        {"to": "Amsterdam", "distance": 173}
        ],
        "London": [
        {"to": "Brussels",  "distance": 322},
        {"to": "Paris",     "distance": 344},
        {"to": "Amsterdam", "distance": 358}
        ],
        "Paris": [
        {"to": "Brussels",  "distance": 265},
        {"to": "London",    "distance": 344},
        {"to": "Amsterdam", "distance": 431}
        ],
        "Amsterdam": [
        {"to": "Brussels",  "distance": 173},
        {"to": "London",    "distance": 358},
        {"to": "Paris",     "distance": 431}
        ]
        }
        }</data>
</root>

The template for the data element then simply calls json-to-xml(.) and the whole stylesheet to demonstrate the result outputs the returned XML:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="xs math"
    version="3.0">

    <xsl:output indent="yes"/>

    <xsl:template match="data">
        <xsl:copy-of select="json-to-xml(.)"/>
    </xsl:template>

</xsl:stylesheet>

The output with Saxon 9.7 HE is

        <map xmlns="http://www.w3.org/2005/xpath-functions">
   <string key="desc">Distances between several cities, in kilometers.</string>
   <string key="updated">2014-02-04T18:50:45</string>
   <boolean key="uptodate">true</boolean>
   <null key="author"/>
   <map key="cities">
      <array key="Brussels">
         <map>
            <string key="to">London</string>
            <number key="distance">322</number>
         </map>
         <map>
            <string key="to">Paris</string>
            <number key="distance">265</number>
         </map>
         <map>
            <string key="to">Amsterdam</string>
            <number key="distance">173</number>
         </map>
      </array>
      <array key="London">
         <map>
            <string key="to">Brussels</string>
            <number key="distance">322</number>
         </map>
         <map>
            <string key="to">Paris</string>
            <number key="distance">344</number>
         </map>
         <map>
            <string key="to">Amsterdam</string>
            <number key="distance">358</number>
         </map>
      </array>
      <array key="Paris">
         <map>
            <string key="to">Brussels</string>
            <number key="distance">265</number>
         </map>
         <map>
            <string key="to">London</string>
            <number key="distance">344</number>
         </map>
         <map>
            <string key="to">Amsterdam</string>
            <number key="distance">431</number>
         </map>
      </array>
      <array key="Amsterdam">
         <map>
            <string key="to">Brussels</string>
            <number key="distance">173</number>
         </map>
         <map>
            <string key="to">London</string>
            <number key="distance">358</number>
         </map>
         <map>
            <string key="to">Paris</string>
            <number key="distance">431</number>
         </map>
      </array>
   </map>
</map>

So as with any other function taking an input as as xs:string you can of course pass in any string value you have in your XSLT or XPath code or you can pass in a node which is then atomized to a string first.

查看更多
登录 后发表回答