我很难用xsl和xml对每个进行折叠。 这是我的xml文件:
<a>
<b>
<c name="namec1">
<d name="named1">value1</d>
<d name="named2">value2</d>
<d name="named3">value3</d>
<d name="named4">value4</d>
</c>
<c name="namec1">
<d name="named1">value5</d>
<d name="named2">value6</d>
<d name="named3">value7</d>
<d name="named4">value8</d>
</c>
<c name="namec1">
<d name="named1">value9</d>
<d name="named2">value10</d>
<d name="named3">value11</d>
<d name="named4">value12</d>
</c>
</b>
</a>
这就是我想要的:
namec: [namec1, namec2, namec3]
named: [value1, value5, value9]
named: [value2, value6, value10]
named: [value3, value7, value11]
named: [value4, value8, value12]
这是我的xsl代码:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="index" select="." />
<html>
<body>
namec: [
<xsl:for-each select="a/b">
<xsl:choose>
<xsl:when test="position()=1">
<xsl:value-of select='c/@name'/>
</xsl:when>
<xsl:otherwise>
, <xsl:value-of select='d/@name'/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
]
<xsl:for-each select="a/b">
<xsl:if test="position()=1">
<xsl:for-each select="c/d">
named: <xsl:value-of select="@name" />,
data: [
<xsl:for-each select="$index/a/b/c">
<xsl:value-of select="d" />,
</xsl:for-each>
]
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
第一个for-each是好的 但我真的不明白为什么第二次崩溃 - 每个都没有用 我尝试过,但我是新来的...... 它甚至可能吗? 提前谢谢!
答案 0 :(得分:0)
我尝试将this answer应用于我的案例,但确实有效:
xml:
<table>
<tr name="date1">
<td name="var1">A11</td>
<td name="var2">A12</td>
<td name="var3">A13</td>
<td name="var4">A14</td>
<td name="var5">A15</td>
</tr>
<tr name="date2">
<td name="var1">A21</td>
<td name="var2">A22</td>
<td name="var3">A23</td>
<td name="var4">A24</td>
<td name="var5">A25</td>
</tr>
<tr name="date3">
<td name="var1">A31</td>
<td name="var2">A32</td>
<td name="var3">A33</td>
<td name="var4">A34</td>
<td name="var5">A35</td>
</tr>
</table>
和xsl:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
categories: [
<xsl:for-each select="table/tr">
<xsl:choose>
<xsl:when test="position()=1">
'<xsl:value-of select='@name'/>'
</xsl:when>
<xsl:otherwise>
, '<xsl:value-of select='@name'/>'
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
]
<xsl:for-each select="table/tr[1]/td">
<xsl:variable name="vRowPos" select="position()"/>
<xsl:for-each select="/table/tr">
<xsl:variable name="vColPos" select="position()"/>
<xsl:choose>
<xsl:when test="position()=1">
name: '<xsl:value-of select="/table/tr[$vColPos]/td[$vRowPos]/@name" />',
data: ['<xsl:value-of select="/table/tr[$vColPos]/td[$vRowPos]" />'
</xsl:when>
<xsl:otherwise>
, '<xsl:value-of select="/table/tr[$vColPos]/td[$vRowPos]" />'
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>]
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
它让我回答:
categories: ['date1', 'date2', 'date3']
name: 'var1',
data: ['A11', 'A21', 'A31']
name: 'var2',
data: ['A12', 'A22', 'A32']
name: 'var3',
data: ['A13', 'A23', 'A33']
name: 'var4',
data: ['A14', 'A24', 'A34']
name: 'var5',
data: ['A15', 'A25', 'A35']