代码之家  ›  专栏  ›  技术社区  ›  ChrisV

使用xslt排序时忽略“a”和“the”

  •  3
  • ChrisV  · 技术社区  · 16 年前

    我想将列表排序,忽略任何初始的定/不定冠词“the”和“a”。例如:

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

    我认为,也许在XSLT2.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>
    

    但是,我想在浏览器处理中使用,因此必须使用XSLT1.0。在XLST 1.0中有什么方法可以实现这一点吗?

    2 回复  |  直到 7 年前
        1
  •  5
  •   Dimitre Novatchev    11 年前

    这种转变 :

    <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>
    
        2
  •  2
  •   Gart    16 年前

    我会这样做:

    <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 来自XSLT1.0。校对链接:第一个搜索结果 Google 产量 XSLT 1.0: function starts-with