【问题标题】:xsl to convert xml to jsonxsl 将 xml 转换为 json
【发布时间】:2014-07-30 03:35:05
【问题描述】:

输入 XML

<Root>
    <Result>
        <System>
            <Name>ABC</Name>
            <ID pname="PAD">
                <value>4567</value>
            </ID>
            <lastTime>2013-11-06T17:36:46.000-05:00</lastTime>
        </System>
        <line>Metals</line>
    </Result>
    <Result>
        <System>
            <Name>CAYS</Name>
            <ID pname="PAD">
                <value>MCIERT</value>
            </ID>
            <ID pname="ATPAD">
                <value>56412</value>
            </ID>
            <lastTime>2013-12-06T16:43:36.000-05:00</lastTime>
        </System>
        <System>
            <Name>CAYS</Name>
            <ID pname="CAD">
                <value>DGSG</value>
            </ID>
            <ID pname="ARCAD">
                <value>2847114</value>
            </ID>
            <lastTime>2013-12-07T20:02:38.000-05:00</lastTime>
        </System>
        <line>Minerals</line>
    </Result>
</Root>

输出Json

{
"Root": {
"Result": [
  {
    "System": {
      "Name": "ABC",
      "ID": {
        "pname": "PAD",
        "value": "4567"
      },
      "lastTime": "2013-11-06T17:36:46.000-05:00"
    },
    "line": "Metals"
  },
  {
    "System": [
      {
        "Name": "CAYS",
        "ID": [
          {
            "pname": "PAD",
            "value": "MCIERT"
          },
          {
            "pname": "ATPAD",
            "value": "56412"
          }
        ],
        "lastTime": "2013-12-06T16:43:36.000-05:00"
      },
      {
        "Name": "CAYS",
        "ID": [
          {
            "pname": "CAD",
            "value": "DGSG"
          },
          {
            "pname": "ARCAD",
            "value": "2847114"
          }
        ],
        "lastTime": "2013-12-07T20:02:38.000-05:00"
      }
    ],
    "line": "Minerals"
  }
]
}
}

如何编写将输入 xml 转换为 json 的通用 xslt 样式表

输入可能在根目录下有很多结果,在结果下有系统和名称,在系统下还有 ID 名称和值。

【问题讨论】:

    标签: xml json xslt


    【解决方案1】:

    我从here 复制和粘贴的以下 XSLT 应该可以帮助您将 XML 转换为 JSON。谢谢:)

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    
    <xsl:template match="/">{
        <xsl:apply-templates select="*"/>}
    </xsl:template>
    
    <!-- Object or Element Property-->
    <xsl:template match="*">
        "<xsl:value-of select="name()"/>" :<xsl:call-template name="Properties">
            <xsl:with-param name="parent" select="'Yes'"> </xsl:with-param>
        </xsl:call-template>
    </xsl:template>
    
    <!-- Array Element -->
    <xsl:template match="*" mode="ArrayElement">
        <xsl:call-template name="Properties"/>
    </xsl:template>
    
    <!-- Object Properties -->
    <xsl:template name="Properties">
        <xsl:param name="parent"></xsl:param>
        <xsl:variable name="childName" select="name(*[1])"/>
        <xsl:choose>            
            <xsl:when test="not(*|@*)"><xsl:choose><xsl:when test="$parent='Yes'"> <xsl:text>&quot;</xsl:text><xsl:value-of select="."/><xsl:text>&quot;</xsl:text></xsl:when>
                    <xsl:otherwise>"<xsl:value-of select="name()"/>":"<xsl:value-of  select="."/>"</xsl:otherwise>
                </xsl:choose>           
            </xsl:when>                
            <xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of  select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when>
            <xsl:otherwise>{
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates select="*"/>
                }</xsl:otherwise>
        </xsl:choose>
        <xsl:if test="following-sibling::*">,</xsl:if>
    </xsl:template>
    
    <!-- Attribute Property -->
    <xsl:template match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
    </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • 请在将 XML 转换为 JSON 后使用一些 JSON 验证选项。
    • 一切正常。但最后它没有给出矿物的元素名称},"Minerals"] }] }}
    • 我正在检查这个
    • 非常有用,但如果 XML 节点内的字符串有双引号,则会给出无效的 JSON。
    【解决方案2】:

    @sancelot 发表了一条评论,其中包含一个 github 样式表的链接,我在回答的实例旁边进行了测试(我赞成)。此处提供更多详细信息以显示以下示例。

    示例 XML:

    <avpList>
      <eComStringAttributeValuePairList attributeName="orderTypeCode" qualifierCodeName="order" qualifierCodeList="OrderTypeCode" qualifierCodeListVersion="2">220</eComStringAttributeValuePairList>
      <eComStringAttributeValuePairList attributeName="orderPriority">647</eComStringAttributeValuePairList>
      <eComStringAttributeValuePairList attributeName="customerDocumentReference">0</eComStringAttributeValuePairList>
    </avpList>
    

    如果我有一个混合属性节点值并使用赞成的答案,我会得到以下输出:

                "avpList": {
                "eComStringAttributeValuePairList": [
                    {
                        "attributeName": "orderTypeCode",
                        "qualifierCodeName": "order",
                        "qualifierCodeList": "OrderTypeCode",
                        "qualifierCodeListVersion": "2",
                    },
                    {
                        "attributeName": "orderPriority",
                    },
                    {
                        "attributeName": "customerDocumentReference",
                    }
                ]
            }
    

    通过JSONLint 运行它会显示除了缺少元素值之外的尾随逗号问题。

    Error: Parse error on line 30: ...Version": "2",                    }
    

    使用 XML Spy 2019 解析的脚本。

    使用注释过的Github 链接并解析会生成以下 JSON,该 JSON 在第一次通过 linter 时进行验证。请注意,如果您希望 JSON 中有任何形式的类型化数据,则样式表都不会这样做。

                "avpList": {
                "eComStringAttributeValuePairList": [
                    {
                        "attributeName": "orderTypeCode",
                        "qualifierCodeName": "order",
                        "qualifierCodeList": "OrderTypeCode",
                        "qualifierCodeListVersion": "2",
                        "text": "220"
                    },
                    {
                        "attributeName": "orderPriority",
                        "text": "647"
                    },
                    {
                        "attributeName": "customerDocumentReference",
                        "text": "0"
                    }
                ]
            }
    

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 2012-02-01
      • 2014-08-11
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      相关资源
      最近更新 更多