[使用XPath表达式的XML到JSON的转换

问题描述 投票:0回答:1

我必须使用XPath表达式将XML转换为JSON格式。有什么方法可以实现?

我尝试使用xml-to-json函数,但没有得到如何为该函数提供参数。

我只需要运行XPath表达式,就不能使用XSLT来实现相同的效果。

XML:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
    <book category="cooking">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
        <location>DL</location>
    </book>
    <book category="children">
        <title lang="es">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
        <location>UP</location>
    </book>
    <book category="web">
        <title lang="en">XQuery Kick Start</title>
        <author>James McGovern</author>
        <author>Per Bothner</author>
        <author>Kurt Cagle</author>
        <author>James Linn</author>
        <author>Vaidyanathan Nagarajan</author>
        <year>2003</year>
        <price>49.99</price>
        <location>  DL</location>
    </book>
    <book category="web">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
        <location>dl </location>
    </book>
    <category>web</category>
</bookstore>
xml xpath xpath-2.0
1个回答
0
投票

即使您使用的是3.1版,这实际上是在扩展XPath的功能。您确实应该使用XSLT或XQuery。

但是,我认为有可能-但不能使用xml-to-json()函数。您需要类似的东西

serialize (
  map { "bookstore": 
    array { //book !
      map { "category" : string(@category),
            "title" : string(title),
            "authors" : 
               array { author ! string(.) },
            "year" : number(year)
          }
    }
  },
  map("method" : "json")
)
© www.soinside.com 2019 - 2024. All rights reserved.