使用自定义 XSLT 将 XML 转换为 JSON 无需大括号

And*_*ard 5 c# xml xslt json

我有一个 .NET 库,它使用 XSLT 文件将啤酒 xml 文件转换为 Web 应用程序的 json。

XSLT 文件看起来很像这样:

<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"/>",
      ...
Run Code Online (Sandbox Code Playgroud)

我正在使用 C# 中的这段代码进行转换:

using(var writer = new StringWriter()){
     _xsltCompiler.Transform(_document, null, writer);
     json = writer.ToString();
}
Run Code Online (Sandbox Code Playgroud)

现在的问题是输出中缺少大括号和空格。它曾经有效。从源代码管理历史记录中我看不到最近有明显的变化。对于如何解决这个问题,有任何的建议吗?

cod*_*awi 1

我建议首先将其转换为 xml 并将其存储到变量中,然后应用标准/通用模板将其转换为 JSON。我会使用 XSLT 2.0 或 3.0 稍微不同并实现xml-to-json().

这是我对上面例子的解决方案:

<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>&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="@*" 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>
Run Code Online (Sandbox Code Playgroud)