Ignoring 'A' and 'The' when sorting with XSLT
I would like to have a list sorted ignoring any initial definite/indefinite articles 'the' and 'a'. For instance:
- The Comedy 开发者_如何学Cof Errors
- Hamlet
- A Midsummer Night's Dream
- Twelfth Night
- The Winter's Tale
I think perhaps in XSLT 2.0 this could be achieved along the lines of:
<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>
However, I want to use in-browser processing, so have to use XSLT 1.0. Is there any way to achieve this in XLST 1.0?
This transformation:
<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>
when applied on this XML document:
produces the wanted, correct result:
<p>Plays sorted by title: </p>
<p>Barber</p>
<p>The Comedy of Errors</p>
<p>CTA & Fred</p>
<p>Hamlet</p>
<p>A Midsummer Night's Dream</p>
<p>Twelfth Night</p>
<p>The Winter's Tale</p>
Here is how I would do that:
<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>
Update: I forgot to add 1 to the expression (classic off-by-one error)
Well, starts-with
is from XSLT 1.0. Prooflink: the first search result in Google yields XSLT 1.0: function starts-with
精彩评论