xml - 使用 XSLT 将此 XML 转换为 JSON

标签 xml json xslt xpath

我正在尝试使用 XSLT 将一些简单的 XML 转换为 JSON。

我的 XML 如下所示:

<some_xml>
<a>
 <b>
  <c foo="bar1">
    <listing n="1">a</listing>
    <listing n="2">b</listing>
    <listing n="3">c</listing>
    <listing n="4">d</listing>
  </c>
  <c foo="bar2">
    <listing n="1">e</listing>
    <listing n="2">b</listing>
    <listing n="3">n</listing>
    <listing n="4">d</listing>
  </c>
 </b>
</a>
</some_xml>

输出应该如下所示:

{
    "my_c": [
        {
            "c": {
                "foo_id": "bar1",
                "listing_1": "a",
                "listing_2": "b",
                "listing_3": "c",
                "listing_4": "d"

            }
        },
        {
            "c": {
                "foo_id": "bar2",
                "listing_1": "e",
                "listing_2": "b",
                "listing_3": "n",
                "listing_4": "d"
            }   
        }
    ],
}

我的 XSLT 试图让这个翻译工作:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" omit-xml-declaration="yes" />
    <xsl:template match="/some_xml">
    {
        "my_c": [
             <xsl:for-each select="a/b/c">
            {
              "c": {
                "foo_id": <xsl:value-of select="@foo">,
                "listing_1": <xsl:value-of select="current()/listing[@n='1']" />,
                "listing_2": <xsl:value-of select="current()/listing[@n='2']" />,
                "listing_3": <xsl:value-of select="current()/listing[@n='3']" />,
                "listing_4": <xsl:value-of select="current()/listing[@n='4']" />
              } 
            },
          </xsl:for-each>
        ], 
    }
    </xsl:template>
</xsl:stylesheet>

下面的错误输出是结果:

{
"my_c": [

            {
              "c": {
                "foo_id": "bar1"
        ],
      }
    }

            {
              "c": {
                "foo_id": "bar2"
        ],
      }
}

我的 XSLT 哪里出错了?

最佳答案

尝试正确关闭您的第一个 xsl:value-of .

这个:<xsl:value-of select="@foo">

应该是:<xsl:value-of select="@foo"/>

如果我改变它,我会得到这个输出(接近你想要的输出,但你还有一点工作要做):

    {
    "my_c": [

        {
        "c": {
        "foo_id": bar1,
            "listing_1": a,
            "listing_2": b,
            "listing_3": c,
            "listing_4": d
            } 
            },

        {
        "c": {
        "foo_id": bar2,
            "listing_1": e,
            "listing_2": b,
            "listing_3": n,
            "listing_4": d
            } 
            },

    ], 
    }

此外,您不需要 current() .

关于xml - 使用 XSLT 将此 XML 转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18323495/

相关文章:

python - 使用 Python 解析 JSON

PHP DOM : Get Nodevalue without descendant nodes

xml - 从哪里获取 Google 搜索 XSD

javascript - 如何在客户端从灵活的 xml 生成 html 中的 TreeView

java - 转换时出现异常 "JSONArray cannot be converted to JSONObject error"

xslt - XSL : Ignoring/stripping namespaces in secondary documents

android - 如何防止元素隐藏在透明操作栏后面

javascript - 与 javascript 日期相比,Json Stringify 日期产生错误的日​​期

xslt 在元素不存在的地方添加默认值

html - XSLT - 如何循环遍历 XML 重复/重复节点并获得第一次出现