XSLT 1.0:使用XSLT格式化JSON输出格式/删除JSON数组中的反引号 [英] XSLT 1.0: Format JSON output using XSLT/ Remove defualt quotes in an JSON Array

查看:160
本文介绍了XSLT 1.0:使用XSLT格式化JSON输出格式/删除JSON数组中的反引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下JSON响应,这是XSLT转换后得到的。 名称 数组中的值需要显示为 ABC, XYZ

I have a the following JSON response, that I'm getting after a XSLT transformation. The values in the "name" array needed to be present as "ABC","XYZ"

有效负载

<Data>
 <Mapping>
   <LocationID>001</LocationID>
   <GeoX>1.00</GeoX>
   <GeoY>2.00</GeoY>
 </Mapping>
 <Mapping>
   <LocationID>002</LocationID>
   <GeoX>56.00</GeoX>
   <GeoY>42.00</GeoY>
 <Mapping>
</Data>

当前代码,其中实现了 Destination 对象

Current Code where the Destination object is implemented.

<xsl:template match="//Data">
   <Destination>
      <Locations>
          <xsl:text disable-output-escaping="yes">&lt;?xml-multiple?&gt;</xsl:text>
            <Name>
             <jsonArray>
               <xsl:for-each select="Mapping">
                  <xsl:choose>
                     <xsl:when test="LocationID='001'">"ABC"</xsl:when>
                     <xsl:when test="LocationID='002'">"XYZ"</xsl:when>
                     <xsl:otherwise>"NEW"</xsl:otherwise>
                  </xsl:choose>
                  <xsl:if test="position()!=last()">,</xsl:if>
               </xsl:for-each>
              </jsonArray>
            </Name>
        </Locations>
    </Destination>
</xsl:template>

XML输出

<Destination>
  <Locations>
    <Name>"ABC","XYZ"</Name>
  </Locations>
</Destination>

问题XML-to-JSON输出

"Destination": [
    {
        "Locations": {
            "Name": [
                "\"ABC\",\"XYZ\""
            ]
        },

期望的JSON输出

"Destination": [
    {
        "Locations": {
            "Name": [
                "ABC","XYZ"
            ]
        },

当我将XML转换为JSON时,会出现此 \ ABC\,\ XYZ\转义字符。

This "\"ABC\",\"XYZ\"" escape characters happen when i'm converting the XML to JSON. Is there a way to overcome this.

推荐答案

我能够通过更改以下代码来解决此问题。

I was able to resolve this issue by changing the above code as following.

上一个代码:

<xsl:template match="//Data">
   <Destination>
      <Locations>
          <xsl:text disable-output-escaping="yes">&lt;?xml-multiple?&gt;</xsl:text>
            <Name>
             <jsonArray>
               <xsl:for-each select="Mapping">
                  <xsl:choose>
                     <xsl:when test="LocationID='001'">"ABC"</xsl:when>
                     <xsl:when test="LocationID='002'">"XYZ"</xsl:when>
                     <xsl:otherwise>"NEW"</xsl:otherwise>
                  </xsl:choose>
               </xsl:for-each>
              </jsonArray>
            </Name>
        </Locations>
    </Destination>
</xsl:template>

已更改代码:

<xsl:template match="//Data">
   <Destination>
      <Locations>
               <xsl:for-each select="Mapping">
                  <xsl:choose>
                     <xsl:when test="LocationID='001'"><Name>ABC</Name></xsl:when>
                     <xsl:when test="LocationID='002'"><Name>XYZ</Name></xsl:when>
                     <xsl:otherwise><Name>NEW</Name></xsl:otherwise>
                  </xsl:choose>
               </xsl:for-each>
        </Locations>
    </Destination>
</xsl:template>

输出

"Destination": [
    {
        "Locations": {
            "Name": [
                "ABC",
                "XYZ"
            ]
        },

这篇关于XSLT 1.0:使用XSLT格式化JSON输出格式/删除JSON数组中的反引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆