简体   繁体   English

如何在XSLT中渲染有限数量的元素?

[英]How do i render a limited number of elements in XSLT?

I need to render a specified number of elements from an XML source, where the elements "DueDate" is not exeeded. 我需要从XML源(不执行元素“ DueDate”)呈现指定数量的元素。 Here is an example of the xml: 这是xml的示例:

<Items>
  <Item>
    <Title>Title 1</Title>
    <DueDate>01-02-2008</DueDate>
  </Item>
  <Item>
    <Title>Title 2</Title>
    <DueDate>01-02-2009</DueDate>
  </Item>
  <Item>
    <Title>Title 3</Title>
    <DueDate>01-02-2010</DueDate>
  </Item>
  <Item>
    <Title>Title 4</Title>
    <DueDate>01-02-2011</DueDate>
  </Item>
  <Item>
    <Title>Title 5</Title>
    <DueDate>01-02-2012</DueDate>
  </Item>
  <Item>
    <Title>Title 6</Title>
    <DueDate>01-02-2013</DueDate>
  </Item>
</Items>

The number of elements to display and the current date are passed to the XSLT as paramaters. 要显示的元素数量和当前日期作为参数传递给XSLT。 Is it possible to count the number of rendered elements in a for-each loop in Xslt? 是否可以在Xslt的for-each循环中计算渲染元素的数量? Or is there a better approach? 还是有更好的方法?

An example could be that the limit was set to 3 elements. 例如,限制设置为3个元素。 In this example I would expect to see the following results: "Title 3", "Title 4" and "Title 5". 在此示例中,我希望看到以下结果:“标题3”,“标题4”和“标题5”。

You can do something like this. 你可以做这样的事情。 Make it into a template you can call with paramters: 使其成为可以使用参数调用的模板:

<xsl:template match="/">
<xsl:variable name="count" select="3"/>

<xsl:for-each select="Items/Item">
    <xsl:if test="position() &lt; $count">
        <xsl:value-of select="Title"/> - <xsl:value-of select="DueDate"/>
    </xsl:if>
</xsl:for-each>

Try nesting this inside a standard xsl for-each where n in your case is 3: 尝试将其嵌套在标准xsl for-each中,其中n为3:

<xsl:if test="position() &lt; n">

But if you also want to check the date then you will need nest another if and create dates in the format yyyyMMdd which can be numerically compared like this: 但是,如果您还想检查日期,则需要嵌套另一个if并以yyyyMMdd格式创建日期,该日期可以按如下方式进行数字比较:

<xsl:variable name="secondDate" select="concat(substring(submissionDeadline, 1,4),substring(submissionDeadline, 6,2),substring(submissionDeadline, 9,2))"/>

<xsl:if test="$firstDate &gt; $secondDate">

The main problem is that your input XML does not conform to the useful 'yyyy-mm-dd' format. 主要问题是您的输入XML不符合有用的'yyyy-mm-dd'格式。 This makes sorting/filtering these items a pain in the ass behind. 这使得排序/过滤这些项目在 屁股 后面的疼痛。

If I understand you correctly, you need to 如果我对您的理解正确,则需要

  • filter on due date first 首先过滤到期日
  • apply a maximum to the output after that 之后将最大值应用于输出

The XPath for selecting all <Item> s up to a certain maximum due date would go like this: 用于选择所有<Item>直到某个最大到期日期的XPath如下所示:

Item[
  substring-before(DueDate, '-') 
  &lt;= 
  substring-before($MaxDueDate, '-')

  and

  substring-before(substring-after(DueDate, '-'), '-')
  &lt;= 
  substring-before(substring-after($MaxDueDate, '-'), '-')

  and 

  substring-after(substring-after(DueDate, '-'), '-')
  &lt;= 
  substring-after(substring-after($MaxDueDate, '-'), '-')
][
  position() &lt;= $MaxCount
]

Now compare that to the trivial way, if you had 'yyyy-mm-dd' dates: 现在,如果您有'yyyy-mm-dd'日期,则将其与简单的方法进行比较:

Item[
  DueDate &lt;= $MaxDueDate
][
  position() &lt;= $MaxCount
]

So, to copy only these elements, you would go: 因此,仅复制这些元素,您将需要:

<xsl:template match="Items">
  <xsl:copy>
    <xsl:copy-of select="
      { the expression I gave above }
    " />
  </xsl:copy>
</xsl:template>

For parameter values of '01-02-2012' and 4 , respectively, I get: 对于分别为'01-02-2012'4参数值,我得到:

<Items>
  <Item>
    <Title>Title 1</Title>
    <DueDate>01-02-2008</DueDate>
  </Item>
  <Item>
    <Title>Title 2</Title>
    <DueDate>01-02-2009</DueDate>
  </Item>
  <Item>
    <Title>Title 3</Title>
    <DueDate>01-02-2010</DueDate>
  </Item>
  <Item>
    <Title>Title 4</Title>
    <DueDate>01-02-2011</DueDate>
  </Item>
</Items>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM