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

时间:2010-05-17 11:35:38

标签: 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中实现这个目标?

2 个答案:

答案 0 :(得分:5)

此转化

 

<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>

答案 1 :(得分:2)

我将如何做到这一点:

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

更新我忘了在表达式中添加1(经典的逐个错误)

嗯,来自XSLT 1.0的starts-with 。 Prooflink:Google中的第一个搜索结果会产生XSLT 1.0: function starts-with