使用自定义XSLT将XML转换为JSON会松开花括号 [英] Transforming XML to JSON with custom XSLT looses curly braces
问题描述
我有一个.NET库,该库使用XSLT文件将啤酒xml文件转换为Web应用程序的json.
I've got a .NET library that uses an XSLT file for transforming beer xml files into json for a web app.
XSLT文件看起来很像这样:
The XSLT file looks a lot like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" />
<xsl:template match="RECIPES">
{
{
"description": {
"name": "<xsl:value-of select="NAME"/>",
"style": "<xsl:value-of select="STYLE/NAME"/>",
...
我正在使用c#中的这段代码进行转换:
And I'm converting using this piece of code in c#:
using(var writer = new StringWriter()){
_xsltCompiler.Transform(_document, null, writer);
json = writer.ToString();
}
现在,问题在于输出中缺少花括号和空格.它曾经可以工作.从源代码管理历史记录中,我看不到最近有任何明显的变化.有关如何解决此问题的任何建议?
Now, the problem is that curly braces and whitespace is missing from the output. And it used to work. From the source control history I can see no aparent changes lately. Any suggestions on how to fix this?
推荐答案
我建议最初将其转换为xml并将其存储到变量中,然后应用标准/通用模板将其转换为JSON.我将使用XSLT 2.0或3.0并实现xml-to-json()
.
I would recommend transforming it initially to xml and storing it into a variable then applying a standard/general template to transform that to JSON. I would this this slightly different using XSLT 2.0 or 3.0 and implement xml-to-json()
.
这是我对上述示例的解决方案:
This is my solution to the example above:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" />
<xsl:template match="RECIPES">
<xsl:variable name="xml">
<description>
<xsl:element name="name">
<xsl:value-of select="NAME"/>
</xsl:element>
<xsl:element name="style">
<xsl:value-of select="STYLE/NAME"/>
</xsl:element>
</description>
</xsl:variable>
{<xsl:apply-templates select="$xml" mode="xml-to-json"/>}
</xsl:template>
<!-- Object or Element Property-->
<xsl:template match="*" mode="xml-to-json">
"<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>"</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"</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="@*" mode="xml-to-json"/><xsl:apply-templates select="*" mode="xml-to-json"/>}</xsl:otherwise>
</xsl:choose>
<xsl:if test="following-sibling::*">,</xsl:if>
</xsl:template>
</xsl:stylesheet>
这篇关于使用自定义XSLT将XML转换为JSON会松开花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!