【问题标题】:Translating this XML to JSON using XSLT使用 XSLT 将此 XML 转换为 JSON
【发布时间】:2013-08-19 21:32:30
【问题描述】:

我正在尝试使用 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 哪里出错了?

【问题讨论】:

    标签: xml json xslt xpath


    【解决方案1】:

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

    这个:&lt;xsl:value-of select="@foo"&gt;

    应该是:&lt;xsl:value-of select="@foo"/&gt;

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

        {
        "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()

    【讨论】:

    • 太棒了。不知道能造成这么大的伤害。我希望在这种情况下会出现解析器错误。不错的收获,丹尼尔。谢谢。
    • 最后有什么办法可以去掉多余的逗号。即您发布的输出的第 22 行中的 } 之后和第 24 行中的 ] 之后的逗号。由于这些额外的逗号,我收到 JSON eval 错误。
    • 您的问题似乎在这里得到了回答:stackoverflow.com/questions/20658307/…
    猜你喜欢
    • 2020-01-26
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 2018-12-04
    • 2012-10-12
    • 2021-07-22
    相关资源
    最近更新 更多