繁体   English   中英

xslt转换为json

[英]xslt to convert to json

输入我得到:

  <table>   
    <source1>
      <ptr>
          <data name="text1">20431</data>
          <data name="text2">collins</data>
          <data name="text3">20141231></data>
          <data name="text4">regard</data>
      </ptr>
    </source1>
    <source2>
        <ptr>
            <data name="note1">C</data>
            <data name="note2">A</data>
            <data name="note3">22356</data>
            <data name="note4">465506</data>
            <data name="note5">434562491057912</data>
            <data name="note6">milk</data>
            <data name="note7">dfRTA</data>
        </ptr>
    </source2>
    <source3>
    <enroll>n</enroll>
    </source3>
    </table>

需要将其转换为json作为

{
"table":
{
"source1":
{"text1":"20431",
"text2": "collins",
"text3": "20141231",
"text4" :"regard"
},
"source2":
{"note1":"20431",
"note2": "A",
"note3":"22356",
"note4":"465506",
"note5":"434562491057912",
"note6":"milk",
"note7":"dfRTA"
}
}}

如何使用xsl转换任何通用变量而不是在每个属性名称上进行匹配。 任何人都有样式表可以进行上述转换

正如评论中已经提到的那样, XSLT通常是生成JSON的错误工具 但是,如果您已经检查了提供的链接,并且仍然想测试它是否因您的要求或其他原因而起作用,那么遵循XSLT生成一个文本文件,对于您的示例输入XML,该文本文件是有效的JSON格式:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" doctype-public="XSLT-compat" 
     omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
  <xsl:template match="table">
    <xsl:text>{ "table" : {</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>}}</xsl:text>
  </xsl:template>
  <xsl:template match="*[starts-with(local-name(),'source') and .//data]">
    <xsl:text>"</xsl:text>
    <xsl:value-of select="local-name()"/>
    <xsl:text>" :</xsl:text>
    <xsl:text>{</xsl:text>
    <xsl:apply-templates select="@*|node()"/>
    <xsl:text>}</xsl:text>
    <xsl:if test="following-sibling::*[starts-with(local-name(),'source') 
                                            and .//data]">
        <xsl:text>, </xsl:text>
    </xsl:if>
  </xsl:template>
  <xsl:template match="data">
    <xsl:text>"</xsl:text>
    <xsl:value-of select="@name"/>
    <xsl:text>" : "</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>"</xsl:text>
    <xsl:if test="following-sibling::data">
        <xsl:text>, </xsl:text>
    </xsl:if>
  </xsl:template>
  <xsl:template match="*[starts-with(local-name(),'source') 
                              and not(.//data)]"/>
</xsl:transform>

应用于输入XML时,将产生以下输出:

{ "table" : {   
"source1" :{

      "text1" : "20431", 
      "text2" : "collins", 
      "text3" : "20141231>", 
      "text4" : "regard"
}, 
"source2" :{

        "note1" : "C", 
        "note2" : "A", 
        "note3" : "22356", 
        "note4" : "465506", 
        "note5" : "434562491057912", 
        "note6" : "milk", 
        "note7" : "dfRTA"
}
}}

尽管从输入中不能完全清楚,但您在注释中提到所有数据元素都需要转换,因此我排除了每个父元素,这些父元素的名称以source开头,并且没有带有名称data子节点( source3中为source3 ),并应用了最后使用空模板将其从输出中删除。
请注意,为了提高性能,可以根据输入来改进检查source节点是否包含任何data节点的表达式-例如,如果已知包含data源始终将其包装在ptr ,则最好调整匹配模式,例如<xsl:template match="*[starts-with(local-name(),'source') and .//data]"><xsl:template match="*[starts-with(local-name(),'source') and ptr]"> (或ptr/data ,以防出现空的ptr元素)。
如果要从输出中删除不必要的空间,可以在<xsl:output>下面添加<xsl:strip-space elements="*"/> ,以将结果文本/“ JSON”放在一行中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM