使用多个字段进行 XSL 排序

  • 本文关键字:XSL 排序 字段 xml xslt
  • 更新时间 :
  • 英文 :


由于可能很简单但逃脱的原因,我无法在复制时对以下参考书目进行排序。

这是 XML 的示例:

<listBibl>
  <biblStruct type="book">
    <monogr>
        <title level="book-title">Magic in the Middle Ages</title>
        <author>
            <forename>Richard</forename><surname>Kieckhefer</surname>
        </author>
        <imprint>
            <pubPlace>Toronto</pubPlace>
            <publisher>Cambridge University Press</publisher>
            <date>2000</date>
        </imprint>
    </monogr>
  </biblStruct>
  <biblStruct type="journal-article">
    <analytic>
        <title level="article-title">Social Mentalities and the Cases of Medieval Scepticism</title>
        <author>
            <forename>Susan</forename><surname>Reynolds</surname>
        </author>
    </analytic>
    <monogr>
        <title level="journal-name">Transactions of the Royal Historical Society</title>
        <imprint>
            <biblScope unit="volume">1</biblScope>
            <biblScope unit="page">21-41</biblScope>
            <date>1991</date>
        </imprint>
    </monogr>
  </biblStruct>
  <biblStruct type="book">
    <monogr>
        <title level="book-title">Pathways to Medieval Peasants</title>
        <editor>
            <forename>James Ambrose</forename>
            <surname>Raftis</surname>
        </editor>
        <imprint>
            <pubPlace>Toronto</pubPlace>
            <publisher>Pontifical Institute of Mediaeval Studies</publisher>
            <date>1981</date>
        </imprint>
    </monogr>
  </biblStruct>
  <biblStruct type="book">
    <monogr>
        <title level="book-title">Kingdoms and Communities in Western Europe, 900-1300</title>
        <author>
            <forename>Susan</forename><surname>Reynolds</surname>
        </author>
        <imprint>
            <pubPlace>Oxford</pubPlace>
            <publisher>Oxford University Press</publisher>
            <date>1997</date>
        </imprint>
    </monogr>
  </biblStruct>
<listBibl>

每个biblStruct代表一个有作者或编辑的作品,它是我排序的基础。

所需的排序逻辑是这样的:

第一排序1:根据biblStruct @type查找一本书或一篇文章的第一作者并对其进行排序

biblStruct[@type='book']/monogr/author[1]/surname | 
biblStruct[@type='journal-article']/analytic/author[1]/surname 

然后排序 2:获取title @level并对其进行排序

title[@level='book-title'] | title[@level='article-title']

我现在使用的是一个简单的xsl:copy,即使只是基本的排序也不起作用。它只是复制原件。XSL:

<xsl:template match="listBibl">
    <xsl:copy>
        <xsl:apply-templates>
            <xsl:sort select="biblStruct[@type='book']/monogr/author[1]/surname | biblStruct[@type='journal-article']/analytic/author[1]/surname "/>
            <xsl:sort select="title[@level='book-title'] | title[@level='article-title'] "/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

这应该复制原文,但按作者姓氏排序,然后是每个作者的标题。

提前谢谢。

如果您apply-templates listBibl的子元素,则排序需要相对于该子项,因此如果biblStruct是其中一个子项,则以biblStruct开头的路径没有意义,除非您将此类元素相互嵌套。而title元素是更下方的后代,所以从title开始的路径似乎也错过了目标。

如果将路径更改为例如

      <xsl:apply-templates>
          <xsl:sort select=".[@type='book']/monogr/author[1]/surname | .[@type='journal-article']/analytic/author[1]/surname"/>
          <xsl:sort select=".//title[@level='book-title'] | .//title[@level='article-title']"/>
      </xsl:apply-templates>

我希望你得到更好的结果。

http://xsltfiddle.liberty-development.net/jyyiVhh/1

最新更新