Sort glossary in alphabetical order

Questions about XML that are not covered by the other forums should go here.
anna_craneo
Posts: 30
Joined: Tue Feb 20, 2018 2:51 pm

Sort glossary in alphabetical order

Post by anna_craneo » Wed Apr 04, 2018 2:44 pm

I have created ditamap for my glossterms

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE map PUBLIC "-//OASIS//DTD DITA Map//EN" "map.dtd">
<map id="glossary">
<title>A Glossary of Terms used in <ph keyref="product"/></title>
<topicmeta>
<shortdesc>Definitions for terms used in <ph keyref="product"/></shortdesc>
<data name="wh-menu">
<data name="hide" value="no"/>
</data>
</topicmeta>
<glossref href="../glossary/risk-score.dita" print="yes" toc="no" product="platform"
keys="glossentry-risk-score"/>
<glossref href="../glossary/case.dita" keys="glossentry-case" print="yes" toc="no"
product="platform"/>
<glossref href="../glossary/ME-group.dita" print="yes" toc="no" product="platform"
keys="glossentry-me-group"/>
<glossref href="../glossary/owner.dita" print="yes" toc="no" product="platform"
keys="glossentry-owner"/>
</map>
Now I would like to sort refs in alphabetical order. I have found this post, but I do not use bookmap.

I'm not very strong in XSLT, so I would really appreciate any help.

Radu
Posts: 6579
Joined: Fri Jul 09, 2004 5:18 pm

Re: Sort glossary in alphabetical order

Post by Radu » Wed Apr 11, 2018 2:45 pm

Hi,

Maybe in order to use the XSLT stylesheet I proposed on that blog post you could add a <topicgroup> with a certain "id='glossarylistgroup'" attribute to surround all your topicrefs to glossary entries (this will not change how the publishing looks like).
And then in the XSLT stylesheet instead of having the XSLT template match the "glossarylist" have it match that topicgroup like:

Code: Select all

<xsl:template match="topicgroup[@id='glossarylistgroup']">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:for-each select="*" >
<xsl:sort select="document(@href, .)/*/glossterm/text()"/>
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

B-E-N
Posts: 129
Joined: Thu Mar 24, 2016 5:54 pm

Re: Sort glossary in alphabetical order

Post by B-E-N » Thu Dec 05, 2019 11:29 am

I am also trying to sort glossary terms in alphabetical order in our numerous target languages.

We are using ditamaps instead of bookmaps so the initial code from the blog post obviously doesn't work.

I've added the <topicgroup> element then and used your code provided in this thread. Without success.

Is this supposed to work with our glossary term structure?

This is how our glossary term topics look like:

Code: Select all

<glossentry xml:lang="de-DE" id="example_g">
    <glossterm>Beispiel</glossterm>
    <glossdef>
        <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
    </glossdef>
    <prolog>
        <author>b-e-n</author>
        <critdates>
            <created date="2018-07-03"/>
            <revised modified="2019-11-08"/>
        </critdates>
    </prolog>
</glossentry>
When I apply the refactoring method you've explained it does not have any effect on the ditamap. Do I have to make a special selection or change the code somewhere?

I am using the XSL you've provided in this thread.

Code: Select all

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="glossarylist">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:for-each select="*" >
                <xsl:sort select="document(@href, .)/*/glossterm/text()"/>
                <xsl:apply-templates select="."/>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Radu
Posts: 6579
Joined: Fri Jul 09, 2004 5:18 pm

Re: Sort glossary in alphabetical order

Post by Radu » Thu Dec 05, 2019 12:10 pm

Hi,

If the DITA Map has a structure like this:

Code: Select all

  <topicgroup id='glossarylistgroup'>
    <topicref href="concepts/glossaryGenus.dita"
      keys="genus" print="yes"/>
    <topicref href="concepts/glossaryPollination.dita"
      keys="pollination" print="yes"/>
  </topicgroup>
then an XSLT refactoring operation looking like this should sort it:

Code: Select all

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="topicgroup[@id='glossarylistgroup']">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:for-each select="*" >
                <xsl:sort select="document(@href, .)/*/glossterm/text()"/>
                <xsl:apply-templates select="."/>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

B-E-N
Posts: 129
Joined: Thu Mar 24, 2016 5:54 pm

Re: Sort glossary in alphabetical order

Post by B-E-N » Thu Dec 05, 2019 4:17 pm

This works perfectly. Thank you!

B-E-N
Posts: 129
Joined: Thu Mar 24, 2016 5:54 pm

Re: Sort glossary in alphabetical order

Post by B-E-N » Thu Dec 05, 2019 4:55 pm

One addition:

The sorting was case sensitive. So the resulting order was like this:
A
B
a


Instead of:
A
a
B


I've then added <xsl:choose> to your code to add the "lang" attribute when needed. This sorts the languages as required.

Code: Select all

            <xsl:when test="/map/@xml:lang='de-DE'">
                <xsl:copy>
                    <xsl:apply-templates select="@*"/>
                    <xsl:for-each select="*" >
                        <xsl:sort select="document(@href, .)/*/glossterm/text()" lang="de"/>
                        <xsl:apply-templates select="."/>
                    </xsl:for-each>
                </xsl:copy>
            </xsl:when>
This seems to work now as intended. Thanks again.

Post Reply