【问题标题】:how to restructure the xml with xslt for list type of elements to an element如何使用 xslt 将元素列表类型的 xml 重构为元素
【发布时间】:2016-12-30 15:07:32
【问题描述】:

我有一个从 JSON 到 XML 转换的以下格式的 XML。

完整的原始 XML 示例:

示例 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>

示例 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>

以下部分需要在两个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>

如何将其转换为以下格式?

1.

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

2.

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

注意:这里的所有数据(日期、值、数据、条目等)都是动态的。

寻找 XSLT 的通用实现以带来所需的输出。如果不能用 xslt,那么想去 Java 来转换它。

任何帮助将不胜感激。``

【问题讨论】:

  • 嗯,您能否详细说明一下:所有数据(日期、值、数据、条目等)在这里都是动态的。你能描述一下转换的规则吗?例如:“总是跳过第一个 XML 元素...”
  • 1. 您向我们展示的输入不是格式正确的 XML(没有单个根元素)。 -- 2.。很难相信 所有 元素名称都是“动态的”。无论如何,我们这里需要一些规则。
  • 您好 Stefan,感谢您对此进行调查。为混乱道歉。列名(“日期”、“值”)是动态的。因为我们不确定该列表中将出现多少列以及它将是什么文本。此外,下一个数组“数据”名称也不是静态的。这可以作为数据集或其他东西来。我的理解是,标题(列名列表)和数据来自 2 个列表,我们需要将这两个列表合并到一个数组或平面元素中。并从 xml 中删除单个列表。这样我们在生成 xsd 时就可以看到这些列名。
  • 嗨,迈克尔,感谢您对此进行调查。为混乱道歉。它将有一个根元素。我已经发布了一些关于上面动态数据的信息。如果您需要更多信息,请告诉我。
  • 你能提供另一个样本来展示动态性质吗?通常,像&lt;entry&gt; 这样的前几个重复节点有助于构建通用脚本。

标签: java xml xslt


【解决方案1】:

以下样式表适用于您的两个示例。我认为它不需要比这更通用 - 如果是,我不知道如何。

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>

your previous question 一样,这假定提供的列名是有效的 XML 元素名。

【讨论】:

  • 谢谢迈克尔。正确的。你在前一个问题上也帮助了我。它确实对我有用。您能否帮助将这两者结合在一起并提供最终的样式表。原始示例 xml 已发布在此处的主题中。
  • 这两个答案是不兼容的,因为两个输入非常不同:这里有一个列名列表和每行的单独值列表;在那里,您将名称和值作为一对兄弟节点。
  • 嗨迈克尔,在创建元素时,如果 column_names 值是这样的,如何用 _ 替换空格或特殊字符。 “Adj. open”、“Split Ratio”?
  • 使用translate()函数。
猜你喜欢
  • 2010-10-13
  • 2023-03-31
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-29
  • 2018-01-19
相关资源
最近更新 更多