![](/img/trans.png)
[英]How to access the content of simple type XML element via XSLT/XPath after transforming it to JSON and back to XML?
[英]how to restructure the xml with xslt for list type of elements to an element
我有以下格式的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进行转换。
任何帮助将不胜感激.``
以下样式表适用于您的两个示例。 我认为它不需要比这更通用了-如果确实如此,我不知道怎么做。
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>
与上一个问题一样 ,这假定提供的列名称是有效的XML元素名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.