使用XSLT从XML生成Json文件

使用XSLT从XML生成Json文件,xml,json,xslt,Xml,Json,Xslt,我正在使用xslt将xml转换为json。我需要在转换完成后生成一个json输出文件。有没有办法做到这一点。它会更有用 我尝试了以下文件 这是我使用的xml文件,并使用xslt样式表将其解析为json sam.xml <!--?xml version="1.0" encoding="UTF-8"?--> <Customers> <Customer Id="99"> <Name>Bob</Name> <Age>

我正在使用xslt将xml转换为json。我需要在转换完成后生成一个json输出文件。有没有办法做到这一点。它会更有用

我尝试了以下文件

这是我使用的xml文件,并使用xslt样式表将其解析为json

sam.xml

<!--?xml version="1.0" encoding="UTF-8"?-->
<Customers>
<Customer Id="99">
    <Name>Bob</Name>
    <Age>39</Age>
    <Address>
        <Street>10 Idle Lane</Street>
        <City>Yucksville</City>
        <PostalCode>xxxyyy</PostalCode>
    </Address>
</Customer>
<Customer Id="101">
    <Name>Bill</Name>
    <Age>39</Age>
    <Address>
        <Street>10 Idle Lane</Street>
        <City>Yucksville</City>
        <PostalCode>xxxyyy</PostalCode>
    </Address>
</Customer>

</Customers>

上下快速移动
39
10空闲车道
尤克斯维尔
鸿飞
比尔
39
10空闲车道
尤克斯维尔
鸿飞
sam.xsl(用于解析xml的xslt表)


{
}
"" : 
""
{ "" :[] }
{
}
,
"" : "",
index.html(open将在div标记中显示json输出)


$(函数(){
$('#transformResult').xslt(“sam.xml”,”/resources/xsl/SampleXsl.xsl”);
}); 
html仅用于测试json输出。我使用了jquery插件os xslt

我想要一个json文件。我得到了div标签中的输出。但我需要的是生成带有输出的json文件。请帮助

可能的副本
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>  
     <xsl:template match="/">{
        <xsl:apply-templates select="*"/>}
    </xsl:template>  
    <!-- Object or Element Property-->
    <xsl:template match="*">
        "<xsl:value-of select="name()"/>" : <xsl:call-template name="Properties"/>
    </xsl:template>
    <!-- Array Element -->
    <xsl:template match="*" mode="ArrayElement">
        <xsl:call-template name="Properties"/>
    </xsl:template>

    <!-- 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="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>

    <!-- Attribute Property -->
    <xsl:template match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
    </xsl:template>
</xsl:stylesheet>
    <html>
    <head>
    <title></title>
    <script type="text/javascript" src="./resources/js/jquery-1.2.3.min.js"></script>
    <script type="text/javascript" src="./resources/js/jquery.xslt.js"></script>
    <script type="text/javascript">
    $(function() {
     $('#transformResult').xslt("sam.xml","./resources/xsl/SampleXsl.xsl");    
    }); 
    </script>
    </head>
    <body>
    <div id="transformResult"></div>
    </body>
    </html>