我有一个xml文件如下所示,我希望从特定的日期(2017-01-01,在我的示例中)获得评分。但问题是他们中的一些人有多种费率,他们应该全部横向出来。格式没有问题,我可以根据xsl:stylesheet version="1.0"
得到Rate1。但是我如何获得Rate2和Rate3?请帮助,这是一个例子。
这是xml:
<Table>
<TableName>Table A</TableName>
<Details>
<Date>2016-01-01</Date>
<Rate>0.01</Rate>
</Details>
<Details>
<Date>2017-01-01</Date>
<Rate>0.01</Rate>
</Details>
</Table>
<Table>
<TableName>Table B</TableName>
<Details>
<Date>2016-01-01</Date>
<Rate>0.02</Rate>
</Details>
<Details>
<Date>2017-01-01</Date>
<Rate>0.02</Rate>
</Details>
</Table>
<Table>
<TableName>Table C</TableName>
<Details>
<Date>2016-01-01</Date>
<Rate>0.03</Rate>
</Details>
<Details>
<Date>2016-01-01</Date>
<Rate>0.04</Rate>
</Details>
<Details>
<Date>2016-01-01</Date>
<Rate>0.05</Rate>
</Details>
<Details>
<Date>2017-01-01</Date>
<Rate>0.03</Rate>
</Details>
<Details>
<Date>2017-01-01</Date>
<Rate>0.04</Rate>
</Details>
<Details>
<Date>2017-01-01</Date>
<Rate>0.05</Rate>
</Details>
</Table>
这是XSL:
<!--I have a variable defined $Date20170101 to get 2017-01-01-->
<xsl:if test="Details/Date = $Date20170101">
<tr>
<th>
<xsl:value-of select="TableName"/>
</th>
<td>
<!--"format-date" is to format the date to mm/dd/yyyy-->
<xsl:call-template name="format-date">
<xsl:with-param name="date">
<xsl:value-of select="Details/Date"/>
</xsl:with-param>
</xsl:call-template>
</td>
<!--Below is the problem I have-->
<xsl:if test="(Details/Rate)[1]">
<td>
<xsl:value-of select="(Details/Rate)[1]"/>
</td>
</xsl:if>
<xsl:if test="(Details/Rate)[2]">
<td>
<xsl:value-of select="(Details/Rate)[2]"/>
</td>
</xsl:if>
<xsl:if test="(Details/Rate)[3]">
<td>
<xsl:value-of select="(Details/Rate)[2]"/>
</td>
</xsl:if>
</tr>
</xsl:if>
以下是预期结果:
Table Name Date Rate1 Rate2 Rate3
Table A 1/1/2017 0.01 N/A N/A
Table B 1/1/2017 0.02 N/A N/A
Table C 1/1/2017 0.03 0.04 0.05
答案 0 :(得分:1)
最高费率列将是3而不是更多
那么,它可以很简单:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:param name="given-date">2017-01-01</xsl:param>
<xsl:template match="/root">
<table border="1">
<tr>
<th>Table Name</th>
<th>Date</th>
<th>Rate1</th>
<th>Rate2</th>
<th>Rate3</th>
</tr>
<xsl:for-each select="Table">
<tr>
<td>
<xsl:value-of select="TableName"/>
</td>
<td>
<xsl:value-of select="$given-date"/>
</td>
<xsl:variable name="details" select="Details[Date=$given-date]" />
<td>
<xsl:value-of select="$details[1]/Rate"/>
</td>
<td>
<xsl:value-of select="$details[2]/Rate"/>
</td>
<td>
<xsl:value-of select="$details[3]/Rate"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
当这适用于格式正确(!) XML输入时,例如:
<强> XML 强>
<root>
<Table>
<TableName>Table A</TableName>
<Details>
<Date>2016-01-01</Date>
<Rate>0.01</Rate>
</Details>
<Details>
<Date>2017-01-01</Date>
<Rate>0.01</Rate>
</Details>
</Table>
<Table>
<TableName>Table B</TableName>
<Details>
<Date>2016-01-01</Date>
<Rate>0.02</Rate>
</Details>
<Details>
<Date>2017-01-01</Date>
<Rate>0.02</Rate>
</Details>
</Table>
<Table>
<TableName>Table C</TableName>
<Details>
<Date>2016-01-01</Date>
<Rate>0.03</Rate>
</Details>
<Details>
<Date>2016-01-01</Date>
<Rate>0.04</Rate>
</Details>
<Details>
<Date>2016-01-01</Date>
<Rate>0.05</Rate>
</Details>
<Details>
<Date>2017-01-01</Date>
<Rate>0.03</Rate>
</Details>
<Details>
<Date>2017-01-01</Date>
<Rate>0.04</Rate>
</Details>
<Details>
<Date>2017-01-01</Date>
<Rate>0.05</Rate>
</Details>
</Table>
</root>
结果将是:
<table border="1">
<tr>
<th>Table Name</th>
<th>Date</th>
<th>Rate1</th>
<th>Rate2</th>
<th>Rate3</th>
</tr>
<tr>
<td>Table A</td>
<td>2017-01-01</td>
<td>0.01</td>
<td/>
<td/>
</tr>
<tr>
<td>Table B</td>
<td>2017-01-01</td>
<td>0.02</td>
<td/>
<td/>
</tr>
<tr>
<td>Table C</td>
<td>2017-01-01</td>
<td>0.03</td>
<td>0.04</td>
<td>0.05</td>
</tr>
</table>
呈现为: