Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSL finding a node based on value of other node + multilevel sorting

Tags:

xml

xslt

Basically there are two things I need to figure out:

  1. multilevel sorting
  2. on the third level where I put the ???: how to select another <term> which has the <title> that equals the <narrowerterm> of the current <term>.

I hope this is clear enough. I have not been able to find what I need for some time.

My XML (excerpt):

<thesaurus>
    <term>
        <title>defense</title>
         <narrowerterm>defense skills</narrowerterm>
         <narrowerterm>defense actions</narrowerterm>
    </term>
    <term>
        <title>defense skills</title>
        <broaderterm>defense</broaderterm>
        <narrowerterm>skill cd</narrowerterm>
        <narrowerterm>skill xy</narrowerterm>
        <narrowerterm>skill ab</narrowerterm>
    </term>
    <term>
        <title>defense actions</title>
        <broaderterm>defense</broaderterm>
        <narrowerterm>actions against xy</narrowerterm>
        <narrowerterm>actions against ab</narrowerterm>
    </term>
</thesaurus>

My XSL:

<xsl:template match="thesaurus">
<html>
<body> 
<xsl:for-each-group select="term" group-by="title">
    <xsl:sort select="title"/>
    <xsl:choose>
        <xsl:when test="broaderterm"> 
        </xsl:when>
        <xsl:otherwise>
            <xsl:choose>
                <xsl:when test="use"> 
                </xsl:when>
                <xsl:otherwise>
                    <p>
                        <xsl:value-of select="current-grouping-key()"/><br/>
                        <xsl:for-each select="narrowerterm">
                            .<xsl:value-of select="."/><br/>
                            <xsl:for-each select="???"/>
                                ..<xsl:value-of select="narrowerterm"/><br/>
                            </xsl:for-each>
                        </xsl:for-each>
                    </p>
                </xsl:otherwise> 
            </xsl:choose>
        </xsl:otherwise> 
    </xsl:choose>
</xsl:for-each-group> 
</body>
</html>
</xsl:template>

What I want is:

defense
.defense actions
..actions against ab
..actions against xy
.defense skills
..skill ab
..skill cd
..skill xy

Where I am is:

defense
.defense skills
.defense actions
like image 759
Kristof K. Avatar asked Feb 28 '26 01:02

Kristof K.


1 Answers

I don't think this is a grouping task or that it requires XSLT 2.0.

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <xsl:apply-templates select="term[not(broaderterm)]">
    <xsl:with-param name="pIndent" select="''"/>
    <xsl:sort select="title"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="term">
  <xsl:param name="pIndent"/>
  <xsl:value-of select="concat($pIndent, title, '&#xA;')"/>

  <xsl:variable name="vSubterms" select=
      "../term[broaderterm = current()/title]"/>

  <xsl:variable name="vLeafSubterms" select=
  "narrowerterm[not(. = /*/term[broaderterm = current()/title]/title)]"/>

  <xsl:apply-templates select="$vSubterms | $vLeafSubterms">
   <xsl:with-param name="pIndent" select="concat($pIndent, '.')"/>
   <xsl:sort select="concat(self::term/title, self::narrowerterm/text())"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="narrowerterm">
  <xsl:param name="pIndent"/>
  <xsl:value-of select="concat($pIndent, ., '&#xA;')"/>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<thesaurus>
    <term>
        <title>defense</title>
        <narrowerterm>defense skills</narrowerterm>
        <narrowerterm>defense actions</narrowerterm>
    </term>
    <term>
        <title>defense skills</title>
        <broaderterm>defense</broaderterm>
        <narrowerterm>skill cd</narrowerterm>
        <narrowerterm>skill xy</narrowerterm>
        <narrowerterm>skill ab</narrowerterm>
    </term>
    <term>
        <title>defense actions</title>
        <broaderterm>defense</broaderterm>
        <narrowerterm>actions against xy</narrowerterm>
        <narrowerterm>actions against ab</narrowerterm>
    </term>
</thesaurus>

produces the wanted, correct result:

defense
.defense actions
..actions against ab
..actions against xy
.defense skills
..skill ab
..skill cd
..skill xy
like image 144
Dimitre Novatchev Avatar answered Mar 02 '26 06:03

Dimitre Novatchev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!