简体   繁体   English

如何使用xslt将元素的列表类型重构为xml

[英]how to restructure the xml with xslt for list type of elements to an element

i have an XML generated in the below form from JSON to XML Conversion. 我有以下格式的JSON到XML转换生成的XML。

Full Original XML examples: 完整的原始XML示例:

Example1: 范例1:

<?xml version="1.0" encoding="UTF-8"?>
<linked-hash-map>
   <entry>
      <string>dataset</string>
      <linked-hash-map>
         <entry>
            <string>id</string>
            <string>120140</string>
         </entry>
         <entry>
            <string>dataset_code</string>
            <string>GDP</string>
         </entry>
         <entry>
            <string>column_names</string>
            <list>
               <string>DATE</string>
               <string>VALUE</string>
            </list>
         </entry>
         <entry>
            <string>frequency</string>
            <string>quarterly</string>
         </entry>
         <entry>
            <string>type</string>
            <string>Time Series</string>
         </entry>
         <entry>
            <string>premium</string>
            <boolean>false</boolean>
         </entry>
         <entry>
            <string>data</string>
            <list>
               <list>
                  <string>2016-07-01</string>
                  <double>18675.3</double>
               </list>
               <list>
                  <string>2016-04-01</string>
                  <double>18450.1</double>
               </list>
               <list>
                  <string>2016-01-01</string>
                  <double>18281.6</double>
               </list>
            </list>
         </entry>
         <entry>
            <string>database_id</string>
            <int>118</int>
         </entry>
      </linked-hash-map>
   </entry>
</linked-hash-map>

Example 2: 范例2:

<?xml version="1.0" encoding="UTF-8"?>
<linked-hash-map>
   <entry>
      <string>dataset</string>
      <linked-hash-map>
         <entry>
            <string>dataset_code</string>
            <string>AAPL</string>
         </entry>
         <entry>
            <string>column_names</string>
            <list>
               <string>DATE</string>
               <string>Open</string>
               <string>High</string>
               <string>Low</string>
               <string>Close</string>
            </list>
         </entry>
         <entry>
            <string>frequency</string>
            <string>quarterly</string>
         </entry>
         <entry>
            <string>type</string>
            <string>Time Series</string>
         </entry>
         <entry>
            <string>data</string>
            <list>
               <list>
                  <string>2016-07-01</string>
                  <double>116.45</double>
                  <double>117.1095</double>
                  <double>116.4</double>
                  <double>116.73</double>
               </list>
               <list>
                  <string>2016-04-01</string>
                  <double>18450.1</double>
                  <double>113.1095</double>
                  <double>112.4</double>
                  <double>100.73</double>
               </list>
               <list>
                  <string>2016-01-01</string>
                  <double>18281.6</double>
                  <double>157.1095</double>
                  <double>136.4</double>
                  <double>156.73</double>
               </list>
            </list>
         </entry>
         <entry>
            <string>database_id</string>
            <int>218</int>
         </entry>
      </linked-hash-map>
   </entry>
</linked-hash-map>

the below part needs to be converted in both the xmls. 下面的部分需要在两个xml中都进行转换。

<entry>
       <string>column_names</string>
        <list>
          <string>DATE</string>
          <string>VALUE</string>
        </list>
</entry>
     <entry>
            <string>data</string>
            <list>
              <list>
                <string>2016-07-01</string>
                <double>18675.3</double>
              </list>
              <list>
                <string>2016-04-01</string>
                <double>18450.1</double>
              </list>
             </list>
    </entry>

How to convert this into the below formats? 如何将其转换为以下格式?

1. 1。

  <entry>
       <Date>2016-07-01</Date>
            <Value>18675.3</Value>
        </entry>
        <entry>
            <Date>2016-04-01</Date>
            <Value>18450.1</Value>
         </entry>

2. 2。

<entry>
    <Date>2016-07-01</Date>
    <Value>18675.3</Value>
    <Date>2016-04-01</Date>
    <Value>18450.1</Value>
    </entry>

Note: All the data(Date, Value,data,entry,etc) are dynamic here. 注意:所有数据(日期,值,数据,条目等)在此处都是动态的。

Looking for a generic implementation of XSLT to bring the desired output. 寻找XSLT的通用实现以带来所需的输出。 if not possible with xslt, then would like to go for Java to convert this. 如果无法使用xslt,则希望Java进行转换。

Any help would be greatly appreciated.`` 任何帮助将不胜感激.``

The following stylesheet will work for both your examples. 以下样式表适用于您的两个示例。 I don't think it needs to be any more generic than that - and if it does, I don't see how. 我认为它不需要比这更通用了-如果确实如此,我不知道怎么做。

XSLT 1.0 XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/linked-hash-map">
    <xsl:variable name="col-names" select="entry/linked-hash-map/entry[string='column_names']/list/string" />
    <root>
        <xsl:for-each select="entry/linked-hash-map/entry[string='data']/list/list">
            <xsl:variable name="current-row" select="." />
            <entry>
                <xsl:for-each select="$col-names">
                    <xsl:variable name="i" select="position()" />
                    <xsl:element name="{.}">
                        <xsl:value-of select="$current-row/*[$i]" />
                    </xsl:element>
                </xsl:for-each>
            </entry>
        </xsl:for-each>
    </root>
</xsl:template>

</xsl:stylesheet>

As with your previous question , this assumes that the supplied column names are valid XML element names. 上一个问题一样 ,这假定提供的列名称是有效的XML元素名称。

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

相关问题 在将简单类型的XML元素转换为JSON并返回XML之后,如何通过XSLT / XPath访问简单类型的XML元素的内容? - How to access the content of simple type XML element via XSLT/XPath after transforming it to JSON and back to XML? Java的。 抽象元素列表。 如何确定元素类型 - Java. Abstract elements list. How to determine element type 如何根据一些规则重组XML文件? - How to restructure an XML file based on some rules? 如何从XSLT转换中过滤XML节点内的元素? - How to filter elements inside a XML node from transforming using XSLT? 如何在XSLT 1.0中匹配和处理未知的XML元素? - How to match and process unknown XML elements in XSLT 1.0? XSLT 1.0:如何在其他模板周围包装XML元素? - XSLT 1.0: how to wrap XML element around other templates? 如何使用XSLT从XML生成完整的列表和选定的详细信息? - How to generate full List and selected Details from XML using XSLT? 访问具有多个相同类型元素的XML文件中的元素 - Accessing an element in an XML file that has multiple elements of the same type 从XML文档,XSLT和JAXB中删除元素 - Removing elements from an XML document, XSLT and JAXB 在XSLT中,如果某个XML文件包含在xinclude中,我如何获取该元素的xml文件的文件路径? - In XSLT, how do I get the filepath of the xml file of a certain element if that xml file was included with xinclude?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM