3

我想要一个列表排序,忽略任何初始的定冠词/不定冠词“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 中实现这一点?

4

2 回答 2

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>
于 2010-05-17T13:43:08.970 回答
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(经典的一对一错误)

嗯,starts-with 来自XSLT 1.0。Prooflink:Google中的第一个搜索结果 产生XSLT 1.0:函数开始于

于 2010-05-17T11:56:00.287 回答