Search code examples
xmlxslttei

xsl: display result of concat variable with comma between each value


I have the following variable:

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE stylesheet [
 <!ENTITY menu SYSTEM "verb.xml">   
]>

<xsl:variable name="per">
  <xsl:value-of select="*//per/@value | *//gen/@value | *//number/@value"/>
</xsl:variable>

<xsl:for-each select="concat($per, '')" >
  <xsl:value-of select="translate(., ' ', '')" separator="," />
</xsl:for-each>

TEI-XML content:

 <gramGrp n="1.1" ana="#actor-affixes">
   <per ana="#s2" value="2"/>
   <gen ana="#smasc" value="m"/>
   <number ana="#ssing" value="sg"/>
 </gramGrp>
 <gramGrp n="1.2" ana="#actor-affixes">
   <per ana="#s3" value="3"/>
   <gen ana="#sfem" value="f"/>
   <number ana="#ssing" value="sg"/>
 </gramGrp>
 <!-- gramGrp -->

The output is currently: 2msg3fsg

But I want: 2msg, 3fsg

I tried several solutions, for example, a variable for each element, instead of one variable for all TEI element, or <xsl:if test="position() != last()"><xsl:value-of select="translate(., ' ', '')" /><xsl:text>, </xsl:text></xsl:if>. But it doesn't work...

In advance, thanks for your kind advice.


Solution

  • Use a variable that is a sequence of strings of the concatenated values or string-joined values:

    <xsl:variable name="values" as="xs:string*" select="//gramGrp/string-join((per/@value, gen/@value, number/@value), '')"/>
    <xsl:value-of select="$values" separator=", "/>
    

    See http://xsltfiddle.liberty-development.net/eiQZDbb.