sorting - 使用 XSLT 排序时忽略 'A' 和 'The'

标签 sorting xslt xslt-1.0

我想要一个忽略任何初始定冠词/不定冠词“the”和“a”的列表。例如:

  • 错误喜剧
  • 哈姆雷特
  • 仲夏夜之梦
  • 第十二夜
  • 冬天的故事

我认为也许在 XSLT 2.0 中,这可以通过以下方式实现:

<xsl:template match="/">
  <xsl:for-each select="play"/>
    <xsl:sort select="if (starts-with(title, 'A ')) then substring(title, 2) else
                      if (starts-with(title, 'The ')) then substring(title, 4) else title"/>
    <p><xsl:value-of select="title"/></p>
  </xsl:for-each>
</xsl:template>

但是,我想使用浏览器内处理,所以必须使用 XSLT 1.0。有什么方法可以在 XLST 1.0 中实现这一点?

最佳答案

这个转换:

<xsl:template match="plays">
 <p>Plays sorted by title: </p>
    <xsl:for-each select="play">
      <xsl:sort select=
      "concat(@title
               [not(starts-with(.,'A ') 
                  or 
                   starts-with(.,'The '))],
              substring-after(@title[starts-with(., 'The ')], 'The '),
              substring-after(@title[starts-with(., 'A ')], 'A ')
              )
     "/>
      <p>
        <xsl:value-of select="@title"/>
      </p>
    </xsl:for-each>
</xsl:template>

应用于此 XML 文档时:

产生想要的、正确的结果:

<p>Plays sorted by title: </p>
<p>Barber</p>
<p>The Comedy of Errors</p>
<p>CTA &amp; Fred</p>
<p>Hamlet</p>
<p>A Midsummer Night's Dream</p>
<p>Twelfth Night</p>
<p>The Winter's Tale</p>

关于sorting - 使用 XSLT 排序时忽略 'A' 和 'The',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2848641/

相关文章:

java - 如何在 XSLT 中迭代列表(例如 ArrayList)

sql - 将空值排序在最后而不是最前面

xml - 使用 XSLT 进行简单的 XML 重构

xml - 如何过滤具有某个子节点的节点

xslt - xsl :sort: sorting by numeric value

css - XSL 选择样式文本和 td

xml - XSLT:从特定 child 开始的 child 的不同迭代

c++ - std::stable_sort:如何选择内存优化算法而不是时间优化算法?

java - 合并排序比较?

php - 稳定的 uasort - 为什么顺序被颠倒了?