如何使用XPath/XSLT fn:json-to-xml [英] How to use XPath/XSLT fn:json-to-xml

查看:135
本文介绍了如何使用XPath/XSLT fn:json-to-xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将JSON字符串转换为XML字符串.标签确实包含属性.从主题中的答案开始使用XSLT.

存在一个函数 fn:json-to -xml .我知道它应该将JSON转换为不带属性的XML(我使用XSLT对其进行格式化).

如何使用此功能?

因为它是在XSLT中实现的,所以我会在.xsl文件中进行猜测,但是找不到任何示例.

非常感谢!

解决方案

这是从XSLT 3.0规范中摘录的简单示例

data元素的模板然后简单地调用json-to-xml(.)和整个样式表以演示结果输出返回的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>

Saxon 9.7 HE的输出为

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

因此,与将输入作为as xs:string的任何其他函数一样,您当然可以传入XSLT或XPath代码中具有的任何字符串值,也可以传入一个节点,然后将该节点首先原子化为字符串. /p>

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!

解决方案

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.

这篇关于如何使用XPath/XSLT fn:json-to-xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆