Xml 如果节点具有相同的名称,如何使用xslt生成具有相同节点名称下的节点值的数组。output是json格式的

Xml 如果节点具有相同的名称,如何使用xslt生成具有相同节点名称下的节点值的数组。output是json格式的,xml,json,xslt,Xml,Json,Xslt,我是xslt新手。我必须使用xslt生成json,并将输入作为xml 这里我的输入xml是这样的 <root> <section> <item name="a"> <uuid>1</uuid> </item> </section> <section> <item name="b"> <uu

我是xslt新手。我必须使用xslt生成json,并将输入作为xml

这里我的输入xml是这样的

<root> 
<section>   
  <item name="a">  
       <uuid>1</uuid>  
          </item> 
   </section> 
 <section>    
 <item name="b">     
     <uuid>2</uuid>  
   </item> 
     </section> 
   </root> 
{ "root" : { "section" : { "item" :[{ "name" : "a", "uuid" : "1"},
                                    { "name" : "b", "uuid" : "2"}] }
}}
我要做的是:

查找数组中的子节点是否具有相同的名称

如果它们具有相同的名称,则生成一个数组,其中包含相同节点名称下的该节点值


输出必须是json。

一般来说,由于两种语言及其表达能力的不同,XML文档和json对象之间没有1:1的映射

也就是说,在您的具体案例中,此转换:


一般来说,由于两种语言及其表达能力的不同,XML文档和JSON对象之间没有1:1的映射

也就是说,在您的具体案例中,此转换:


正当你尝试了什么?为什么你想要的结果没有一个节数组?这是与提供的XML文档1:1对应的内容。您是否尝试用谷歌搜索XSLT到JSON?我认为用这样一个简单的问题来讨论这个任务太琐碎了。对。。。。。你尝试了什么?为什么你想要的结果没有一个节数组?这是与提供的XML文档1:1对应的内容。您是否尝试用谷歌搜索XSLT到JSON?我认为用这样一个简单的问题来讨论这项任务太微不足道了。
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vQ">"</xsl:variable>

 <xsl:template match="/*">
{ "root" : { "section" :
 <xsl:apply-templates select="section[1]/*"/>
 }}
 </xsl:template>

 <xsl:template match="item">
  { "item" :
    [<xsl:text/>
      <xsl:apply-templates select="../../*/item" mode="data"/>
     ]
   }
 </xsl:template>

 <xsl:template match="item" mode="data">
   <xsl:if test="position() > 1">, &#xA;&#9;&#9; </xsl:if>
   <xsl:text>{</xsl:text><xsl:apply-templates select="@*|*"/>}<xsl:text/>
 </xsl:template>

 <xsl:template match="item/@* | item/*">
   <xsl:if test="position() > 1">, </xsl:if>
   <xsl:value-of select='concat($vQ, name(), $vQ, " : ", $vQ, ., $vQ)'/>
 </xsl:template>
</xsl:stylesheet>
<root>
    <section>
        <item name="a">
            <uuid>1</uuid>
        </item>
    </section>
    <section>
        <item name="b">
            <uuid>2</uuid>
        </item>
    </section>
</root>
{ "root" : { "section" :

  { "item" :
    [{"name" : "a", "uuid" : "1"}, 
         {"name" : "b", "uuid" : "2"}
     ]
   }

 }}