XSLT 如果第一个值为空,则对第二个值应用排序
我有一个 for every 循环新闻项节点。除其他属性外,这些新闻项目还有两个属性:创建日期。系统添加日期和用户输入创建日期(以覆盖系统日期)。我希望列表按创建日期排序,并优先选择用户输入的日期。
以下是我卑微的无效尝试!
<xsl:for-each select="$currentPage/ancestor-or-self::node /node [@nodeTypeAlias = $documentTypeAlias and string(data [@alias='umbracoNaviHide']) != '1']">
<xsl:choose>
<xsl:when test="data [@alias = 'createdDate'] != ''">
<xsl:variable name="sort" select="string(data [@alias = 'createdDate'])"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="sort" select="string(@createDate)"/>
</xsl:otherwise>
</xsl:choose>
<xsl:sort select="$sort" order="descending"/>
非常感谢
I have a for each which loops round news item nodes. Among other properties these news items have two attributes for created date. System added date and a user entered created date (to override the system date). I would like the list sorted by created date with the preference on the user entered date.
Below is my humble invalid attempt!
<xsl:for-each select="$currentPage/ancestor-or-self::node /node [@nodeTypeAlias = $documentTypeAlias and string(data [@alias='umbracoNaviHide']) != '1']">
<xsl:choose>
<xsl:when test="data [@alias = 'createdDate'] != ''">
<xsl:variable name="sort" select="string(data [@alias = 'createdDate'])"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="sort" select="string(@createDate)"/>
</xsl:otherwise>
</xsl:choose>
<xsl:sort select="$sort" order="descending"/>
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该语句将两个包含日期的节点创建一个节点集,并根据文档顺序获取最后一个节点进行排序。如果 data 节点存在且不为空,则将使用该节点进行排序,因为元素的子元素出现在其属性节点之后。
concat() 仅在少数情况下,如果您使用文本排序才有效;数字排序将会失败。
This statement creates a nodeset with the two nodes containing date, and get the last one according the document order to do the sorting. If data node exists and is not empty, it will be used for the sorting because child elements of an element occur after its attribute nodes.
concat() can only work, and in a few cases, if you use text sorting; it will fail with numeric sorting.
是的,看起来像是一个黑客,但我已经能够通过使用带有排序的 concat 来实现这一点。
下面的例子
Right, seems like a hack but I have been able to achieve this by using a concat with the sort.
Example below
要测试 XSLT 中的节点是否为空(或省略):
To test if a node is empty (or omitted) in XSLT:
非常感谢 Erlock 的解决方案。由于对 Umbraco XSLT 语法进行了更改,我确实花了一段时间才在我的 Umbraco 版本 (4.7.1) 中实现此功能。
对于任何感兴趣的人,我的工作示例会将 Erlock 的代码更改为:
Many thanks to Erlock for his solution. I did struggle for a while to get this working in my version of Umbraco (4.7.1) due to the changes made to the Umbraco XSLT syntax.
For anyone interested, my working sample would change Erlock's code to become;