Sakthi V
Sakthi V

Reputation: 59

XSLT distinct value of the ancestor element

This is the input XML:

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>

<book>
<chapter label="Chapter 1">
<para>Text Text<link role="kwd" linkend="Gloss_1">Term 1</link> Text</para>
<para>Text Text Text<link role="kwd" linkend="Gloss_2">Term 2</link></para>
<section>
<title>Key Terms</title>
<itemizedlist mark="none">
<listitem><para><link role="kwd" linkend="Gloss_1">Term 1</link> Def 1</para></listitem>
<listitem><para><link role="kwd" linkend="Gloss_2">Term 2</link> Def 2</para></listitem>
</itemizedlist>
</section>
</chapter>
<chapter label="Chapter 2">
<para>Text Text<link role="kwd" linkend="Gloss_3">Term 3</link> Text</para>
<para>Text Text Text<link role="kwd" linkend="Gloss_1">Term 1</link></para>
<section>
<title>Key Terms</title>
<itemizedlist mark="none">
<listitem><para><link role="kwd" linkend="Gloss_1">Term 1</link> Def 3</para></listitem>
<listitem><para><link role="kwd" linkend="Gloss_3">Term 3</link> Def 1</para></listitem>
</itemizedlist>
</section>
</chapter>
<chapter label="Chapter 3">
<para>Text Text<link role="kwd" linkend="Gloss_4">Term 4</link> Text</para>
<para>Text Text Text<link role="kwd" linkend="Gloss_2">Term 2</link></para>
<para>Text Text Text<link role="kwd" linkend="Gloss_5">Term 5</link></para>
<section>
<title>Key Terms</title>
<itemizedlist mark="none">
<listitem><para><link role="kwd" linkend="Gloss_2">Term 2</link> Def 2</para></listitem>
<listitem><para><link role="kwd" linkend="Gloss_4">Term 4</link> Def 4</para></listitem>
<listitem><para><link role="kwd" linkend="Gloss_5">Term 5</link> Def 5</para></listitem>
</itemizedlist>
</section>
</chapter>
<glossary>
<glossentry xml:id="Gloss_1"><glossterm>Term 1</glossterm><glossdef>Def 1</glossdef></glossentry>
<glossentry xml:id="Gloss_2"><glossterm>Term 2</glossterm><glossdef>Def 2</glossdef></glossentry>
<glossentry xml:id="Gloss_3"><glossterm>Term 3</glossterm><glossdef>Def 3</glossdef></glossentry>
<glossentry xml:id="Gloss_4"><glossterm>Term 4</glossterm><glossdef>Def 4</glossdef></glossentry>
<glossentry xml:id="Gloss_5"><glossterm>Term 5</glossterm><glossdef>Def 5</glossdef></glossentry>
</glossary>
</book>

Output would be:

<?xml version="1.0" encoding="utf-8"?>
<glossary>
   <row>
      <col1>Chapter 1</col1>
      <col1>Chapter 2</col1>
      <col2>Term 1</col2>
   </row>
   <row>
      <col1>Chapter 1</col1>
      <col1>Chapter 3</col1>
      <col2>Term 2</col2>
   </row>
   <row>
      <col1>Chapter 2</col1>
      <col2>Term 3</col2>
   </row>
   <row>
      <col1>Chapter 3</col1>
      <col2>Term 4</col2>
   </row>
   <row>
      <col1>Chapter 3</col1>
      <col2>Term 5</col2>
   </row>
</glossary>

My code is:

<xsl:result-document href="out.xml">
  <glossary>


        <xsl:for-each select="book/glossary/glossentry">

          <row>

          <xsl:for-each select="key('num', @xml:id)">
            <col1>
            <xsl:value-of select="ancestor::chapter/@label"/>
          </col1>
          </xsl:for-each>
          <col2><xsl:value-of select="glossterm"/></col2>

        </row>
        </xsl:for-each>
</glossary>
</xsl:result-document>

The glossary items are listed twice in the book - One at the end of each chapter and a consolidated items at the end. I would like to get the chapter number(s) for each glossary term listed at the end of the book. I tried various things but I am unable to get distinct value of the ancestor element. Can someone please help?

Upvotes: 0

Views: 103

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 116992

I think you could simply reverse your point-of-view:

<xsl:key name="chapter-by-link" match="chapter" use="descendant::link/@linkend" />

<xsl:template match="/">
    <!-- other stuff -->
    <glossary>
        <xsl:for-each select="book/glossary/glossentry">
            <row>
                <xsl:for-each select="key('chapter-by-link', @xml:id)">
                    <col1>
                        <xsl:value-of select="@label"/>
                    </col1>
                </xsl:for-each>
                 <col2>
                    <xsl:value-of select="glossterm"/>
                </col2>
            </row>
        </xsl:for-each>
    </glossary> 
</xsl:template>

Upvotes: 2

Martin Honnen
Martin Honnen

Reputation: 167506

I suppose it suffices to select the ancestor::chapter/@label attributes as duplicates would be eliminated with any step selecting nodes so you would just change

      <xsl:for-each select="key('num', @xml:id)">
        <col1>
        <xsl:value-of select="ancestor::chapter/@label"/>
      </col1>
      </xsl:for-each>

to

      <xsl:for-each select="key('num', @xml:id)/ancestor::chapter/@label">
        <col1>
        <xsl:value-of select="."/>
      </col1>
      </xsl:for-each>

or perhaps organize the code into small templates to have cleaner code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:output indent="yes"/>

  <xsl:key name="ref" match="link[@role = 'kwd']" use="@linkend"/>

  <xsl:template match="book">
    <glossary>
        <xsl:apply-templates select="glossary/glossentry"/>
    </glossary>
  </xsl:template>

  <xsl:template match="glossentry">
      <row>
          <xsl:apply-templates select="key('ref', @xml:id)/ancestor::chapter/@label"/>
          <col2>
              <xsl:value-of select="glossterm"/>
          </col2>
      </row>
  </xsl:template>

  <xsl:template match="chapter/@label">
      <col1>
          <xsl:value-of select="."/>
      </col1>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/jyH9rNv

Upvotes: 1

Related Questions