<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Oracle BI Publisher -Dataengine, datamodel:_Custom_OAL_ATG_OM_Dashboard_DM_xdm -->
<DATA>
<SUCCESS>
<COUNT___>5686</COUNT___>
</SUCCESS>
<REJECT>
<COUNT___>641</COUNT___>
</REJECT>
<FAILURE>
<COUNT___>8536</COUNT___>
</FAILURE>
<ERROR>
<COUNT___>1447</COUNT___>
</ERROR>
<TERMINATED>
<COUNT___>1341</COUNT___>
</TERMINATED>
</DATA>
上面是XML。我想将以上XML转换为下面显示的JSON。
{
"appName": "PERFORMANCE",
"statsName": "Status Counts",
"DateBegin": "xxxxxx",
"DateEnd": "xxxxxxx",
"data": {
"SUCCESS ": 1341,
"REJECT":5666, "FAILURE":640,
"ERROR":8515,
"TERMINATED":1447
}
}
我是XSLT样式表的新手。谁能帮助我解决上述问题?什么是将给定XML转换为JSON的XSLT样式表?
您可以使用以下样式表。它适用于给定的输入XML:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" />
<xsl:template match="/DATA">
<xsl:text>{
"appName": "PERFORMANCE",
"statsName": "Status Counts",
"DateBegin": "xxxxxx",
"DateEnd": "xxxxxxx",
"data":
</xsl:text>{
<xsl:apply-templates select="*" /><xsl:text>
}
}</xsl:text>
</xsl:template>
<xsl:template match="SUCCESS|REJECT|FAILURE|ERROR|TERMINATED">
<xsl:value-of select="concat('"',local-name(),'": ',COUNT___)" />
<xsl:if test="position() != last()">,
</xsl:if>
</xsl:template>
</xsl:stylesheet>
其输出是:
{
"appName": "PERFORMANCE",
"statsName": "Status Counts",
"DateBegin": "xxxxxx",
"DateEnd": "xxxxxxx",
"data":
{
"SUCCESS": 5686,
"REJECT": 641,
"FAILURE": 8536,
"ERROR": 1447,
"TERMINATED": 1341
}
}
输出不匹配您所需的输出,因为输入XML和所需的输出XML之间存在一些不一致之处。根据您的需要更改XSLT。