xsl将xml转换为json

时间:2014-06-09 14:53:59

标签: xml json xslt

输入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名称和系统下的值。

2 个答案:

答案 0 :(得分:12)

我从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>

答案 1 :(得分:0)

@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"
                }
            ]
        }
相关问题