我正在尝试遍历几个输入文件,在HTML输出文件中创建一个表,其中每一行对应一个评论,但是我无法获得相应的@date值并将其复制到相应的单元格中桌子。非常感谢任何帮助。
XML输入文件
档案1
<review rid="id_001" date="2018-03-20">
<pc pid="pc_001">
<result>...</result>
</pc>
<pc pid="pc_002">
<result>...</result>
</pc>
</review>
文件2
<review rid="id_002" date="2018-05-19">
<pc pid="pc_001">
<result>...</result>
</pc>
<pc pid="pc_002">
<result>...</result>
</pc>
</review>
所需的HTML输出
<!-- @pid value outside the table as header. -->
<table>
<tr>
<td>Review ID</td>
<td>Date</td>
<td>Result</td>
</tr>
<tr>
<td>001</td>
<td>2018-03-20</td>
<td>...</td>
</tr>
<td>002</td>
<td>2018-05-19</td>
<td>...</td>
</tr>
</table>
XSLT
<xsl:variable name="files" select="collection('../?select=*.xml')"/>
<xsl:template match="/">
<!-- @pid value outside the table as header. -->
<table>
<tr>
<td>Review ID</td>
<td>Date</td>
<td>Result</td>
</tr>
<xsl:if test="$files">
<xsl:for-each select="distinct-values($files/review/@rid)">
<xsl:variable name="currentRid" select="distinct-values($files/review/@rid)"/>
<xsl:variable name="currentPid" select="distinct-values($files/review/pc/@pid)"/>
<tr>
<td>
<xsl:value-of select="."/>
</td>
<td>
<xsl:for-each select="distinct-values($files/review/@date)">
<xsl:value-of select="."/>
</xsl:for-each>
</td>
<td>
<xsl:value-of select="distinct-values($files/review[@rid=$currentRid]/pc[@pid=$currentPid]/result)"/>
</td>
</tr>
</xsl:for-each>
</xsl:if>
</tr>
</table>
</xsl:template>
答案 0 :(得分:0)
问题在于您尝试迭代所有 review
元素的@date
值。将for-each
表达式限制为与review
current()
匹配的rid
元素,就像您在下一个xsl:value-of
表达式中所做的那样,可以获得所需的输出。所以改变
<xsl:for-each select="distinct-values($files/review/@date)">
到
<xsl:for-each select="distinct-values($files/review[@rid=current()]/@date)">
我无法估计xsl:for-each
是否有必要 - 这取决于更大的图片 - 但可能以下表达式就足够了
<xsl:value-of select="$files/review[@rid=current()]/@date"/>