本文介绍了使用 XSLT 排序时忽略“A"和“The"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个列表排序,忽略任何初始定冠词/不定冠词the"和a".例如:
I would like to have a list sorted ignoring any initial definite/indefinite articles 'the' and 'a'. For instance:
- 错误的喜剧
- 哈姆雷特
- 仲夏夜之梦
- 第十二夜
- 冬天的故事
我认为也许在 XSLT 2.0 中,这可以按照以下方式实现:
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>
但是,我想使用浏览器内处理,所以必须使用XSLT 1.0.有没有办法在 XLST 1.0 中实现这一点?
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?
推荐答案
这种转变:
<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 & Fred</p>
<p>Hamlet</p>
<p>A Midsummer Night's Dream</p>
<p>Twelfth Night</p>
<p>The Winter's Tale</p>
这篇关于使用 XSLT 排序时忽略“A"和“The"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!