使用 XSLT 生成 html 表的多个动态行
我想根据 XML 中的内容动态地在表中创建行。在下面的代码中,我尝试创建一行(),其中包含 5 列。填充 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>
输入 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>
有人可以帮助我如何继续吗?
I want to create rows in a table dynamically depending on the content in the XML. In the below code I am trying to create a row(<tr>
) with 5 columns. After 5 columns are filled, I want to create a new row.
A row can only contain 5 columns as per the below code. If i apply the XSL on the XML, I get an error displaying
XSLT compile error. The 'tr' start tag on line 574 does not match the end tag of 'xsl:when'. Line 578, position 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>
Input 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>
Can some one help me how to go ahead with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这个解决方案:(
其想法是为
输出设置一个外部循环,遍历每五个
account_links_info 元素
,并使用一个内部循环填充行account_id
值)。Try this solution:
(the idea is to have an outer loop for the
<tr>
output running through every fifthaccount_links_info element
, and an inner loop filling the rows with theaccount_id
values).XSLT 指令在结果树上生成节点,而不是词法开始和结束标记。输出一个节点是一个单一的操作,你不能将其分成两个操作,每个操作写入半个节点。因此,您的想法应该是“对于输入中的每五个节点,我想在输出中生成一个节点”,这自然会导致诸如“
不要被引诱为此使用禁用输出转义”的构造。这是毒药。它打破了转换引擎和序列化之间的清晰架构边界,这意味着您的样式表无法在任意管道中干净地部署和重用(这就是它在 Firefox 中不起作用的原因,如果您感兴趣的话)的实际后果)。
XSLT instructions produce nodes on a result tree, not lexical begin and end tags. Outputting a node is a single operation, you can't divide it into two operations writing half a node each. So your thinking should be "for every five nodes in the input I want to produce a node in the output", which leads naturally to a construct such as
Don't be seduced into using disable-output-escaping for this. It's poison. It breaks the clean architectural boundary between the transformation engine and the serialized, which means that your stylesheet can't be cleanly deployed and reused in an arbitrary pipeline (which is the reason it won't work in Firefox, in case you're interested in the practical consequences).
由于您在不同位置设置
和
标记,因此必须将它们输出为文本以使其保持 XSL 文件的有效 XML。另外,当它是表中的最后一个元素时,您应该输出
标记,否则最后一行可能不会关闭:
Because you are setting the
<tr>
and</tr>
tags in different locations, you must output them as text to keep it valid XML for the XSL file. Also, you should output the</tr>
tag when it is the last element in the table or the last row may not be closed: