我想根据XML中的内容动态地在表中创建行.在下面的代码中,我试图创建一个<tr>包含5列的row().填充5列后,我想创建一个新行.
根据以下代码,行只能包含5列.如果我在XML上应用XSL,则会显示错误
XSLT编译错误.第574行的'tr'开始标记与'xsl:when'的结束标记不匹配.第578行,第7位.
570:<table>
571: <xsl:for-each select="/alert/account_links/account_links_info">
572: <xsl:choose>
573: <xsl:when test="position() mod 5 = 1">
574: <tr>
575: <td>
576: <xsl:value-of select="account_id"/>
577: </td>
578: </xsl:when>
579: <xsl:when test="position() mod 5 = 0">
580: <td>
581: <xsl:value-of select="account_id"/>
582: </td>
583: </tr>
584: </xsl:when>
585: <xsl:otherwise>
586: <td>
587: <xsl:value-of select="account_id"/>
588: </td>
589: </xsl:otherwise>
590: </xsl:choose>
591: </xsl:for-each>
592: </table>
Run Code Online (Sandbox Code Playgroud)
输入Xml:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<alert>
<account_links>
<account_links_info>
<account_id>1</account_id>
</account_links_info>
<account_links_info>
<account_id>2</account_id>
</account_links_info>
<account_links_info>
<account_id>3</account_id>
</account_links_info>
<account_links_info>
<account_id>4</account_id>
</account_links_info>
<account_links_info>
<account_id>5</account_id>
</account_links_info>
</account_links>
</alert>
Run Code Online (Sandbox Code Playgroud)
有人能帮助我如何继续这个吗?
尝试此解决方案:
<table>
<xsl:for-each select="/alert/account_links/account_links_info[position()mod5=1]">
<xsl:variable name = "current-pos" select="(position()-1) * 5+1"/>
<tr>
<xsl:for-each select="../account_links_info[position()>=$current-pos and position() < $current-pos+5]" >
<td>
<xsl:value-of select="account_id"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
Run Code Online (Sandbox Code Playgroud)
(这个想法是让<tr>输出的外部循环每隔五分之一运行一次account_links_info element,内部循环用account_id值填充行).