我有一个从JSON到XML Conversion的下面表格生成的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>
以下部分需要在xmls中进行转换。
<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来转换它。
非常感谢任何帮助.`
答案 0 :(得分: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>
与your previous question一样,这假设提供的列名是有效的XML元素名称。