如何使用 XSLT 将单个 child xml 元素转换为 Json 数组

How to convert single child xml element to Json Array using XSLT

我正在使用通用 xslt(http://www.bizcoder.com/convert-xml-to-json-using-xslt) 将 xml 转换为 json,当请求中有多个数组元素时它工作得很好,现在我想转换一个特定的 xml 元素甚至很难它有单个 child element.For 示例:

样本XML

<messages>
<message>
<to>Karim</to>
<from>Tom</from>
<heading>Reminder</heading>
<body>Please check your email !</body>
</message>
</messages>
<?xml version="1.0" encoding="UTF-8"?> 
<Response>
<Info id="10485824">
<Data tipus="11" megnevezes="APEH hátralék (rendezetlen)">
<Value num="1" subtype="xbool">false</Value>
</Data> 
</Info>
</Response>

样本JSON:


    {
        "messages": {
            "message": [{
                "to": "Karim",
                "from": "Tom",
                "heading": "Reminder",
                "body": "Please check your email !"
            }]
        }
    }

我们是否可以在 xslt 中添加任何内容以始终仅将此元素过滤为 return 作为 json 数组?

将创建数组的条件更改为

<!-- Object Properties -->
<xsl:template name="Properties">
    <xsl:variable name="childName" select="name(*[1])"/>
    <xsl:choose>
        <xsl:when test="not(*|@*)">"<xsl:value-of select="."/>"</xsl:when>
        <xsl:when test="$childName = 'message' or count(*[name()=$childName]) > 1">{ "<xsl:value-of select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when>
        <xsl:otherwise>{
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="*"/>
}</xsl:otherwise>
    </xsl:choose>
    <xsl:if test="following-sibling::*">,</xsl:if>
</xsl:template>