我正在进行不同的测试,这些测试有几个步骤。问题是步骤和测试是在同一个标签上(并且不可能改变它)。有一个例子会更容易(它不是原始代码):
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="new 1.xsl" type="text/xsl"?>
<root>
<file id="0">
<artist>Metallica</artist>
<album>Kill'Em All</album>
<Message>First Album</Message>
</file>
<file id="1">
<artist>Iron Maiden</artist>
<album>Piece of Mind</album>
<Message>The one with The Trooper song</Message>
</file>
<file id="2">
<artist>U2</artist>
<album>War</album>
<Message>There is a child on the cover</Message>
</file>
<file id="3">
<artist/>
<album/>
<Message>End of the year 1983</Message>
</file>
<file id="4">
<artist>Van Halen</artist>
<album>1984</album>
<Message>Hot for teacher</Message>
</file>
<file id="5">
<artist>Iron Maiden</artist>
<album>Powerslave</album>
<Message>Best</Message>
</file>
<file id="6">
<artist>Mercyful Fate</artist>
<album>Dont Break the Oath</album>
<Message>with King Diamond</Message>
</file>
<file id="7">
<artist/>
<album/>
<Message>End of the year 1984</Message>
</file>
<file id="8">
<artist>Dire Straits</artist>
<album>Brother in Arms</album>
<Message>Money for Nothing</Message>
</file>
<file id="9">
<artist>Megadeth</artist>
<album>Killing is my Business</album>
<Message>Megadave</Message>
</file>
<file id="10">
<artist/>
<album/>
<Message>End of the year 1985</Message>
</file>
<file id="11">
<artist>Metallica</artist>
<album>Master of Puppets</album>
<Message>perfect</Message>
</file>
<file id="12">
<artist>Slayer</artist>
<album>Reign in Blood</album>
<Message>so good</Message>
</file>
<file id="13">
<artist>Megadeth</artist>
<album>Peace Sells</album>
<Message>but who's buying</Message>
</file>
<file id="14">
<artist>Judas Priest</artist>
<album>Turbo</album>
<Message>Seen better</Message>
</file>
<file id="15">
<artist/>
<album/>
<Message>End of the year 1986</Message>
</file>
<file id="16">
<artist>GNR</artist>
<album>Appetite for Destruction</album>
<Message>best album ever</Message>
</file>
<file id="17">
<artist/>
<album/>
<Message>End of the year 1987</Message>
</file>
<file id="18">
<artist/>
<album/>
<Message>End of the year 1988</Message>
</file>
<file id="19">
<artist>Motley Crue</artist>
<album>Dr.Feelgood</album>
<Message>hair metal as it's root</Message>
</file>
<file id="20">
<artist>Overkill</artist>
<album>Years of Decay</album>
<Message>OK</Message>
</file>
<file id="21">
<artist/>
<album/>
<Message>End of the year 1989</Message>
</file>
</root>
正如您所看到的,测试“年末198X”与步骤'艺术家'或'专辑'的标签相同。 目标是显示Metallica是否在198X年发行了专辑。 这是我的XSLT代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="principal">
<xsl:param name="counter">0</xsl:param>
<xsl:choose>
<xsl:when test="contains(root/artist[@id='$counter'], 'Metallica')">
<xsl:template name="second-one">
<xsl:if test="not(contains(root/Message[@id='$counter'], 'year'))">
<xsl:call-template name="second-one">
<xsl:with-param name="counter" select="$counter+1"/>
</xsl:call-template>
</xsl:if>
<xsl:value-of select="root/Message[@id='$counter+1']"> Metallica released an album </xsl:value-of>
</xsl:template>
</xsl:when>
<xsl:otherwise>
<xsl:template name="second-two">
<xsl:choose>
<xsl:when test="contains(root/Message[@id='$counter'], 'year')">
<xsl:value-of select="//document[@id='$counter']/Message"/>
<td> no Metallica </td>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="second-two">
<xsl:with-param name="counter" select="$counter+1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
所以我有两个问题:
$counter
,并且在模板'second-one'和'second-two'中仍然具有相同的$ counter值吗?感谢您的回答
答案 0 :(得分:0)
xsl:with-param
is used to pass a parameter into a template 但是,模板必须预期,因此模板需要xsl:param
declaration
xsl:when
can only take sequence-constructor children (literals or instructions)和xsl:template
is a declaration,而不是指令。因此,请将该模板移至xsl:stylesheet
的孩子,向其添加param
声明,并在call-template
指令中添加适当的when
指令,它应该至少在语法上是正确的。
但是,使用计数器迭代命名模板中的元素并不是特别的自动XSLT。使用具有match
属性的模板,让XSLT按顺序处理将模板应用于适当节点的业务。
答案 1 :(得分:0)
如果您首先为每个“真实”专辑分配一年,那么您将拥有更容易使用的内容。
以下示例除了创建一个包含所有相册及其年份的表格之外什么都不做,但您也可以将结果存储在变量中,然后从那里开始工作。
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="k" match="file[string(artist) and string(album)]" use="following-sibling::file[not(string(artist) or string(album))][1]/@id" />
<xsl:template match="/root">
<table border="1">
<xsl:for-each select="file[not(string(artist) or string(album))]">
<xsl:variable name="year" select="substring-after(Message, 'End of the year ')" />
<xsl:for-each select="key('k', @id)">
<tr>
<td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="album"/></td>
<td><xsl:value-of select="$year"/></td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
<强>结果强>
<?xml version="1.0" encoding="UTF-8"?>
<table border="1">
<tr>
<td>Metallica</td>
<td>Kill'Em All</td>
<td>1983</td>
</tr>
<tr>
<td>Iron Maiden</td>
<td>Piece of Mind</td>
<td>1983</td>
</tr>
<tr>
<td>U2</td>
<td>War</td>
<td>1983</td>
</tr>
<tr>
<td>Van Halen</td>
<td>1984</td>
<td>1984</td>
</tr>
<tr>
<td>Iron Maiden</td>
<td>Powerslave</td>
<td>1984</td>
</tr>
<tr>
<td>Mercyful Fate</td>
<td>Dont Break the Oath</td>
<td>1984</td>
</tr>
<tr>
<td>Dire Straits</td>
<td>Brother in Arms</td>
<td>1985</td>
</tr>
<tr>
<td>Megadeth</td>
<td>Killing is my Business</td>
<td>1985</td>
</tr>
<tr>
<td>Metallica</td>
<td>Master of Puppets</td>
<td>1986</td>
</tr>
<tr>
<td>Slayer</td>
<td>Reign in Blood</td>
<td>1986</td>
</tr>
<tr>
<td>Megadeth</td>
<td>Peace Sells</td>
<td>1986</td>
</tr>
<tr>
<td>Judas Priest</td>
<td>Turbo</td>
<td>1986</td>
</tr>
<tr>
<td>GNR</td>
<td>Appetite for Destruction</td>
<td>1987</td>
</tr>
<tr>
<td>Motley Crue</td>
<td>Dr.Feelgood</td>
<td>1989</td>
</tr>
<tr>
<td>Overkill</td>
<td>Years of Decay</td>
<td>1989</td>
</tr>
</table>
呈现为: