XSL search node based on value of another node + multilevel sort

Basically, I need to figure out two things:

  • multilevel sorting
  • on the third level where I put ???: how to choose another <term>

    one that has <title>

    one that is equal to the <narrowerterm>

    current one <term>

    .

Hope this is clear enough. I haven't 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>

      

I want to:

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

      

Where I am:

defense
.defense skills
.defense actions

      

+3


source to share


1 answer


I don't think this is a grouping task or 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 to 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 desired, correct result :

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

      

+3


source







All Articles