Xml XSL:尝试对不同的模板使用相同的计数器
我正在进行不同的测试,这些测试有几个步骤。问题是,步骤和测试处于同一标签上,不可能改变它。有一个示例会更容易,因为它不是原始代码:Xml XSL:尝试对不同的模板使用相同的计数器,xml,xslt,Xml,Xslt,我正在进行不同的测试,这些测试有几个步骤。问题是,步骤和测试处于同一标签上,不可能改变它。有一个示例会更容易,因为它不是原始代码: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="new 1.xsl" type="text/xsl"?> <root> <file id="0"> <artist>Metallica</artist&
<?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年的测试结束与steps的“艺术家”或“专辑”处于同一标签上。
目标是显示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>
所以我有两个问题:
我是否可以在模板“principal”中使用计数器$counter,并且在模板“second one”和“second two”中仍然具有相同的$counter值?
Editix说我不能使用第5行,我不知道为什么。
谢谢你的回答
是的,你可以,
但是,模板必须预期它,因此模板需要
第5行失败的原因可能是由于,而不是指令。
因此,将该模板移到xsl:stylesheet的子级,向其添加param声明,并向when指令添加适当的call-template指令,它至少在语法上应该是正确的
但是,使用计数器在命名模板中的元素上迭代并不是一种特殊的idomatic XSLT。改为使用具有匹配属性的模板,并让XSLT处理将模板按顺序应用到适当节点的业务。如果您开始为每个“真实相册”分配一年,您将拥有更易于使用的内容 下面的示例只创建一个包含所有相册及其年份的表,但您也可以将结果存储在一个变量中,然后从那里开始工作 XSLT1.0 结果 呈现为:
目标是显示Metallica是否在198X年发行了一张专辑。这到底是什么意思?x是已知的吗?如果是,它来自哪里?或者你只是想看看他们在那十年里有没有专辑?简单地说,如果Metallica在1983年或1984年发行了一张专辑,19851986198719881989…假设条目是按时间顺序排列的,对吗,而那些没有艺术家或专辑的是标志着每年结束的标记另外,我仍然不完全理解你的目标:你的名单似乎只涵盖1983-1989年,所以你只需要看看其中是否有Metallica。这是正确的!这只是我很快做出的一个例子。这里的问题是,所有的“文件”都是根目录的子目录,不管是对专辑的描述还是年终消息。那么,当节目看到《Metallica》时,怎么知道这张专辑是当年发行的呢?通过找到没有艺人/唱片集.IMHO的时刻,您应该从按年份对唱片集进行分组开始。那么一切都会简单得多。谢谢你的快速回答!因此,我已经将模板作为样式表的子项进行了移动,但是调用模板仍然是递归的吗?非常感谢!我现在明白你的意思了。我没有想到xsl:key,它将是我程序的解决方案。在XSLT2.0中,使用group-end-with.Good解决方案更容易。这里的一般原则是,如果您想要处理凌乱的输入XML,最好的方法总是编写一个管道,从生成干净的XML开始,然后将其作为真正工作的样式表的输入。这样做的一个很大的优点是,messy->clean转换是高度可重用的,并且将使使用相同输入XML的所有后续转换更易于编写。
<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>