Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 忽略';A';和';';使用XSLT排序时_Sorting_Xslt_Xslt 1.0 - Fatal编程技术网

Sorting 忽略';A';和';';使用XSLT排序时

Sorting 忽略';A';和';';使用XSLT排序时,sorting,xslt,xslt-1.0,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

我想有一个列表排序忽略任何初始的确定/不确定的条款'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>


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

以下是我的方法:

<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(经典关闭一个错误)


好的,
开头是XSLT1.0中的。校对链接:中的第一个搜索结果将产生此转换:

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


按标题排序的剧本:

应用于此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>
生成所需的正确结果

<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>
按标题排序的播放:

理发师

错误的喜剧

CTA&;弗雷德

哈姆雷特

仲夏夜之梦

第十二夜

冬天的故事


问题是,
开始,我认为是XSLT2.0,编辑我站得住脚!我在23分钟前一发表帖子就纠正了自己,我只是不想删除原始评论,因为删除自己的错误似乎有点可疑。干杯。出色的简单创意解决方案Gart–谢谢!通常正确的方法是为每个元素存储一个“排序标题”,因为规则可能变得非常复杂,甚至可以扩展到其他语言(例如,“Die”是一篇文章,在德语中应该忽略,但“Die Hard”不应该在“H”下排序,即使在德语索引中也可以找到)。“仲夏夜之梦”的排序标题是“仲夏夜之梦,A”。我应该提到,XLST 1.0中提供了start-with(),但如果不是…@Joachim–你是对的,但我需要使用现有数据,没有资源来维护并行排序字段;Gart的quick-n-dirty解决方案正是我所需要的——好的评论,好的问题!(+1). 请参阅我的答案以获得完整的解决方案。第二个优秀的解决方案,Dimitre,它避免了Gart解决方案的数学运算——也谢谢!因为我不得不多想一想,所以我会为其他人的利益添加:例如,对于“冬天的故事”,它将排序的值将是“冬天的故事”。克里斯夫:是的,这种解决方案的优点是它更简单,在做算术时犯错误的机会更少。尽管排序键看起来很奇怪,但这不会影响最终输出的值。如果“the”位于文本的中间,则可能会出现问题,更像“…Something the Something…”(抱歉,无法想出任何真实的示例)。另外,在一般情况下,文章“An”也应该相应地处理。@Gart:当然。但是这个问题严格地说,文章是在剧本的开头——为了排序的目的,去掉任何中间词是没有意义的。我发现的一个边缘情况是,当要以“CTA&;Fred”形式排序的项目时,条目将根据
进行排序。这不是“The”的问题,而是更容易匹配“a”的问题。