以下是我的xml:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="stylesheet2.xsl" type="text/xsl"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
<SOAP-ENV:Body>
<return>
<ActionPlanCats>
<Recommendations>
<itemname>Recommendations</itemname>
<itemId>1869</itemId>
<question>Bicep Curl</question>
<answer>23</answer>
<comment />
</Recommendations>
<Exercises>
<itemname>Exercises</itemname>
<itemId>1871</itemId>
<question>Serratus Punch</question>
<answer>Something</answer>
<comment />
</Exercises>
<RedFlags>
<itemname>Red Flags</itemname>
<itemId>1976</itemId>
<question>Immunisation</question>
<answer>three</answer>
<comment />
</RedFlags>
</ActionPlanCats>
</return>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我写了一个像这样的xslt:
<table>
<xsl:for-each select="//return/ActionPlanCats">
<xsl:for-each select="*">
<td>
<xsl:value-of select="."/>
</td>
</xsl:for-each>
</xsl:for-each>
</table>
这有效,但现在actionplancats可以有多个来自数据库的标签。我想创建一个页面,使它有2只猫(类别)并排。如果有3个类别,第一行应该是1,2,而3行将有1个全宽的列。有些人可以帮我设计一个。
当前输出:
通缉输出:(目标,红旗是类别名称)
HTML代码需要这样:
<html>
<table border="1">
<tr>
<td>Recommendations</td>
<td>Exercises</td>
</tr>
<tr>
<td>1869<br/>Bicep Curl <br/> 23</td>
<td>1871<br/>Serratus Punch <br/> Something</td>
</tr>
<tr>
<td colspan="100%">Red Flags</td>
</tr>
<tr>
<td colspan="100%">1976 <br/> Immunization <br/> three </td>
</tr>
</table>
</html>
答案 0 :(得分:2)
不要像使用<xsl:for-each select="*">
一样循环遍历 ActionPlanCats 的当前子元素,而只需选择将成为每行开头的子元素,就像这样
<xsl:for-each select="*[position() mod 2 = 1]">
每个子元素实际上会产生两行,一个标题行(显示 itemname )和一个详细行(显示其他记录)。您可以使用 xsl:apply-templates 为每行选择单元格,但您需要使用模式参数来区分它们。
<tr>
<xsl:apply-templates select="self::*|following-sibling::*[1]" mode="header"/>
</tr>
<tr>
<xsl:apply-templates select="self::*|following-sibling::*[1]" mode="body"/>
</tr>
请注意,如果没有后续兄弟(如第三个元素的情况),则仅选择当前元素。然后,在&#34;标题&#34;例如,您可以检查是否存在以下兄弟,以确定是否添加colspan。
<xsl:template match="ActionPlanCats/*" mode="header">
<td>
<xsl:if test="not(following-sibling::*)">
<xsl:attribute name="colspan">2</xsl:attribute>
</xsl:if>
<xsl:value-of select="itemname"/>
</td>
</xsl:template>
试试这个XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<table>
<xsl:apply-templates select="//return/ActionPlanCats"/>
</table>
</xsl:template>
<xsl:template match="ActionPlanCats">
<xsl:for-each select="*[position() mod 2 = 1]">
<tr>
<xsl:apply-templates select="self::*|following-sibling::*[1]" mode="header"/>
</tr>
<tr>
<xsl:apply-templates select="self::*|following-sibling::*[1]" mode="body"/>
</tr>
</xsl:for-each>
</xsl:template>
<xsl:template match="ActionPlanCats/*" mode="header">
<td>
<xsl:if test="not(following-sibling::*)">
<xsl:attribute name="colspan">2</xsl:attribute>
</xsl:if>
<xsl:value-of select="itemname"/>
</td>
</xsl:template>
<xsl:template match="ActionPlanCats/*" mode="body">
<td>
<xsl:if test="not(following-sibling::*)">
<xsl:attribute name="colspan">2</xsl:attribute>
</xsl:if>
<xsl:value-of select="itemId"/><br/>
<xsl:value-of select="question"/><br/>
<xsl:value-of select="answer"/> <br/>
<xsl:value-of select="comment"/><br/>
</td>
</xsl:template>
</xsl:stylesheet>