【问题标题】:XSLT 1.0: Format JSON output using XSLT/ Remove defualt quotes in an JSON ArrayXSLT 1.0:使用 XSLT 格式化 JSON 输出/删除 JSON 数组中的默认引号
【发布时间】:2020-06-04 21:07:52
【问题描述】:

我在 XSLT 转换后得到以下 JSON 响应。 "name" 数组中的值需要显示为 "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 对象的当前代码。

<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 到 JSON 输出问题

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

预期的 JSON 输出

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

当我将 XML 转换为 JSON 时,会出现此 "\"ABC\",\"XYZ\"" 转义字符。有没有办法克服这个问题。

【问题讨论】:

  • 请发布minimal reproducible example 显示输入和完整的 XSLT。您当前的代码不会产生您显示的输出。
  • 嗨 Micheal,我不能分享类似的东西,因为我无法分享整个 XSLT。我知道这是否困难,我正在使用以前的有效负载,并且我想在Name 数组中接收值作为"ABC","XYZ"。我希望这能让你明白
  • 您要求我们修复我们看不到的代码。我投票结束这个问题。
  • 很抱歉,我明白了。我会关闭它,我在想如果我们将 ABC 和 XYZ 分配给一个变量并在最后连接,这样就可以实现"ABC","XYZ"
  • 生成"ABC","XYZ" 的输出没有问题 - 正如我已经展示的那样:xsltfiddle.liberty-development.net/ehW12fq。问题是您有其他代码可以转义引号。我们不知道这段代码是如何工作的,所以没有办法知道它需要什么输入来生成预期的 JSON,如果它完全可能,或者它需要如何修改以防万一。

标签: arrays json xslt xslt-1.0 xml-to-json


【解决方案1】:

我可以通过如下更改上述代码来解决此问题。

以前的代码:

<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"
            ]
        },

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多