0

I need to build a flat table in text from a hierarchical XML. I need one flat line per meas node. Each meas node has an index number (mN), which refers back to a smaller section which where a label (tL) is kept for each unique possibility of the index number. I want to loop each meas node and use the value of mN attribute to look up the corresponding tL value. The example below delivers the index (mN) numbers properly, but always delivers One for every row:

xml

<root> <cond tN="1" tL="One"/> <cond tN="2" tL="Two"/> <cond tN="3" tL="Three"/> <cond tN="4" tL="Four"/> <cond tN="5" tL="Five"/> <meas mN="1"/> <meas mN="2"/> <meas mN="3"/> <meas mN="4"/> <meas mN="5"/> <meas mN="1"/> <meas mN="2"/> <meas mN="3"/> <meas mN="4"/> <meas mN="5"/> </root> 

xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output method="text"/> <xsl:template match="/"> <xsl:for-each select="/root/meas"> <xsl:variable name="MeasN"> <xsl:value-of select="@mN"/> </xsl:variable> <xsl:value-of select="$MeasN"/>, <xsl:value-of select="../cond[$MeasN]/@tL"/>. </xsl:for-each> </xsl:template> </xsl:stylesheet> 

output

1 One 2 One 3 One 4 One 5 One 1 One 2 One 3 One 4 One 5 One 

1 Answer 1

1

Your MeasN contains a string, which means your expression cond[$MeasN] will find all cond elements for which your string is non-empty (i.e all of them). However, xsl:value-of will then just output the first.

You probably need to do this

 <xsl:value-of select="../cond[number($MeasN)]/@tL"/> 

Or maybe, you should be you should be checking the tN attribute instead? (This would work regardless of the order of the cond elements

 <xsl:value-of select="../cond[@tN = $MeasN]/@tL"/> 

In the case of checking the tL attribute, instead of the position, you could also achieve it with a key...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output method="text"/> <xsl:key name="cond" match="cond" use="@tN" /> <xsl:template match="/"> <xsl:for-each select="/root/meas"> <xsl:variable name="MeasN" select="@mN"/> <xsl:value-of select="$MeasN"/>, <xsl:value-of select="key('cond', $MeasN)/@tL"/>. </xsl:for-each> </xsl:template> </xsl:stylesheet> 
Sign up to request clarification or add additional context in comments.

1 Comment

Tim C, thank you very much! All of your suggestions are suitable and give me plenty to work with and learn from. And, thanks for simplifying my xsl:variable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.