<?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样式表是什么?
答案 0 :(得分:2)
您可以使用以下样式表。对于给定的输入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。