【问题标题】:How to convert json to xml using xslt如何使用 xslt 将 json 转换为 xml
【发布时间】:2012-10-12 00:25:36
【问题描述】:

如何将 JSON 转换为 XML?

考虑:

<sampleTag>
{
  "Order": {
    "InvestmentAccount": { "AccountNumber": "10" },
    "Parcel": {      
      "Limit": "0",
      "ExpiryDate": "1900-01-01T00:00:00",
      "Asset": [
    {        
        "Open": "25.15",
        "High": "25.15",
        "Low": "25.11",
        "Close": "25.87"
      }
    {        
        "Open": "25.15",
        "High": "25.15",
        "Low": "25.11",
        "Close": "25.87"
      }]
    },

    "OrderDate": "2012-10-11T21:46:03.6489906+11:00",

  }
}
</sampleTag>

改造后的文件如下:

<Order>
    <InvestmentAccount>
        <AccountNumber>10</AccountNumber>
    </InvestmentAccount>
    <Parcel>
        <Limit>0</Limit>
        <ExpiryDate>1900-01-01T00:00:00</ExpiryDate>
        <Asset>
            <Open>25.15</Open>
            <High>25.15</High>
            <Low>25.11</Low>
            <Close>25.87</Close>
        </Asset>
        <Asset>
            <Open>25.15</Open>
            <High>25.15</High>
            <Low>25.11</Low>
            <Close>25.87</Close>
        </Asset>
    </Parcel>
    <OrderDate>2012-10-11T21:46:03.6489906+11:00</OrderDate>
</Order>

【问题讨论】:

  • 我真的怀疑您是否可以使用更适合 xml 处理的 xslt 轻松做到这一点。考虑提取 json 并改用您的编程语言的库进行转换。
  • 为什么要受限于 XSLT 1.0?您当前的 XSLT 处理器是什么?
  • 这个xslt我必须在Datapower工具中使用..它只支持xslt 1.0
  • Datapower 部分支持 xslt 2.0。我不确定使用FXSL的json()函数是否足够,但你可以试试。
  • 我尝试使用 FXSL 函数但无法成功。 DP 支持这些 exslt 函数(),但我可以从这些函数中得到任何东西。

标签: xslt xslt-1.0


【解决方案1】:

我在 JSON 解析方面的工作并未涵盖完整的 JSON 语法。

而将任何 JSON 文档“翻译”为 XML 文档的任务没有解决方案。有 JSON 结构,如果不定义额外的约定和引入额外的元素,就无法将其转换为 XML——因此最终的 XML 结构并不是原始 JSON 对象的真实自然表示。

在 XSLT 3.0 中有一个函数可以将任何 JSON 对象 -- parse-json() -- 解析为 map -- XSLT 3.0 中引入的一种新数据类型。在这里阅读: http://www.w3.org/TR/xslt-30/#json

【讨论】:

    【解决方案2】:

    其实,这并不难。处理它的方法是检查 jason 的语法,并像查看模板一样查看每个产品。当我考虑到 OP 忘记谷歌搜索预先存在的解决方案的可能性时,我正要写一个解决方案。我搜索了一下,你瞧……

    更新

    这是一个 JSON 到 XML 的转换器。但它只适用于 json 的一个子集。希望该子集足够广泛,可以满足您的特定需求。特别是限制是:

    1. 唯一支持的简单类型是字符串。没有整数、布尔值或空值。
    2. Json 对象名称必须是有效的 xml 元素名称。
    3. 字符串值中不允许有转义码。这意味着您不能传输包含例如 " 字符的值(无需滚动您自己的编码层)。

    此 XSLT 1.0 样式表...*

    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
      xmlns:exsl="http://exslt.org/common"
      xmlns:so="http://stackoverflow.com/questions/13007280"
      exclude-result-prefixes="xsl xs json so exsl">
    <xsl:output indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
    <xsl:strip-space elements="*" /> 
    
    <xsl:variable name="quot" select="'&quot;'" />
    
    <xsl:template match="/*">
      <xsl:variable name="t1">
        <xsl:call-template name="object">
         <xsl:with-param name="json-in" select="." />
        </xsl:call-template>
      </xsl:variable>
      <xsl:apply-templates select="exsl:node-set($t1)/so:output/*" mode="copy-sans-namespace" />  
    </xsl:template>
    
    <xsl:template match="*" mode="copy-sans-namespace">
      <xsl:element name="{name()}" namespace="{namespace-uri()}">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates mode="copy-sans-namespace" />
      </xsl:element>
    </xsl:template>
    
    <xsl:template name="field">
      <!-- Input like: "Open": "25.15" bla -->
      <!-- output like: <so:output><Open>25.15</Open></so:output> <so:extra>bla</so:extra> -->
      <xsl:param name="json-in" />
      <xsl:variable name="field-name" select="substring-before(substring-after($json-in,$quot),$quot)" />
      <xsl:variable name="remainder" select="substring-after($json-in,':')" />
      <xsl:call-template name="value">
        <xsl:with-param name="json-in" select="$remainder" />
        <xsl:with-param name="parent-ele" select="$field-name" />
      </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="fields">
      <!-- Input like: "Open": "25.15" , "High": "25.15" } bla -->
      <!-- output like: <so:output><Open>25.15</Open><High>25.15</High></so:output> <so:extra>} bla</so:extra> -->
      <xsl:param name="json-in" />
      <xsl:variable name="n" select="normalize-space($json-in)" />
      <xsl:choose>
        <xsl:when test="substring($n,1,1) = $quot">
        <xsl:variable name="t1">
            <xsl:call-template name="field">
              <xsl:with-param name="json-in" select="$n" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
        <xsl:variable name="t3">
          <xsl:choose>
          <xsl:when test="substring($t2,1,1)=','">
                <xsl:call-template name="fields">
                  <xsl:with-param name="json-in" select="substring-after($t2,',')" />
              </xsl:call-template>
          </xsl:when>
          <xsl:when test="$t2">
            <so:extra><xsl:value-of select="$t2" /></so:extra>
          </xsl:when>
          </xsl:choose>
        </xsl:variable>
        <so:output>
          <xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
        </so:output>
        <xsl:copy-of select="exsl:node-set($t3)/so:extra" />
      </xsl:when>
        <xsl:when test="$n">
          <so:extra><xsl:value-of select="$n" /></so:extra>
        </xsl:when>
      </xsl:choose>
    </xsl:template>
    
    <xsl:template name="object">
      <!-- Input like: { X } bla -->
      <!-- output like: <so:output>fields(X)</so:output> <so:extra>bla</so:extra> -->
      <xsl:param name="json-in" />
      <xsl:param name="parent-ele" select="''" />
      <xsl:variable name="t1" select="normalize-space(substring-after($json-in,'{'))" />
      <xsl:variable name="t2">
        <xsl:call-template name="fields">
          <xsl:with-param name="json-in" select="$t1" />
        </xsl:call-template>
      </xsl:variable>  
      <xsl:variable name="t3" select="normalize-space(substring-after( exsl:node-set($t2)/so:extra, '}'))" />
      <so:output>
        <xsl:choose>
        <xsl:when test="$parent-ele">
          <xsl:element name="{$parent-ele}">
            <xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
          </xsl:element>
        </xsl:when>
          <xsl:otherwise>    
            <xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
          </xsl:otherwise>    
        </xsl:choose>
      </so:output>
      <xsl:if test="$t3">
        <so:extra><xsl:value-of select="$t3" /></so:extra>
      </xsl:if>  
    </xsl:template>
    
    <xsl:template name="objects">
      <xsl:param name="json-in" />
      <xsl:param name="parent-ele" />
      <xsl:variable name="n" select="normalize-space($json-in)" />
      <xsl:choose>
        <xsl:when test="substring($n,1,1) = '{'">
        <xsl:variable name="t1">
            <xsl:call-template name="object">
              <xsl:with-param name="json-in" select="$n" />
              <xsl:with-param name="parent-ele" select="$parent-ele" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
        <xsl:variable name="t3">
          <xsl:choose>
          <xsl:when test="substring($t2,1,1)='{'">
                <xsl:call-template name="objects">
                  <xsl:with-param name="json-in" select="$t2" />
                  <xsl:with-param name="parent-ele" select="$parent-ele" />
              </xsl:call-template>
          </xsl:when>
          <xsl:when test="$t2">
            <so:extra><xsl:value-of select="$t2" /></so:extra>
          </xsl:when>
          </xsl:choose>
        </xsl:variable>
        <so:output>
          <xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
        </so:output>
        <xsl:copy-of select="exsl:node-set($t3)/so:extra" />
      </xsl:when>
        <xsl:when test="$n">
          <so:extra><xsl:value-of select="$n" /></so:extra>
        </xsl:when>
      </xsl:choose>
    </xsl:template>
    
    <xsl:template name="array">
      <!-- Input like: [ X1 X2 ] bla -->
      <!-- output like: <so:output><Y>X1</Y><Y>X2</Y></so:output> <so:extra>}bla</so:extra> -->
      <xsl:param name="json-in" />
      <xsl:param name="parent-ele" />
      <xsl:variable name="t1" select="normalize-space(substring-after($json-in,'['))" />
      <xsl:variable name="t2">
        <xsl:call-template name="objects">
          <xsl:with-param name="json-in" select="$t1" />
          <xsl:with-param name="parent-ele" select="$parent-ele" />
        </xsl:call-template>
      </xsl:variable>  
      <xsl:variable name="t3" select="normalize-space(substring-after( exsl:node-set($t2)/so:extra, ']'))" />
      <xsl:copy-of select="exsl:node-set($t2)/so:output" />
      <xsl:if test="$t3">
        <so:extra><xsl:value-of select="$t3" /></so:extra>
      </xsl:if>  
    </xsl:template>
    
    <xsl:template name="value">
      <!-- Input like either array, object or string -->
      <!-- output like either array, object or string -->
      <xsl:param name="json-in" />
      <xsl:param name="parent-ele" />
      <xsl:variable name="first-letter" select="substring(normalize-space($json-in),1,1)" />
      <xsl:choose>
        <xsl:when test="$first-letter='{'">
        <xsl:call-template name="object">
            <xsl:with-param name="json-in" select="$json-in" />
            <xsl:with-param name="parent-ele" select="$parent-ele" />
        </xsl:call-template>
        </xsl:when>
        <xsl:when test="$first-letter='['">
        <xsl:call-template name="array">
            <xsl:with-param name="json-in" select="$json-in" />
            <xsl:with-param name="parent-ele" select="$parent-ele" />
        </xsl:call-template>
        </xsl:when>
        <xsl:when test="$first-letter=$quot">
        <xsl:call-template name="string">
            <xsl:with-param name="json-in" select="$json-in" />
            <xsl:with-param name="parent-ele" select="$parent-ele" />
        </xsl:call-template>
        </xsl:when>
      <xsl:otherwise>
        <so:output>ERROR</so:output>
      </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    
    <xsl:template name="string">
      <!-- Input like: "X" bla -->
      <!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
      <xsl:param name="json-in" />
      <xsl:param name="parent-ele" />
      <xsl:variable name="value" select="substring-before(substring-after($json-in,$quot),$quot)" />
      <xsl:variable name="remainder" select="normalize-space(substring-after(substring-after($json-in,$quot),$quot))" />
      <so:output>
       <xsl:element name="{$parent-ele}">
        <xsl:value-of select="$value" />
       </xsl:element>
      </so:output>
      <xsl:if test="$remainder">
        <so:extra><xsl:value-of select="$remainder" /></so:extra>
      </xsl:if>  
    </xsl:template>
    
    </xsl:stylesheet>
    

    ...应用于此输入(根据提供的 OP 修改以删除多余的逗号)...

    <sampleTag>
    {
      "Order": {
        "InvestmentAccount": { "AccountNumber": "10" },
        "Parcel": {      
          "Limit": "0",
          "ExpiryDate": "1900-01-01T00:00:00",
          "Asset": [
        {        
            "Open": "25.15",
            "High": "25.15",
            "Low": "25.11",
            "Close": "25.87"
          }
        {        
            "Open": "25.15",
            "High": "25.15",
            "Low": "25.11",
            "Close": "25.87"
          }]
        },
        "OrderDate": "2012-10-11T21:46:03.6489906+11:00"
      }
    }
    </sampleTag>
    

    ..yields...

    <Order>
      <InvestmentAccount>
        <AccountNumber>10</AccountNumber>
      </InvestmentAccount>
      <Parcel>
        <Limit>0</Limit>
        <ExpiryDate>1900-01-01T00:00:00</ExpiryDate>
        <Asset>
          <Open>25.15</Open>
          <High>25.15</High>
          <Low>25.11</Low>
          <Close>25.87</Close>
        </Asset>
        <Asset>
          <Open>25.15</Open>
          <High>25.15</High>
          <Low>25.11</Low>
          <Close>25.87</Close>
        </Asset>
      </Parcel>
      <OrderDate>2012-10-11T21:46:03.6489906+11:00</OrderDate>
    </Order>
    

    【讨论】:

    • @Sean---感谢您的链接。我猜这仅支持 xslt2.0 版本,但不支持 xslt v1.0...
    • @user1731504 我将使用 XSLT 1.0 json 到 xml 转换器,但只有一个适用于有限的 json 子集。希望它足以满足您的需求。
    • @Sean -- 在我的 json 数组中,项目以逗号分隔。以上xsl,只给我第一个元素。如何为逗号分隔的数组元素调整它?例如:两个资产项目以逗号分隔,} 后,每个项目的括号。
    • @Kumaresan:我通过将名为“objects”的模板中变量 $t2 的定义更改为 &lt;xsl:choose&gt; 元素来检查exsl:node-set($t1)/so:extra 的第一个字符是否为逗号。如果是这样,请在规范化空白之前将其删除。到目前为止似乎对我们有用!
    【解决方案3】:

    我稍微调整了 Sean B. Durkin 的模板,并认为我会分享。

    更新包括:

    • 支持数字
    • 支持布尔值
    • 修复了以逗号分隔的对象数组元素(根据 JSON 规范)

    非更新更改:

    • 数组元素显示在自己的 XML 元素中,元素名称为对象键,后缀为_element

    仍然不支持:

    • 字符串中的转义字符(引号)

    这是模板:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
        xmlns:exsl="http://exslt.org/common"
        xmlns:so="http://stackoverflow.com/questions/13007280"
        exclude-result-prefixes="xsl xs json so exsl">
        <xsl:output indent="yes" encoding="UTF-8" />
        <xsl:strip-space elements="*" /> 
    
        <xsl:variable name="quot" select="'&quot;'" />
        <xsl:variable name="numbers" select="'0123456789'"/>
        <xsl:variable name="booleans" select="'tf'"/>
    
        <xsl:template match="/*">
            <xsl:variable name="t1">
                <xsl:call-template name="object">
                    <xsl:with-param name="json-in" select="." />
                </xsl:call-template>
            </xsl:variable>
            <xsl:apply-templates select="exsl:node-set($t1)/so:output/*" mode="copy-sans-namespace" />  
        </xsl:template>
    
        <xsl:template match="*" mode="copy-sans-namespace">
            <xsl:element name="{name()}" namespace="{namespace-uri()}">
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates mode="copy-sans-namespace" />
            </xsl:element>
        </xsl:template>
    
        <xsl:template name="field">
            <!-- Input like: "Open": "25.15" bla -->
            <!-- output like: <so:output><Open>25.15</Open></so:output> <so:extra>bla</so:extra> -->
            <xsl:param name="json-in" />
            <xsl:variable name="field-name" select="substring-before(substring-after($json-in,$quot),$quot)" />
            <xsl:variable name="remainder" select="substring-after($json-in,':')" />
            <xsl:call-template name="value">
                <xsl:with-param name="json-in" select="$remainder" />
                <xsl:with-param name="parent-ele" select="$field-name" />
            </xsl:call-template>
        </xsl:template>
    
        <xsl:template name="fields">
            <!-- Input like: "Open": "25.15" , "High": "25.15" } bla -->
            <!-- output like: <so:output><Open>25.15</Open><High>25.15</High></so:output> <so:extra>} bla</so:extra> -->
            <xsl:param name="json-in" />
            <xsl:variable name="n" select="normalize-space($json-in)" />
            <xsl:choose>
                <xsl:when test="substring($n,1,1) = $quot">
                    <xsl:variable name="t1">
                        <xsl:call-template name="field">
                            <xsl:with-param name="json-in" select="$n" />
                        </xsl:call-template>
                    </xsl:variable>
                    <xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
                    <xsl:variable name="t3">
                        <xsl:choose>
                            <xsl:when test="substring($t2,1,1)=','">
                                <xsl:call-template name="fields">
                                    <xsl:with-param name="json-in" select="substring-after($t2,',')" />
                                </xsl:call-template>
                            </xsl:when>
                            <xsl:when test="$t2">
                                <so:extra><xsl:value-of select="$t2" /></so:extra>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:variable>
                    <so:output>
                        <xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
                    </so:output>
                    <xsl:copy-of select="exsl:node-set($t3)/so:extra" />
                </xsl:when>
                <xsl:when test="$n">
                    <so:extra><xsl:value-of select="$n" /></so:extra>
                </xsl:when>
            </xsl:choose>
        </xsl:template>
    
        <xsl:template name="object">
            <!-- Input like: { X } bla -->
            <!-- output like: <so:output>fields(X)</so:output> <so:extra>bla</so:extra> -->
            <xsl:param name="json-in" />
            <xsl:param name="parent-ele" select="''" />
            <xsl:variable name="t1" select="normalize-space(substring-after($json-in,'{'))" />
            <xsl:variable name="t2">
                    <xsl:call-template name="fields">
                    <xsl:with-param name="json-in" select="$t1" />
                </xsl:call-template>
            </xsl:variable>  
            <xsl:variable name="t3" select="normalize-space(substring-after( exsl:node-set($t2)/so:extra, '}'))" />
            <so:output>
                <xsl:choose>
                    <xsl:when test="$parent-ele">
                        <xsl:element name="{$parent-ele}">
                            <xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
                        </xsl:element>
                    </xsl:when>
                    <xsl:otherwise>    
                        <xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
                    </xsl:otherwise>    
                </xsl:choose>
            </so:output>
            <xsl:if test="$t3">
                <so:extra><xsl:value-of select="$t3" /></so:extra>
            </xsl:if>  
        </xsl:template>
    
        <xsl:template name="objects">
            <xsl:param name="json-in" />
            <xsl:param name="parent-ele" />
            <xsl:variable name="n" select="normalize-space($json-in)" />
            <xsl:choose>
                <xsl:when test="substring($n,1,1) = '{'">
                    <xsl:variable name="t1">
                        <xsl:call-template name="object">
                            <xsl:with-param name="json-in" select="$n" />
                            <xsl:with-param name="parent-ele" select="$parent-ele" />
                        </xsl:call-template>
                    </xsl:variable>
                    <xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
                    <xsl:variable name="t3">
                        <xsl:choose>
                            <xsl:when test="substring($t2,1,1)='{'">
                                <xsl:call-template name="objects">
                                    <xsl:with-param name="json-in" select="$t2" />
                                    <xsl:with-param name="parent-ele" select="$parent-ele" />
                                </xsl:call-template>
                            </xsl:when>
                            <xsl:when test="substring($t2,1,1)=',' and substring(normalize-space(substring-after($t2,',')),1,1)='{'">
                                <xsl:call-template name="objects">
                                    <xsl:with-param name="json-in" select="normalize-space(substring-after($t2,','))" />
                                    <xsl:with-param name="parent-ele" select="$parent-ele" />
                                </xsl:call-template>
                            </xsl:when>
                            <xsl:when test="$t2">
                                <so:extra><xsl:value-of select="$t2" /></so:extra>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:variable>
                    <so:output>
                        <xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
                    </so:output>
                    <xsl:copy-of select="exsl:node-set($t3)/so:extra" />
                </xsl:when>
                <xsl:when test="$n">
                    <so:extra><xsl:value-of select="$n" /></so:extra>
                </xsl:when>
            </xsl:choose>
        </xsl:template>
    
        <xsl:template name="array">
            <!-- Input like: [ X1 X2 ] bla -->
            <!-- output like: <so:output><Y>X1</Y><Y>X2</Y></so:output> <so:extra>}bla</so:extra> -->
            <xsl:param name="json-in" />
            <xsl:param name="parent-ele" />
            <xsl:variable name="t1" select="normalize-space(substring-after($json-in,'['))" />
            <xsl:variable name="t2">
                <xsl:call-template name="objects">
                    <xsl:with-param name="json-in" select="$t1" />
                    <xsl:with-param name="parent-ele" select="$parent-ele" />
                </xsl:call-template>
            </xsl:variable>  
            <xsl:variable name="t3">
                <xsl:choose>
                    <xsl:when test="contains(substring-before(exsl:node-set($t2)/so:extra,']'),',')">
                        <xsl:value-of select="normalize-space(substring-after(exsl:node-set($t2)/so:extra,','))"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="normalize-space(substring-after(exsl:node-set($t2)/so:extra,']'))"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:variable name="t4">
                <xsl:element name="{$parent-ele}">
                    <xsl:for-each select="$t2/so:output/*[local-name(.)=$parent-ele]">
                        <xsl:variable name="self" select="."/>
                        <xsl:variable name="tempResult">
                            <xsl:element name="{concat($parent-ele,'_element')}">
                                <xsl:copy-of select="exsl:node-set($self/*)" />
                            </xsl:element>
                        </xsl:variable>
                        <xsl:copy-of select="exsl:node-set($tempResult)"/>
                    </xsl:for-each>
                </xsl:element>
            </xsl:variable>
            <xsl:variable name="t5" select="exsl:node-set($t4)"/>
            <so:output>
                <xsl:copy-of select="$t5"/>
            </so:output>
            <xsl:if test="$t3">
                <so:extra><xsl:value-of select="$t3" /></so:extra>
            </xsl:if>  
        </xsl:template>
    
        <xsl:template name="value">
            <!-- Input like either array, object or string -->
            <!-- output like either array, object or string -->
            <xsl:param name="json-in" />
            <xsl:param name="parent-ele" />
            <xsl:variable name="first-letter" select="substring(normalize-space($json-in),1,1)" />
            <xsl:choose>
                <xsl:when test="$first-letter='{'">
                    <xsl:call-template name="object">
                        <xsl:with-param name="json-in" select="$json-in" />
                        <xsl:with-param name="parent-ele" select="$parent-ele" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:when test="$first-letter='['">
                    <xsl:call-template name="array">
                        <xsl:with-param name="json-in" select="$json-in" />
                        <xsl:with-param name="parent-ele" select="$parent-ele" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:when test="$first-letter=$quot">
                    <xsl:call-template name="string">
                        <xsl:with-param name="json-in" select="$json-in" />
                        <xsl:with-param name="parent-ele" select="$parent-ele" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:when test="contains($numbers,$first-letter)">
                    <xsl:call-template name="number">
                        <xsl:with-param name="json-in" select="$json-in" />
                        <xsl:with-param name="parent-ele" select="$parent-ele"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:when test="contains($booleans,$first-letter)">
                    <xsl:call-template name="boolean">
                        <xsl:with-param name="json-in" select="$json-in" />
                        <xsl:with-param name="parent-ele" select="$parent-ele"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <so:output>ERROR</so:output>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    
        <xsl:template name="string">
            <!-- Input like: "X" bla -->
            <!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
            <xsl:param name="json-in" />
            <xsl:param name="parent-ele" />
            <xsl:variable name="value" select="substring-before(substring-after($json-in,$quot),$quot)" />
            <xsl:variable name="remainder" select="normalize-space(substring-after(substring-after($json-in,$quot),$quot))" />
            <so:output>
                <xsl:element name="{$parent-ele}">
                    <xsl:value-of select="$value" />
                </xsl:element>
            </so:output>
            <xsl:if test="$remainder">
                <so:extra><xsl:value-of select="$remainder" /></so:extra>
            </xsl:if>  
        </xsl:template>
    
        <xsl:template name="number">
            <!-- Input like: "X" bla -->
            <!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
            <xsl:param name="json-in" />
            <xsl:param name="parent-ele" />
            <xsl:variable name="value">
                <xsl:choose>
                    <xsl:when test="contains(substring-before($json-in,','),'}')">
                        <xsl:value-of select="normalize-space(substring-before($json-in,'}'))"/>
                    </xsl:when>
                    <xsl:when test="contains(substring-before($json-in,','),']')">
                        <xsl:value-of select="normalize-space(substring-before($json-in,']'))"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="normalize-space(substring-before($json-in,','))"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:variable name="remainder">
                <xsl:choose>
                    <xsl:when test="contains(substring-before($json-in,','),'}')">
                        <xsl:value-of select="concat('}',normalize-space(substring-after($json-in,'}')))"/>
                    </xsl:when>
                    <xsl:when test="contains(substring-before($json-in,','),']')">
                        <xsl:value-of select="concat(']',normalize-space(substring-after($json-in,']')))"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="concat(',',normalize-space(substring-after($json-in,',')))"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>  
            <so:output>
                <xsl:element name="{$parent-ele}">
                    <xsl:value-of select="$value" />
                </xsl:element>
            </so:output>
            <xsl:if test="$remainder">
                <so:extra><xsl:value-of select="$remainder" /></so:extra>
            </xsl:if>  
        </xsl:template>
        <xsl:template name="boolean">
            <!-- Input like: "X" bla -->
            <!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
            <xsl:param name="json-in" />
            <xsl:param name="parent-ele" />
            <xsl:variable name="value">
                <xsl:choose>
                    <xsl:when test="contains(substring-before($json-in,','),'}')">
                        <xsl:value-of select="normalize-space(substring-before($json-in,'}'))"/>
                    </xsl:when>
                    <xsl:when test="contains(substring-before($json-in,','),']')">
                        <xsl:value-of select="normalize-space(substring-before($json-in,']'))"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="normalize-space(substring-before($json-in,','))"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:variable name="remainder">
                <xsl:choose>
                    <xsl:when test="contains(substring-before($json-in,','),'}')">
                        <xsl:value-of select="concat('}',normalize-space(substring-after($json-in,'}')))"/>
                    </xsl:when>
                    <xsl:when test="contains(substring-before($json-in,','),']')">
                        <xsl:value-of select="concat(']',normalize-space(substring-after($json-in,']')))"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="concat(',',normalize-space(substring-after($json-in,',')))"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>  
            <so:output>
                <xsl:element name="{$parent-ele}">
                    <xsl:value-of select="$value" />
                </xsl:element>
            </so:output>
            <xsl:if test="$remainder">
                <so:extra><xsl:value-of select="$remainder" /></so:extra>
            </xsl:if>  
        </xsl:template>
    
    </xsl:stylesheet>
    

    所以有了这个(调整后的)输入:

    <?xml version="1.0" encoding="UTF-8"?>
    <sampleTag><![CDATA[
        {
            "Order": {
                "InvestmentAccount": { "AccountNumber": "10" },
                "Parcel": {      
                    "Limit": 0,
                    "ExpiryDate": "1900-01-01T00:00:00",
                    "valid": true,
                    "Asset": [
                        {        
                            "Open": 25.15,
                            "High": 25.15,
                            "Low": 25.11,
                            "Close": 25.87
                        },
                        {        
                            "Open": 25.15,
                            "High": 25.15,
                            "Low": 25.11,
                            "Close": 25.87
                        }
                    ]
                },
                "OrderDate": "2012-10-11T21:46:03.6489906+11:00"
            }
        }
    ]]></sampleTag>
    

    我得到这个输出:

    <?xml version="1.0" encoding="UTF-8"?>
    <Order>
       <InvestmentAccount>
          <AccountNumber>10</AccountNumber>
       </InvestmentAccount>
       <Parcel>
          <Limit>0</Limit>
          <ExpiryDate>1900-01-01T00:00:00</ExpiryDate>
          <valid>true</valid>
          <Asset>
             <Asset_element>
                <Open>25.15</Open>
                <High>25.15</High>
                <Low>25.11</Low>
                <Close>25.87</Close>
             </Asset_element>
             <Asset_element>
                <Open>25.15</Open>
                <High>25.15</High>
                <Low>25.11</Low>
                <Close>25.87</Close>
             </Asset_element>
          </Asset>
       </Parcel>
       <OrderDate>2012-10-11T21:46:03.6489906+11:00</OrderDate>
    </Order>
    

    【讨论】:

    • 这个解决方案几乎是完美的。只是缺少简单元素的数组:{"array": ["1", "2", "3"]}
    【解决方案4】:

    这个问题被标记为 XSLT 1.0,所以我不知道这个答案是否会对原始问题有所帮助。但是,如果您可以使用 XSLT 3.0,那么函数 json-to-xml 完全可以满足您的需求。

    https://www.w3.org/TR/xslt-30/#func-json-to-xml

    【讨论】:

    • 使用我们的实用程序作为预处理步骤,您可以获得 XSLT 3.0 JSON/XML 格式,而无需 XSLT 3.0 处理器:github.com/AtomGraph/JSON2XML
    【解决方案5】:

    更新塞缪尔墨菲的答案。

    更新包括:

    • 支持 null
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
        xmlns:exsl="http://exslt.org/common"
        xmlns:so="http://stackoverflow.com/questions/13007280"
        exclude-result-prefixes="xsl xs json so exsl">
        <xsl:output indent="yes" encoding="UTF-8" />
        <xsl:strip-space elements="*" /> 
    
        <xsl:variable name="quot" select="'&quot;'" />
        <xsl:variable name="numbers" select="'0123456789'"/>
        <xsl:variable name="booleans" select="'tf'"/>
        <xsl:variable name="nulls" select="'n'"/>
    
        <xsl:template match="/*">
            <xsl:variable name="t1">
                <xsl:call-template name="object">
                    <xsl:with-param name="json-in" select="." />
                </xsl:call-template>
            </xsl:variable>
            <xsl:apply-templates select="exsl:node-set($t1)/so:output/*" mode="copy-sans-namespace" />  
        </xsl:template>
    
        <xsl:template match="*" mode="copy-sans-namespace">
            <xsl:element name="{name()}" namespace="{namespace-uri()}">
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates mode="copy-sans-namespace" />
            </xsl:element>
        </xsl:template>
    
        <xsl:template name="field">
            <!-- Input like: "Open": "25.15" bla -->
            <!-- output like: <so:output><Open>25.15</Open></so:output> <so:extra>bla</so:extra> -->
            <xsl:param name="json-in" />
            <xsl:variable name="field-name" select="substring-before(substring-after($json-in,$quot),$quot)" />
            <xsl:variable name="remainder" select="substring-after($json-in,':')" />
            <xsl:call-template name="value">
                <xsl:with-param name="json-in" select="$remainder" />
                <xsl:with-param name="parent-ele" select="$field-name" />
            </xsl:call-template>
        </xsl:template>
    
        <xsl:template name="fields">
            <!-- Input like: "Open": "25.15" , "High": "25.15" } bla -->
            <!-- output like: <so:output><Open>25.15</Open><High>25.15</High></so:output> <so:extra>} bla</so:extra> -->
            <xsl:param name="json-in" />
            <xsl:variable name="n" select="normalize-space($json-in)" />
            <xsl:choose>
                <xsl:when test="substring($n,1,1) = $quot">
                    <xsl:variable name="t1">
                        <xsl:call-template name="field">
                            <xsl:with-param name="json-in" select="$n" />
                        </xsl:call-template>
                    </xsl:variable>
                    <xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
                    <xsl:variable name="t3">
                        <xsl:choose>
                            <xsl:when test="substring($t2,1,1)=','">
                                <xsl:call-template name="fields">
                                    <xsl:with-param name="json-in" select="substring-after($t2,',')" />
                                </xsl:call-template>
                            </xsl:when>
                            <xsl:when test="$t2">
                                <so:extra><xsl:value-of select="$t2" /></so:extra>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:variable>
                    <so:output>
                        <xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
                    </so:output>
                    <xsl:copy-of select="exsl:node-set($t3)/so:extra" />
                </xsl:when>
                <xsl:when test="$n">
                    <so:extra><xsl:value-of select="$n" /></so:extra>
                </xsl:when>
            </xsl:choose>
        </xsl:template>
    
        <xsl:template name="object">
            <!-- Input like: { X } bla -->
            <!-- output like: <so:output>fields(X)</so:output> <so:extra>bla</so:extra> -->
            <xsl:param name="json-in" />
            <xsl:param name="parent-ele" select="''" />
            <xsl:variable name="t1" select="normalize-space(substring-after($json-in,'{'))" />
            <xsl:variable name="t2">
                    <xsl:call-template name="fields">
                    <xsl:with-param name="json-in" select="$t1" />
                </xsl:call-template>
            </xsl:variable>  
            <xsl:variable name="t3" select="normalize-space(substring-after( exsl:node-set($t2)/so:extra, '}'))" />
            <so:output>
                <xsl:choose>
                    <xsl:when test="$parent-ele">
                        <xsl:element name="{$parent-ele}">
                            <xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
                        </xsl:element>
                    </xsl:when>
                    <xsl:otherwise>    
                        <xsl:copy-of select="exsl:node-set($t2)/so:output/node()" />
                    </xsl:otherwise>    
                </xsl:choose>
            </so:output>
            <xsl:if test="$t3">
                <so:extra><xsl:value-of select="$t3" /></so:extra>
            </xsl:if>  
        </xsl:template>
    
        <xsl:template name="objects">
            <xsl:param name="json-in" />
            <xsl:param name="parent-ele" />
            <xsl:variable name="n" select="normalize-space($json-in)" />
            <xsl:choose>
                <xsl:when test="substring($n,1,1) = '{'">
                    <xsl:variable name="t1">
                        <xsl:call-template name="object">
                            <xsl:with-param name="json-in" select="$n" />
                            <xsl:with-param name="parent-ele" select="$parent-ele" />
                        </xsl:call-template>
                    </xsl:variable>
                    <xsl:variable name="t2" select="normalize-space( exsl:node-set($t1)/so:extra) " />
                    <xsl:variable name="t3">
                        <xsl:choose>
                            <xsl:when test="substring($t2,1,1)='{'">
                                <xsl:call-template name="objects">
                                    <xsl:with-param name="json-in" select="$t2" />
                                    <xsl:with-param name="parent-ele" select="$parent-ele" />
                                </xsl:call-template>
                            </xsl:when>
                            <xsl:when test="substring($t2,1,1)=',' and substring(normalize-space(substring-after($t2,',')),1,1)='{'">
                                <xsl:call-template name="objects">
                                    <xsl:with-param name="json-in" select="normalize-space(substring-after($t2,','))" />
                                    <xsl:with-param name="parent-ele" select="$parent-ele" />
                                </xsl:call-template>
                            </xsl:when>
                            <xsl:when test="$t2">
                                <so:extra><xsl:value-of select="$t2" /></so:extra>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:variable>
                    <so:output>
                        <xsl:copy-of select="exsl:node-set($t1)/so:output/* | exsl:node-set($t3)/so:output/*" />
                    </so:output>
                    <xsl:copy-of select="exsl:node-set($t3)/so:extra" />
                </xsl:when>
                <xsl:when test="$n">
                    <so:extra><xsl:value-of select="$n" /></so:extra>
                </xsl:when>
            </xsl:choose>
        </xsl:template>
    
        <xsl:template name="array">
            <!-- Input like: [ X1 X2 ] bla -->
            <!-- output like: <so:output><Y>X1</Y><Y>X2</Y></so:output> <so:extra>}bla</so:extra> -->
            <xsl:param name="json-in" />
            <xsl:param name="parent-ele" />
            <xsl:variable name="t1" select="normalize-space(substring-after($json-in,'['))" />
            <xsl:variable name="t2">
                <xsl:call-template name="objects">
                    <xsl:with-param name="json-in" select="$t1" />
                    <xsl:with-param name="parent-ele" select="$parent-ele" />
                </xsl:call-template>
            </xsl:variable>  
            <xsl:variable name="t3">
                <xsl:choose>
                    <xsl:when test="contains(substring-before(exsl:node-set($t2)/so:extra,']'),',')">
                        <xsl:value-of select="normalize-space(substring-after(exsl:node-set($t2)/so:extra,','))"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="normalize-space(substring-after(exsl:node-set($t2)/so:extra,']'))"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:variable name="t4">
                <xsl:element name="{$parent-ele}">
                    <xsl:for-each select="$t2/so:output/*[local-name(.)=$parent-ele]">
                        <xsl:variable name="self" select="."/>
                        <xsl:variable name="tempResult">
                            <xsl:element name="{concat($parent-ele,'_element')}">
                                <xsl:copy-of select="exsl:node-set($self/*)" />
                            </xsl:element>
                        </xsl:variable>
                        <xsl:copy-of select="exsl:node-set($tempResult)"/>
                    </xsl:for-each>
                </xsl:element>
            </xsl:variable>
            <xsl:variable name="t5" select="exsl:node-set($t4)"/>
            <so:output>
                <xsl:copy-of select="$t5"/>
            </so:output>
            <xsl:if test="$t3">
                <so:extra><xsl:value-of select="$t3" /></so:extra>
            </xsl:if>  
        </xsl:template>
    
        <xsl:template name="value">
            <!-- Input like either array, object or string -->
            <!-- output like either array, object or string -->
            <xsl:param name="json-in" />
            <xsl:param name="parent-ele" />
            <xsl:variable name="first-letter" select="substring(normalize-space($json-in),1,1)" />
            <xsl:choose>
                <xsl:when test="$first-letter='{'">
                    <xsl:call-template name="object">
                        <xsl:with-param name="json-in" select="$json-in" />
                        <xsl:with-param name="parent-ele" select="$parent-ele" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:when test="$first-letter='['">
                    <xsl:call-template name="array">
                        <xsl:with-param name="json-in" select="$json-in" />
                        <xsl:with-param name="parent-ele" select="$parent-ele" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:when test="$first-letter=$quot">
                    <xsl:call-template name="string">
                        <xsl:with-param name="json-in" select="$json-in" />
                        <xsl:with-param name="parent-ele" select="$parent-ele" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:when test="contains($numbers,$first-letter)">
                    <xsl:call-template name="number">
                        <xsl:with-param name="json-in" select="$json-in" />
                        <xsl:with-param name="parent-ele" select="$parent-ele"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:when test="contains($booleans,$first-letter)">
                    <xsl:call-template name="boolean">
                        <xsl:with-param name="json-in" select="$json-in" />
                        <xsl:with-param name="parent-ele" select="$parent-ele"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:when test="contains($nulls,$first-letter)">
                    <xsl:call-template name="boolean">
                        <xsl:with-param name="json-in" select="$json-in" />
                        <xsl:with-param name="parent-ele" select="$parent-ele"/>
                    </xsl:call-template>
                </xsl:when>         
                <xsl:otherwise>
                    <so:output>ERROR</so:output>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    
        <xsl:template name="string">
            <!-- Input like: "X" bla -->
            <!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
            <xsl:param name="json-in" />
            <xsl:param name="parent-ele" />
            <xsl:variable name="value" select="substring-before(substring-after($json-in,$quot),$quot)" />
            <xsl:variable name="remainder" select="normalize-space(substring-after(substring-after($json-in,$quot),$quot))" />
            <so:output>
                <xsl:element name="{$parent-ele}">
                    <xsl:value-of select="$value" />
                </xsl:element>
            </so:output>
            <xsl:if test="$remainder">
                <so:extra><xsl:value-of select="$remainder" /></so:extra>
            </xsl:if>  
        </xsl:template>
    
        <xsl:template name="number">
            <!-- Input like: "X" bla -->
            <!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
            <xsl:param name="json-in" />
            <xsl:param name="parent-ele" />
            <xsl:variable name="value">
                <xsl:choose>
                    <xsl:when test="contains(substring-before($json-in,','),'}')">
                        <xsl:value-of select="normalize-space(substring-before($json-in,'}'))"/>
                    </xsl:when>
                    <xsl:when test="contains(substring-before($json-in,','),']')">
                        <xsl:value-of select="normalize-space(substring-before($json-in,']'))"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="normalize-space(substring-before($json-in,','))"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:variable name="remainder">
                <xsl:choose>
                    <xsl:when test="contains(substring-before($json-in,','),'}')">
                        <xsl:value-of select="concat('}',normalize-space(substring-after($json-in,'}')))"/>
                    </xsl:when>
                    <xsl:when test="contains(substring-before($json-in,','),']')">
                        <xsl:value-of select="concat(']',normalize-space(substring-after($json-in,']')))"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="concat(',',normalize-space(substring-after($json-in,',')))"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>  
            <so:output>
                <xsl:element name="{$parent-ele}">
                    <xsl:value-of select="$value" />
                </xsl:element>
            </so:output>
            <xsl:if test="$remainder">
                <so:extra><xsl:value-of select="$remainder" /></so:extra>
            </xsl:if>  
        </xsl:template>
        <xsl:template name="boolean">
            <!-- Input like: "X" bla -->
            <!-- output like: <so:output><Y>X</Y></so:output> <so:extra>bla</so:extra> -->
            <xsl:param name="json-in" />
            <xsl:param name="parent-ele" />
            <xsl:variable name="value">
                <xsl:choose>
                    <xsl:when test="contains(substring-before($json-in,','),'}')">
                        <xsl:value-of select="normalize-space(substring-before($json-in,'}'))"/>
                    </xsl:when>
                    <xsl:when test="contains(substring-before($json-in,','),']')">
                        <xsl:value-of select="normalize-space(substring-before($json-in,']'))"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="normalize-space(substring-before($json-in,','))"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:variable name="remainder">
                <xsl:choose>
                    <xsl:when test="contains(substring-before($json-in,','),'}')">
                        <xsl:value-of select="concat('}',normalize-space(substring-after($json-in,'}')))"/>
                    </xsl:when>
                    <xsl:when test="contains(substring-before($json-in,','),']')">
                        <xsl:value-of select="concat(']',normalize-space(substring-after($json-in,']')))"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="concat(',',normalize-space(substring-after($json-in,',')))"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>  
            <so:output>
                <xsl:element name="{$parent-ele}">
                    <xsl:value-of select="$value" />
                </xsl:element>
            </so:output>
            <xsl:if test="$remainder">
                <so:extra><xsl:value-of select="$remainder" /></so:extra>
            </xsl:if>  
        </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      【解决方案6】:

      除了 Dimitre 的 XSLT 解析框架之外,还有 Gunther Rademacher 的 Rex 解析器生成器,它还包含 JSON 作为其示例语法之一:

      http://www.bottlecaps.de/rex/

      【讨论】:

        【解决方案7】:

        试试这个库:

        https://github.com/bramstein/xsltjson

        看起来很不错。

        它是 2.0 XSLT 解决方案,虽然他也指向 1.0 版本。

        【讨论】:

          【解决方案8】:

          XSLT 有很多优点和一些大缺点。文本处理至少1.0版本是它的弱点 尽管使用 XSLT 1.0 处理该文本在技术上是可行的,但我想不出在任何情况下它会是一个非常好的主意,并且它不会是一个非常脆弱的转换。您必须生成的代码将非常笨拙。

          是否没有其他语言可供您进行处理?

          【讨论】:

          • @Woody--我也只能使用 XSLT v1.0 来做到这一点
          • 好的,好吧,正如我所说的那样,这是可能的,也是最糟糕的选择,但如果这是唯一的选择,我想你必须继续这样做。那么每个标签只有一个订单吗?情况总是一样的,脚本中的键总是一样的吗?还有其他标签吗?要使用 xslt,您必须使用非常愚蠢的文本处理并丢弃 xslt 的所有有用功能,因此您需要事先了解限制(如果格式不完全相同,它将中断)跨度>
          • @Woody--标签会被改变(不能依赖于标签名)。是否可以获得通用的xslt
          • 我不得不不同意@Woody 关于脆弱性的看法。看起来 Dimitre 的 json to xml 库函数足够通用,可以被认为是健壮的。我相信他会顺便过来谈一谈。
          • @Woody,各位,请不要对 XSLT 做出绝对的陈述。适用于 XSLT 1.0 的情况可能不适用于 XSLT 2.0 和 XSLT 3.0。事实上,XSLT 2.0 及更高版本具有非常强大的字符串处理能力,并且有完整的通用解析器 (LR-1) 框架完全用 XSLT 2.0 编写。我已经为 JSON 子集和 XPath 2.0 编写了解析器——使用我的通用 LR-1 框架——所有这些都是用纯 XSLT 2.0 编写的。当有人说“XSLT”时,我们应该认真思考我们应该理解的默认值。我相信这不再应该是 XSLT 1.0——现在这是 XSLT 2.0。
          猜你喜欢
          • 2021-07-22
          • 2020-01-26
          • 2023-03-30
          • 1970-01-01
          • 1970-01-01
          • 2019-12-28
          • 1970-01-01
          相关资源
          最近更新 更多