Adding one or incrementing variable in xslt

不想你离开。 提交于 2019-12-02 04:39:00

Your problem arises from thinking like a procedural programmer when trying to use a functional language. It's a bad mix; tools work a lot better when you work with them and not against them.

Don't try to walk through the document and increment variables to keep count of things. Describe what you want in declarative terms.

Here, you want the first two non-empty <video> elements in the document to display, and you want the others (all the empty <video> elements and the third and later non-empty <video> elements) to be passed over in silence. (I'm making this up, of course: since you don't show your XML, I don't actually know what you really want, in XML terms.)

So you want a template to suppress empty <video> elements:

<xsl:template match="video[not(node())]"/>

And you want a template to suppress any <video> element preceded in the document by two or more non-empty <video> elements:

<xsl:template 
  match="video[count(preceding::video
                     [* or normalized-space(.)]
                    ) 
               > 1]"/>

And you want a template to display the first two non-empty <video> elements:

<xsl:template 
  match="video[* or normalized-space(.)]
              [count(preceding::video
                     [* or normalized-space(.)]
                    ) 
               &lt; 2]">

  <xsl:message>Displaying video ... </xsl:message>
  <!--* ... whatever else is needed to display the video ... *-->

<xsl:template>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!