jbrehr
jbrehr

Reputation: 815

XSL sort using multiple fields

For reasons that are probably quite simple yet escaping me, I cannot get the following bibliography to sort while copying.

This is a sample of the 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>

Each biblStruct represents a work that has either an author(s) or an editor(s), and it is the basis for my sort.

The desired sort logic is this:

first sort 1: find either the first author of a book or an article, depending on biblStruct @type and sort on it

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

then sort 2: get the title @level and sort on it

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

I'm using a simple xsl:copy for now, and even just the basic sort does not work. It just copies the original. 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>

This should copy the original but sorted by author surname, then title within each author.

Thanks in advance.

Upvotes: 0

Views: 2147

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167446

If you apply-templates to the children of the listBibl then the sort needs to relative to that so if the biblStruct is one of the children a path starting with biblStruct does not make sense, unless you had such elements nested in each other. And the title elements are descendants further down, so the path starting with title also seems to miss the target.

If you change the paths to e.g.

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

I hope you get a better result.

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

Upvotes: 2

Related Questions