Basically there are two things I need to figure out:
<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.
<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>
<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
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, '
')"/>
<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, ., '
')"/>
</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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With