【问题标题】:how to make specific node as list in xslt and i want output in json如何在 xslt 中将特定节点作为列表,我想在 json 中输出
【发布时间】:2017-12-22 12:25:55
【问题描述】:

我有 xml,我希望使用 xslt 将其输出为 json。 我找到了它的solution,但我需要一些我无法做到的更改,因为我是 xslt 的新手。在这个解决方案中,我想要一些嵌套标签作为 json 中的列表,但是这个解决方案将 xml 中的所有嵌套标签作为我不想要的 json 中的列表提供。要点是我想删除额外的[]。 请帮帮我

我的xml是

<?xml version="1.0" encoding="UTF-8"?><User>
<MaritalStatus>Single</MaritalStatus>
<Name>
    <FirstName>abc</FirstName>
    <MiddleName>def</MiddleName>
    <LastName>ghi</LastName>
</Name>
<Relative>
    <Father>
        <Name>
            <FirstName>abc</FirstName>
            <MiddleName>def</MiddleName>
            <LastName>ghi</LastName>
        </Name>
    </Father>
    <Mother>
        <Name>
            <FirstName>abc</FirstName>
            <MiddleName>def</MiddleName>
            <LastName>ghi</LastName>
        </Name>
    </Mother>
    <Sibling>
        <Name>
            <FirstName>abc</FirstName>
            <MiddleName>def</MiddleName>
            <LastName>ghi</LastName>
        </Name>

    </Sibling>
</Relative>
<Friend>
    <Name>
        <FirstName>abc</FirstName>
        <MiddleName>def</MiddleName>
        <LastName>ghi</LastName>
    </Name>
</Friend></User>

我提到的给我的解决方案是给我 json

[{
"MaritalStatus": "Single",
"Name": [
  {
    "FirstName": "abc",
    "MiddleName": "def",
    "LastName": "ghi"
  }
],
"Relative": [
  {
    "Father": [
      {
        "Name": [
          {
            "FirstName": "abc",
            "MiddleName": "def",
            "LastName": "ghi"
          }
        ]
      }
    ],
    "Mother": [
      {
        "Name": [
          {
            "FirstName": "abc",
            "MiddleName": "def",
            "LastName": "ghi"
          }
        ]
      }
    ],
    "Sibling": [
      {
        "Name": [
          {
            "FirstName": "ewqrew",
            "MiddleName": "defasdfadsf",
            "LastName": "ghiqwerwqer"
          }
        ]
      }
    ]
  }
],
"Friend": [
  {
    "Name": [
      {
        "FirstName": "asd",
        "MiddleName": "ghd",
        "LastName": "rtu"
      }
    ]
  }
]}]

但我想要的输出是

{
"MaritalStatus": "Single",
"Name":
  {
    "FirstName": "abc",
    "MiddleName": "def",
    "LastName": "ghi"
  },
"Relative": [
  {
    "Father":
      {
        "Name":
          {
            "FirstName": "abc",
            "MiddleName": "def",
            "LastName": "ghi"
          }
      },
    "Mother":
      {
        "Name":
          {
            "FirstName": "abc",
            "MiddleName": "def",
            "LastName": "ghi"
          }
      },
    "Sibling": [
      {
        "Name":
          {
            "FirstName": "ewqrew",
            "MiddleName": "defasdfadsf",
            "LastName": "ghiqwerwqer"
          }
      }
    ]
  }
],
"Friend": [
  {
    "Name": 
      {
        "FirstName": "asd",
        "MiddleName": "ghd",
        "LastName": "rtu"
      }
  }
]}

【问题讨论】:

  • 请在使用时提交您自己的 XSLT。指向另一个问题/解决方案没有帮助。
  • [stackoverflow.com/questions/30450607/…我已经尝试过这个链接的解决方案。但我想要上面提到的输出

标签: json xml xslt xslt-1.0


【解决方案1】:

使用 Saxon Home 您可以利用 XPath 3.1 函数fn:xml-to-json。我将包含一个非常简单的解决方案,适合理解这个问题。

下载 Saxon Home 并在下面创建 xslt (test.xsl)。运行

java -jar Saxon-HE-9.7.0-7.jar -s:<your-xml-file> -xsl:test.xsl

(将 Saxon jar 文件名替换为您下载的版本)

test.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="text" encoding="UTF-8" indent="yes" />

    <!-- Naive approach -->
    <xsl:template match="/">
        <xsl:variable name="xmljson">
            <xsl:apply-templates />
        </xsl:variable>
        <xsl:value-of select="fn:xml-to-json($xmljson)" />
    </xsl:template>

    <xsl:template match="User">
        <fn:map>
            <xsl:apply-templates />
        </fn:map>
    </xsl:template>

    <xsl:template match="MaritalStatus">
        <fn:string key="MaritalStatus">
            <xsl:value-of select="." />
        </fn:string>
    </xsl:template>

    <xsl:template match="Name">
        <fn:map key="Name">
            <xsl:apply-templates />
        </fn:map>
    </xsl:template>

    <xsl:template match="FirstName">
        <fn:string key="FirstName">
            <xsl:value-of select="." />
        </fn:string>
    </xsl:template>

    <!-- TODO (naive approach)-->
    <xsl:template match="Relative" />
    <xsl:template match="Friend" />
    <xsl:template match="MiddleName" />
    <xsl:template match="LastName" />

    <!-- TODO better way: fiddle with copy local-name() and more -->

</xsl:stylesheet>

Read about fn:xml-to-json

祝你好运!

【讨论】:

  • 有没有通用的解决方案?我定制了那个解决方案或任何指导方针????
  • 也许我不明白您所说的“通用解决方案”是什么意思。 fn:xml-to-json 是通用的。鉴于您最初的问题和 XML 示例,您不需要任何复杂的通用样式表。 “少数班轮”就可以了。您想要一个将任意复杂 XML(属性、数据类型、cmets 等)输入转换为 JSON 的 XSLT 样式表吗?我不知道这是否存在或者它是否可能/有意义(从未搜索过,也从未想过这一点)。也许您需要类似 [jsonml.org/].请详细说明您的问题,以反映您的所有需求和技能。这有助于帮助您。
猜你喜欢
  • 2014-12-27
  • 1970-01-01
  • 2014-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 1970-01-01
相关资源
最近更新 更多