2012-02-07 110 views
0

我是新来的XSL,我想这样做XSL:通过检查变量

var oldvalue= '' 
for each 
    get currentvalue 
    if (oldvalue != currentvalue) 
    { 
     print divider 
     oldvalue = currentvalue 
    }  
end for 

我已经与

<xsl:variable name="oldname" select="name" /> 

<xsl:for-each select="myxpathstring"> 
    <xsl:choose> 
     <xsl:when test="$newname = $newname"> 
     <xsl:variable name="oldname" select="$newname" /> 
     <div class='divider'>divider stuff </div> 
     </xsl:when> 

     <xsl:otherwise>No</xsl:otherwise> 
</xsl:choose> 

</xsl:for-each> 

尝试过,但不起作用,因为打印分频器我无法更新'oldname'变量。 任何人都有解决方案?

完整XSL(用JSP参数,因为我生成动态的XSL)

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/"> 
<xsl:variable name="counter" select="count(<%= request.getParameter("xpath")%>)" /> 
<div id='counter' class='ui-state-highlight ui-corner-all'><p><span style="float: left; margin-right: .3em;" class="ui-icon ui-icon-info"></span><xsl:value-of select="$counter"/> schema's gevonden</p></div> 
<div class="page_navigation ui-widget-header ui-corner-all"></div> 
<ul id='schemeslist' class="content"> 
<xsl:for-each select="<%= request.getParameter("xpath")%>"><!-- filter on sports //scheme[ (sports/sport ='Fietsen') and (planduration=12 or planduration=16)]--> 
<xsl:sort select="name"/> 

    <!-- ////////////////// --> 
     <!-- print divider if name is new --> 
    <!-- ////////////////// --> 

    <li class='scheme' id='scheme'> 

    <div class='schemeSports'> 
    <!-- <xsl:for-each select="sports/sport"> 
      <xsl:value-of select="."/> 
    </xsl:for-each> --> 
    <xsl:value-of select="sport"/> 
</div> 
<xsl:variable name="theid" select="@id" /> 





<div class='schemeName'><xsl:value-of select="name"/></div> 
<div class='planDuration'><xsl:value-of select="planduration"/></div> 
<div class='fitnessLevel'><xsl:value-of select="fitnesslevel"/></div> 
<div class='order'> 
    <xsl:if test="price != ''"><xsl:value-of select="price"/>euro</xsl:if> 

<button class='more' onClick='showInfo("{$theid}")' id='{$theid}'>MEER</button> <button class='buy' onClick='window.location = "buy.html?ID={$theid}"'>KOOP</button> 
</div> 

</li> 
</xsl:for-each> 



</ul> 
<div style='clear:both'></div> 
<div class="page_navigation ui-widget-header ui-corner-all"></div> 

</xsl:template> 
</xsl:stylesheet> 

,这里是完整的XML

<?xml version="1.0" encoding="ISO-8859-1"?> 
<schemes lang='nl-BE'> 
<scheme id='5E47B7E9'> 
    <sport>Fietsen</sport> 
<author>Energy Lab</author> 
<name>Voorbereiding op de Ronde van Vlaanderen</name> 
<planduration>16</planduration> 
<fitnesslevel>Beginner</fitnesslevel> 
<frequency>1-3</frequency> 
<longtraining></longtraining> 
<rollers>Ja</rollers> 
<price>12</price> 
<description> 
    <![CDATA[...]]> 
</description> 
    </scheme> 
<scheme id='5E47B7E9'> 
    <sport>Triatlon</sport> 

<author>Energy Lab</author> 
<name>Voorbereiding op de Ronde van Vlaanderen</name> 
<planduration>16</planduration> 
<fitnesslevel>Expert</fitnesslevel> 
<frequency>1-3</frequency> 
<longtraining></longtraining> 
<rollers>Ja</rollers> 
<price>12</price> 
<description> 
    <![CDATA[...]]> 
</description> 
    </scheme> 
<scheme id='5E47B7E9'> 
<sport>Fietsen</sport> 

<author>Energy Lab</author> 
<name>Voorbereiding op een Triatlon</name> 
<planduration>24</planduration> 
<fitnesslevel>Beginner</fitnesslevel> 
<frequency>1-3</frequency> 
<longtraining></longtraining> 
<rollers>Ja</rollers> 
<price>48</price> 
<description> 
    <![CDATA[...]]> 
</description> 
    </scheme> 
</schemes> 
+0

你必须在这里使用了不同的方法。你能提供一个输入XML的小样本吗? – Dervall 2012-02-07 11:42:12

回答

0

创建返回新的值的函数的样品,将myxpathstring中的旧值和值列表作为参数。

0

如果我了解您的XSLT正确,您必须使用muenchian grouping。有关此方法如何工作的说明,请参阅this article

XML:

<?xml version="1.0" encoding="UTF-8"?> 
<schemes lang='nl-BE'> 
    <scheme id='5E47B7E9'> 
     <sport>Fietsen</sport> 
     <author>Energy Lab</author> 
     <name>Voorbereiding op de Ronde van Vlaanderen</name> 
     <planduration>16</planduration> 
     <fitnesslevel>Beginner</fitnesslevel> 
     <frequency>1-3</frequency> 
     <longtraining></longtraining> 
     <rollers>Ja</rollers> 
     <price>12</price> 
     <description><![CDATA[...]]></description> 
    </scheme> 
    <scheme id='5E47B7E9'> 
     <sport>Triatlon</sport> 
     <author>Energy Lab</author> 
     <name>Voorbereiding op de Ronde van Vlaanderen</name> 
     <planduration>16</planduration> 
     <fitnesslevel>Expert</fitnesslevel> 
     <frequency>1-3</frequency> 
     <longtraining></longtraining> 
     <rollers>Ja</rollers> 
     <price>12</price> 
     <description><![CDATA[...]]></description> 
    </scheme> 
    <scheme id='5E47B7E9'> 
     <sport>Fietsen</sport> 
     <author>Energy Lab</author> 
     <name>Voorbereiding op een Triatlon</name> 
     <planduration>24</planduration> 
     <fitnesslevel>Beginner</fitnesslevel> 
     <frequency>1-3</frequency> 
     <longtraining></longtraining> 
     <rollers>Ja</rollers> 
     <price>48</price> 
     <description><![CDATA[...]]></description> 
    </scheme> 
</schemes> 

XSLT:

我更换了第一个JSP标签以点并取消了第二,因为我不知道你试图实现什么。我不建议动态*.xsl。您最好将这些信息放入您的input.xml中,并使用xpath进行查询。

<?xml version='1.0' encoding='UTF-8'?> 
<xsl:stylesheet version='1.0' 
       xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> 

<xsl:output method='xml' indent='yes' omit-xml-declaration='yes'/> 

<xsl:key name='schemata' match='scheme' use='name'/> 

<xsl:template match='scheme'> 
    <li class='scheme' id='scheme'> 
     <div class='schemeSports'> 
      <xsl:value-of select="sport"/> 
     </div> 
     <xsl:variable name="theid" select="@id"/> 
     <div class='schemeName'> 
      <xsl:value-of select="name"/> 
     </div> 
     <div class='planDuration'> 
      <xsl:value-of select="planduration"/> 
     </div> 
     <div class='fitnessLevel'> 
      <xsl:value-of select="fitnesslevel"/> 
     </div> 
     <div class='order'> 
      <xsl:if test="price != ''"> 
       <xsl:value-of select="price"/> 
       euro 
      </xsl:if> 
      <button class='more' onClick='showInfo("{$theid}")' id='{$theid}'>MEER</button> 
      <button class='buy' onClick='window.location = "buy.html?ID={$theid}"'>KOOP</button> 
     </div> 
    </li> 
</xsl:template> 

<xsl:template match='/schemes'> 
    <xsl:variable name="counter" select="count(.)"/> <!-- jsp --> 
    <div id='counter' class='ui-state-highlight ui-corner-all'> 
     <p> 
      <span style="float: left; margin-right: .3em;" class="ui-icon ui-icon-info"/> 
      <xsl:value-of select="$counter"/> 
      <xsl:text> schema's gevonden</xsl:text> 
     </p> 
    </div> 
    <div class="page_navigation ui-widget-header ui-corner-all"/> 
    <ul id='schemeslist' class="content"> 
     <!-- muenchian grouping --> 
     <xsl:for-each select='scheme[generate-id() = generate-id(key("schemata", name)[1])]'> 
      <xsl:sort select='name'/> 
      <xsl:if test='position() != 1'> 
       <div class='divider'/> 
      </xsl:if> 
      <xsl:apply-templates select='key("schemata", name)'/> 
     </xsl:for-each> 
    </ul> 
    <div style='clear:both'/> 
    <div class="page_navigation ui-widget-header ui-corner-all"/> 
</xsl:template> 

</xsl:stylesheet> 

结果:

<div id="counter" class="ui-state-highlight ui-corner-all"> 
    <p><span style="float: left; margin-right: .3em;" class="ui-icon ui-icon-info"/>1 schema's gevonden</p> 
</div><div class="page_navigation ui-widget-header ui-corner-all"/><ul id="schemeslist" class="content"> 
    <li class="scheme" id="scheme"> 
    <div class="schemeSports">Fietsen</div> 
    <div class="schemeName">Voorbereiding op de Ronde van Vlaanderen</div> 
    <div class="planDuration">16</div> 
    <div class="fitnessLevel">Beginner</div> 
    <div class="order">12 
       euro 
      <button class="more" onClick="showInfo(&quot;5E47B7E9&quot;)" id="5E47B7E9">MEER</button><button class="buy" onClick="window.location = &quot;buy.html?ID=5E47B7E9&quot;">KOOP</button></div> 
    </li> 
    <li class="scheme" id="scheme"> 
    <div class="schemeSports">Triatlon</div> 
    <div class="schemeName">Voorbereiding op de Ronde van Vlaanderen</div> 
    <div class="planDuration">16</div> 
    <div class="fitnessLevel">Expert</div> 
    <div class="order">12 
       euro 
      <button class="more" onClick="showInfo(&quot;5E47B7E9&quot;)" id="5E47B7E9">MEER</button><button class="buy" onClick="window.location = &quot;buy.html?ID=5E47B7E9&quot;">KOOP</button></div> 
    </li> 
    <div class="divider"/> 
    <li class="scheme" id="scheme"> 
    <div class="schemeSports">Fietsen</div> 
    <div class="schemeName">Voorbereiding op een Triatlon</div> 
    <div class="planDuration">24</div> 
    <div class="fitnessLevel">Beginner</div> 
    <div class="order">48 
       euro 
      <button class="more" onClick="showInfo(&quot;5E47B7E9&quot;)" id="5E47B7E9">MEER</button><button class="buy" onClick="window.location = &quot;buy.html?ID=5E47B7E9&quot;">KOOP</button></div> 
    </li> 
</ul><div style="clear:both"/><div class="page_navigation ui-widget-header ui-corner-all"/> 
+0

我检查了你的例子,它的工作原理,但我没有得到它将它翻译成我的 – user1194465 2012-02-07 12:26:40

+0

@ user1194465:在你的问题中提供输入XML和结果XML。 – Saxoier 2012-02-07 12:31:51