【问题标题】:Transforming JSON Object without root name转换没有根名称的 JSON 对象
【发布时间】:2018-11-29 05:15:57
【问题描述】:

我正在尝试使用 for-each 转换我的 JSON 对象,但我没有任何根元素。这是我的对象,节点数可以更多。

[       
{
  "id": "1",
  "href": "string",
  "description": "string",
  "isBundle": true,
  "isCustomerVisible": true,
  "name": "string",
  "productSerialNumber": "string",
  "productNumber": "string",
  "startDate": "2018-11-27T13:26:22.783Z",
  "endDate": "2018-11-27T13:26:22.783Z",
  "status": "created"
},
{
  "id": "2",
  "href": "string",
  "description": "string",
  "isBundle": true,
  "isCustomerVisible": true,
  "name": "string",
  "productSerialNumber": "string",
  "productNumber": "string",
  "startDate": "2018-11-27T13:26:22.783Z",
  "endDate": "2018-11-27T13:26:22.783Z",
  "status": "created"
},
{
  "id": "3",
  "href": "string",
  "description": "string",
  "isBundle": true,
  "isCustomerVisible": true,
  "name": "string",
  "productSerialNumber": "string",
  "productNumber": "string",
  "startDate": "2018-11-27T13:26:22.783Z",
  "endDate": "2018-11-27T13:26:22.783Z",
  "status": "created"
}
]

我正在尝试使用我的 xsl 转换:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
    <xsl:template match="/">
        <jsonObject xmlns:json="http://json.org/">
            <requestResponse>
                <xsl:choose>
                    <xsl:when test="count(//id) > 0">
                        <returnCode>100</returnCode>
                        <returnMessage>SUCCESS</returnMessage>
                    </xsl:when>
                    <xsl:otherwise>
                        <returnCode>9999</returnCode>
                        <returnMessage>BUSINESS_FAULT</returnMessage>
                    </xsl:otherwise>
                </xsl:choose>
            </requestResponse>

            <xsl:for-each select="@*|node()">           
                    <xsl:if test="id">
                        <id>
                            <xsl:value-of select="/id" />
                        </id>
                    </xsl:if>
            </xsl:for-each>                     
        </jsonObject>
    </xsl:template>
</xsl:stylesheet>

如果我有我的对象的根名称,我可以处理它,但我需要一些帮助。提前感谢您的任何想法!

【问题讨论】:

  • 不确定是否要自动执行,但有一些工具可以做到这一点:oxygenxml.com/doc/versions/20.1/ug-editor/topics/…。否则,您将不得不将文档加载到一个变量中并 fn:tokenize() 它。之后,您将能够使用 for-each 循环并通过一些子字符串将您的 json 转换为 xml。
  • 在进行 XSLT 转换之前,您是否正在将 JSON 转换为 XML? XSLT 2.0 没有任何处理 JSON 输入的能力,所以我认为这是您必须要做的。然后,它完全取决于您如何将 JSON 转换为 XML:有很多库可以做到这一点,而且它们的做法都不同。如果您迁移到 XSLT 3.0,那么您可以在 XSLT 内部控制 JSON 到 XML 的转换,或者您可以在不转换为 XML 节点树的情况下本地处理 JSON。
  • @MichaelKay 是的,我没有声明,但我得到 JSON 输入,然后我映射(转换)到 XML。我可以转换如果它存在一个节点。但是这种情况我的服务会抛出 JSON 数组。所以,我不能得到像//jsonObject/id 这样的变量。
  • 您需要向我们展示为转换提供了哪些 XML,因为将 JSON 映射到 XML 有很多不同的方法。

标签: json xml xslt wso2


【解决方案1】:

在无根名称数组 json 中,您可以在 xslt 下方使用 &lt;id&gt; 标记。关键是&lt;jsonArray&gt;&lt;jsonElement&gt;&lt;/jsonElement&gt;&lt;/jsonArray&gt; 方法。但是你需要小心&lt;jsonElement&gt;,它应该在&lt;xsl:for-each select="//jsonArray/jsonElement"&gt; 块中。为了不暴露每个综合症,您不应该为其中的字段编写根元素。例如&lt;xsl:if test="id"&gt;

<xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:fn="http://www.w3.org/2005/xpath-functions"
        xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:ns1="http://www.qas.com/OnDemand-2011-03"
        xmlns:json="http://json.org">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
    <xsl:template match="/">
        <jsonArray>
            <xsl:for-each select="//jsonArray/jsonElement">
                <jsonElement>   
                    <xsl:if test="id">
                        <id><xsl:value-of select="id" /> </id>
                    </xsl:if>
                </jsonElement>
            </xsl:for-each>
        </jsonArray>
    </xsl:template>
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 2020-02-02
    • 2016-11-08
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多