最近,我不得不自己为同一类型的事情编写一个定制的解决方案。我使用 XSLT 完成了它,使用XslCompiledTransform
该类来运行 XML 输入和输出 JSON。
这需要一些工作,但应该可以帮助您了解基础知识(它是从我所做的工作中复制粘贴的,已更改为几乎适合您的需求):
适配器卡.XML
<AdapterCards>
<cards type="MCS">
<card>
<id>id1</id>
<description>desc1</description>
<mccode>code1</mccode>
</card>
<card>
<id>id2</id>
<description>desc2</description>
<mccode>code2</mccode>
</card>
</cards>
<cards type="MCM">
<card>
<id>id3</id>
<description>desc3</description>
<mccode>code3</mccode>
</card>
<card>
<id>id4</id>
<description>desc4</description>
<mccode>code4</mccode>
</card>
</cards>
<cards type="F"/>
<cards type="B"/>
</AdapterCards>
适配器卡.XSL
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="no" omit-xml-declaration="yes" method="text" encoding="UTF-8" media-type="text/x-json" />
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<!-- Main template for escaping strings; used by above template and for object-properties
Responsibilities: placed quotes around string, and chain up to next filter, escape-bs-string -->
<xsl:template name="escape-string">
<xsl:param name="s"/>
<xsl:text>"</xsl:text>
<xsl:call-template name="escape-bs-string">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
<xsl:text>"</xsl:text>
</xsl:template>
<!-- Escape the backslash (\) before everything else. -->
<xsl:template name="escape-bs-string">
<xsl:param name="s"/>
<xsl:choose>
<xsl:when test="contains($s,'\')">
<xsl:call-template name="escape-quot-string">
<xsl:with-param name="s" select="concat(substring-before($s,'\'),'\\')"/>
</xsl:call-template>
<xsl:call-template name="escape-bs-string">
<xsl:with-param name="s" select="substring-after($s,'\')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="escape-quot-string">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Escape the double quote ("). -->
<xsl:template name="escape-quot-string">
<xsl:param name="s"/>
<xsl:choose>
<xsl:when test="contains($s,'"')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'"'),'\"')"/>
</xsl:call-template>
<xsl:call-template name="escape-quot-string">
<xsl:with-param name="s" select="substring-after($s,'"')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Replace tab, line feed and/or carriage return by its matching escape code. Can't escape backslash
or double quote here, because they don't replace characters (� becomes \t), but they prefix
characters (\ becomes \\). Besides, backslash should be seperate anyway, because it should be
processed first. This function can't do that. -->
<xsl:template name="encode-string">
<xsl:param name="s"/>
<xsl:choose>
<!-- tab -->
<xsl:when test="contains($s,'	')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'	'),'\t',substring-after($s,'	'))"/>
</xsl:call-template>
</xsl:when>
<!-- line feed -->
<xsl:when test="contains($s,'
')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'
'),'\n',substring-after($s,'
'))"/>
</xsl:call-template>
</xsl:when>
<!-- carriage return -->
<xsl:when test="contains($s,'
')">
<xsl:call-template name="encode-string">
<xsl:with-param name="s" select="concat(substring-before($s,'
'),'\r',substring-after($s,'
'))"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$s"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="card">
<xsl:text>{</xsl:text>
<xsl:text>"type":</xsl:text>
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="translate(../@type, $uppercase, $smallcase)"/>
</xsl:call-template>
<xsl:text>,"id":</xsl:text>
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="id"/>
</xsl:call-template>
<xsl:text>,"description":</xsl:text>
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="description"/>
</xsl:call-template>
<xsl:text>,"mccode":</xsl:text>
<xsl:call-template name="escape-string">
<xsl:with-param name="s" select="mccode"/>
</xsl:call-template>
<xsl:if test="following::card">},</xsl:if>
<xsl:if test="not(following::card)">}</xsl:if>
</xsl:template>
<xsl:template match="/AdapterCards">
<xsl:text>{[</xsl:text>
<xsl:apply-templates select="cards/card" />
<xsl:text>]}</xsl:text>
</xsl:template>
</xsl:stylesheet>
C#
// Load XML document
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("AdapterCards.XML");
// Transform the XML into JSON
XslCompiledTransform transformer = new XslCompiledTransform();
using (var xslStylesheetFile = File.Open("AdapterCards.XSL", FileMode.Open))
{
using (var xmlReader = new XmlTextReader(xslStylesheetFile))
{
transformer.Load(xmlReader);
}
}
var sourceNavigator = xmlDoc.CreateNavigator();
using (MemoryStream ms = new MemoryStream())
{
transformer.Transform(sourceNavigator, null, ms);
ms.Position = 0;
using (var sr = new StreamReader(ms))
{
return sr.ReadToEnd(); // <-- this is your JSON
}
}
我在 Notepad++ 中执行了上面的 XSL,得到了以下结果:
{[{"type":"mcs","id":"id1","description":"desc1","mccode":"code1"},
{"type":"mcs","id":"id2","description":"desc2","mccode":"code2"},
{"type":"mcm","id":"id3","description":"desc3","mccode":"code3"},
{"type":"mcm","id":"id4","description":"desc4","mccode":"code4"}]}
如您所见,它们需要的地方缺少逗号。但它几乎就在那里!
更新了 XSL 和现在在正确位置使用逗号的输出。接下来要解决的是“类型”的情况。我想您可能需要使用 XSL 2.0 来访问xpath 函数 fn:lower-case()
。
EDIT3 :在这个答案的帮助下, Done-case 现在被翻译为更低。
参考: