XSLT 如果第一个值为空,则对第二个值应用排序

发布于 2024-08-09 22:37:14 字数 695 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤者何惧 2024-08-16 22:37:14
<xsl:sort select="(data[@alias='createdDate' and normalize-space() != '']|@createDate)[last()]" order="descending" />

该语句将两个包含日期的节点创建一个节点集,并根据文档顺序获取最后一个节点进行排序。如果 data 节点存在且不为空,则将使用该节点进行排序,因为元素的子元素出现在其属性节点之后。

concat() 仅在少数情况下,如果您使用文本排序才有效;数字排序将会失败。

<xsl:sort select="(data[@alias='createdDate' and normalize-space() != '']|@createDate)[last()]" order="descending" />

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.

ま昔日黯然 2024-08-16 22:37:14

是的,看起来像是一个黑客,但我已经能够通过使用带有排序的 concat 来实现这一点。

下面的例子

<xsl:for-each select="$currentPage/ancestor-or-self::node /node [@nodeTypeAlias = $documentTypeAlias and string(data [@alias='umbracoNaviHide']) != '1']">
<xsl:sort select="concat(data [@alias = 'createdDate'],@createDate)" order="descending"/>

Right, seems like a hack but I have been able to achieve this by using a concat with the sort.

Example below

<xsl:for-each select="$currentPage/ancestor-or-self::node /node [@nodeTypeAlias = $documentTypeAlias and string(data [@alias='umbracoNaviHide']) != '1']">
<xsl:sort select="concat(data [@alias = 'createdDate'],@createDate)" order="descending"/>
蓝眼睛不忧郁 2024-08-16 22:37:14

要测试 XSLT 中的节点是否为空(或省略):

<xsl:when test="not(string(name))">...</xsl:when>
<!-- or -->
<xsl:when test="not(name)">...</xsl:when>

To test if a node is empty (or omitted) in XSLT:

<xsl:when test="not(string(name))">...</xsl:when>
<!-- or -->
<xsl:when test="not(name)">...</xsl:when>
那小子欠揍 2024-08-16 22:37:14

非常感谢 Erlock 的解决方案。由于对 Umbraco XSLT 语法进行了更改,我确实花了一段时间才在我的 Umbraco 版本 (4.7.1) 中实现此功能。

对于任何感兴趣的人,我的工作示例会将 Erlock 的代码更改为:

<xsl:sort select="(current()/createdDate[normalize-space() != '']|@createDate)[last()]" order="descending" />

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;

<xsl:sort select="(current()/createdDate[normalize-space() != '']|@createDate)[last()]" order="descending" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文