More generalized solution. Requires XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<xsl:element name="json:object">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="*[*]">
<xsl:param name="nodeName" select="name()" />
<xsl:variable name="firstNodeName" select="name(*[1])" />
<xsl:element name="json:object">
<xsl:if test="$nodeName">
<xsl:attribute name="name" select="$nodeName" />
</xsl:if>
<xsl:choose>
<xsl:when test="(count(*) > 1) and (every $x in */name() satisfies $x=$firstNodeName)">
<xsl:element name="json:array">
<xsl:attribute name="name" select="$firstNodeName" />
<xsl:apply-templates >
<xsl:with-param name="nodeName" select="''" />
</xsl:apply-templates>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates />
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
<xsl:template match="*[not(*)]">
<xsl:element name="json:string">
<xsl:attribute name="name">
<xsl:value-of select="name()" />
</xsl:attribute>
<xsl:value-of select="text()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Applied to the provided sample XML, produces following output:
<?xml version="1.0" encoding="UTF-8"?>
<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
<json:object name="Login">
<json:object name="Groups">
<json:array name="Group">
<json:object>
<json:string name="Name">john</json:string>
<json:string name="Password"></json:string>
</json:object>
<json:object>
<json:string name="Name">john</json:string>
<json:string name="Password"></json:string>
</json:object>
</json:array>
</json:object>
</json:object>
</json:object>
The above solution was tested on this site: http://xslttest.appspot.com/
EDIT:
every $x in */name() satisfies $x=$firstNodeName
is XPATH 2.0 construction that checks if all the elements in */name()
sequence are equal to $firstNodeName. So this whole condition actually means checking if a node has more than one child with the same name - this is a condition for checking if we're dealing with json:array
case.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…