【问题标题】:Convert Embedded JSON to XML using XSLT使用 XSLT 将嵌入式 JSON 转换为 XML
【发布时间】:2016-06-13 09:29:06
【问题描述】:

在使用 XSLT 转换 XML 文档时,是否可以在此过程中转换嵌入的 JSON(即 JSON 格式的内容)?

例如:-

<form>
    <data>[{"id":1,"name":"Hello"},{"id":2,"name":"World"}]</data>
</form>

将转换为:-

<form>
    <data>
        <id name="Hello">1</id>
        <id name="World">2</id>
    </data>
</form>

【问题讨论】:

    标签: json xml xslt


    【解决方案1】:

    XSLT 3.0 支持解析 JSON,因此您可以使用 Saxon 9.7 的商业版本

    <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:strip-space elements="*"/>
    
        <xsl:mode on-no-match="shallow-copy"/>
    
        <xsl:template match="data">
            <xsl:copy>
                <xsl:apply-templates select="parse-json(.)?*"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match=".[. instance of map(xs:string, item())]">
            <id name="{.?name}">
                <xsl:value-of select=".?id"/>
            </id>
        </xsl:template>
    
    </xsl:stylesheet>
    

    使用 Saxon 9.7 的开源版本(即 Saxon 9.7 HE),以下采纳了 wero 提出的使用 json-to-xml 的建议,并展示了如何实现该要求:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:fn="http://www.w3.org/2005/xpath-functions"
        xmlns:math="http://www.w3.org/2005/xpath-functions/math"
        exclude-result-prefixes="xs math fn"
        version="3.0">
    
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="data">
            <xsl:copy>
                <xsl:apply-templates select="json-to-xml(.)//fn:map"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="fn:map">
            <id name="{fn:string[@key = 'name']}">
                <xsl:value-of select="fn:number[@key = 'id']"/>
            </id>
        </xsl:template>
    
    </xsl:stylesheet>
    

    Saxon 9.7 HE 可在 Maven 和 http://saxon.sourceforge.net/ 获得

    【讨论】:

    • 谢谢,但我担心商业产品超出了我的项目范围。我希望有一个 XSLT 2.0 解决方案(Oracle 11gR2 数据库中支持的版本)或支持 XSLT 3.0 的替代方法通过导入 jar 或其他任何东西在 Oracle 数据库中。
    • @AlbPuado,我添加了一个适用于 Saxon 9.7 的开源 HE 版本的示例,它使用了另一个答案中已经建议的函数 json-to-xml。至于 Oracle 和 XSLT 2.0,恐怕我不熟悉它及其扩展功能等特性,如果您正在寻找该环境中的解决方案,请考虑用特定标签标记您的问题,也许比其他熟悉它的人可以告诉你是否有解析处理JSON的扩展函数。
    【解决方案2】:

    在 XSLT 3.0 中应该可以,因为它有一个 json-to-xml function:

    解析以 JSON 文本形式提供的字符串,返回 结果以 XML 文档节点的形式出现。

    您可以尝试使用Saxon 中的当前实现来运行它。

    【讨论】:

      猜你喜欢
      • 2020-01-26
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 2019-07-02
      • 2018-12-04
      相关资源
      最近更新 更多