Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

convert xml to jsonx using xslt

Can anyone help me out in getting the below with an array..I have to generate the generalised xsl.. Input XML:

<Login>
    <Groups>
        <Group>
            <Name>john</Name>
            <Password/>
        </Group>
        <Group>
            <Name>john</Name>
            <Password/>
        </Group>
    </Groups>
</Login>

Output:

<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <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:object>
                <json:object>
                    <json:string name="Name">john</json:string>
                    <json:string name="Password"/>
                </json:object>
            </json:array>
        </json:object>
    </json:object>
</json:object>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...