Re: [xsl] Path according to a child node attribute

Subject: Re: [xsl] Path according to a child node attribute
From: "Gian Paolo Bernardini" <eu1517395@xxxxxxxxx>
Date: Mon, 2 Jan 2012 10:58:47 +0100
Hi all,

I have posted a similar question two days ago see
http://www.biglist.com/lists/lists.mulberrytech.com/xsl-list/archives/201112
/msg00325.html in order to create a Path according to a child node attribute
Only, there is a slightly change which adds other Rubric elements with more
kind attributes and inside them, two Label elements for the language chosen:

<?xml version="1.0" encoding="UTF-8"?>
<root>
	<Class code="B">
		<SubClass code="b1"/>
		<SubClass code="b2"/>
		<Rubric kind="preferred">
			<Label xml:lang="it">FUNZIONI CORPOREE</Label>
			<Label xml:lang="en">BODY FUNCTIONS</Label>
		</Rubric>
		<Rubric kind="definition">
			<Label xml:lang="it">Le funzioni corporee sono
..</Label>
			<Label xml:lang="en">Body functions ...</Label>
		</Rubric>
	</Class>
	<Class code="b1">
		<SuperClass code="B"/>
		<SubClass code="b110-b139"/>
		<Rubric kind="preferred">
			<Label xml:lang="it">FUNZIONI MENTALI</Label>
			<Label xml:lang="en">MENTAL FUNCTIONS</Label>
		</Rubric>
		<Rubric kind="note">
			<Label xml:lang="it">Questo capitolo ...</Label>
			<Label xml:lang="en">This chapter ...</Label>
		</Rubric>
	</Class>
	<Class code="b110-b139">
		<SuperClass code="b1"/>
		<SubClass code="b110"/>
		<Rubric kind="preferred">
			<Label xml:lang="it">FUNZIONI MENTALI
GLOBALI</Label>
			<Label xml:lang="en">GLOBAL MENTAL FUNCTIONS</Label>
		</Rubric>
	</Class>
	<Class code="b110">
		<SuperClass code="b110-b139"/>
		<Rubric kind="preferred">
			<Label xml:lang="it">Funzioni della
coscienza</Label>
			<Label xml:lang="en">Consciousness functions</Label>
		</Rubric>
		<Rubric kind="note">
			<Label xml:lang="it">Funzioni ...</Label>
			<Label xml:lang="en">General ...</Label>
		</Rubric>
	</Class>
	<Class code="b2">
		<SuperClass code="B"/>
		<SubClass code="b210-b229"/>
		<Rubric kind="preferred">
			<Label xml:lang="it">FUNZIONI SENSORIALI E
DOLORE</Label>
			<Label xml:lang="en">SENSORY FUNCTIONS AND
PAIN</Label>
		</Rubric>
		<Rubric kind="note">
			<Label xml:lang="it">Questo capitolo ...</Label>
			<Label xml:lang="en">This chapter ...</Label>
		</Rubric>
	</Class>
	<Class code="b210-b229">
		<SuperClass code="b2"/>
		<SubClass code="b210"/>
		<Rubric kind="preferred">
			<Label xml:lang="it">FUNZIONI VISIVE E
CORRELATE</Label>
			<Label xml:lang="en">SEEING AND RELATED
FUNCTIONS</Label>
		</Rubric>
	</Class>
	<Class code="b210">
		<SuperClass code="b210-b229"/>
		<Rubric kind="preferred">
			<Label xml:lang="it">Funzioni della vista</Label>
			<Label xml:lang="en">Seeing functions</Label>
		</Rubric>
		<Rubric kind="note">
			<Label xml:lang="it">Funzioni ...</Label>
			<Label xml:lang="en">Sensory ...</Label>
		</Rubric>
	</Class>
</root>

The output should be similar like this:

<?xml version="1.0" encoding="UTF-8"?>
<root>
	<Class code="B">
		<SubClass code="b1"/>
		<SubClass code="b2"/>
		<Rubric kind="preferred">
			<Label xml:lang="it">FUNZIONI CORPOREE</Label>
			<Label xml:lang="en">BODY FUNCTIONS</Label>
		</Rubric>
		<Rubric kind="definition">
			<Label xml:lang="it">Le funzioni corporee sono
..</Label>
			<Label xml:lang="en">Body functions ...</Label>
		</Rubric>
	<Path/>
	</Class>
	<Class code="b1">
		<SuperClass code="B"/>
		<SubClass code="b110-b139"/>
		<Rubric kind="preferred">
			<Label xml:lang="it">FUNZIONI MENTALI</Label>
			<Label xml:lang="en">MENTAL FUNCTIONS</Label>
		</Rubric>
		<Rubric kind="note">
			<Label xml:lang="it">Questo capitolo ...</Label>
			<Label xml:lang="en">This chapter ...</Label>
		</Rubric>
		<Path>B FUNZIONI CORPOREE</Path>
	</Class>
	<Class code="b110-b139">
		<SuperClass code="b1"/>
		<SubClass code="b110"/>
		<Rubric kind="preferred">
			<Label xml:lang="it">FUNZIONI MENTALI
GLOBALI</Label>
			<Label xml:lang="en">GLOBAL MENTAL FUNCTIONS</Label>
		</Rubric>
		<Path>B FUNZIONI CORPOREE/b1 FUNZIONI MENTALI</Path>
	</Class>
	<Class code="b110">
		<SuperClass code="b110-b139"/>
		<Rubric kind="preferred">
			<Label xml:lang="it">Funzioni della
coscienza</Label>
			<Label xml:lang="en">Consciousness functions</Label>
		</Rubric>
		<Rubric kind="note">
			<Label xml:lang="it">Funzioni ...</Label>
			<Label xml:lang="en">General ...</Label>
		</Rubric>
		<Path>B FUNZIONI CORPOREE/b1 FUNZIONI MENTALI/b110-b139
FUNZIONI MENTALI GLOBALI</Path>
	</Class>
	...


The aim is the same, now it must be chosen the text inside the <Label
xml:lang="it"> inside <Rubric kind="preferred"> since there are more kind
attributes for <Rubric> element.

Here's the code provided by Martin Hennen which works great if there is only
one <Rubric> element without attribute and inside some text:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">

  <xsl:key name="k1" match="Class" use="@code"/>

   <xsl:template match="@* | node()">
     <xsl:copy>
       <xsl:apply-templates select="@* | node()"/>
     </xsl:copy>
   </xsl:template>

   <xsl:template match="Class">
     <xsl:copy>
       <xsl:apply-templates select="@* | node()"/>
       <Path>
         <xsl:apply-templates select="key('k1', SuperClass/@code)"
mode="path"/>
       </Path>
     </xsl:copy>
   </xsl:template>

   <xsl:template match="Class" mode="path">
     <xsl:variable name="next" select="key('k1', SuperClass/@code)"/>
     <xsl:if test="$next">
       <xsl:apply-templates select="$next" mode="path"/>
       <xsl:text>/</xsl:text>
     </xsl:if>
     <xsl:value-of select="concat(@code,' ', Rubric)"/>
   </xsl:template>

</xsl:stylesheet>

Many thanks if someone can suggest

GB

Current Thread