[xsl] generate an element only if the result is not empty

Subject: [xsl] generate an element only if the result is not empty
From: Manuel Souto Pico <manuel.souto@xxxxxxxxxxxxxx>
Date: Tue, 25 Aug 2009 11:10:32 +0200
Hello!

Again I have a doubt with generating empty elements. Here by empty I mean that the element only contains whitespace (\s|\t|\r|\n). I hope you can provide some help.

I use this spreadsheet view-source:http://xtek-solutions.net/burgundyridge/Viz/XML_TM/XLF_XLIFF_schemas/TMXtoTBX.xsl


to turn omething like this
<tu>
<tuv xml:lang="es-es" changedate="20090824T155642Z" changeid="Transit">
<seg>
<it>&lt;/Title&gt;</it>
<bpt i="1" type="struct">&lt;Elt type='Groupbox' id='242' coor='0,0,412,112' style='50020007,0' class='#FF#0080'&gt;</bpt>
</seg>
</tuv>
</tu>


into

<termEntry>
<langSet xml:lang="es-es">
<ntig>
<termGrp>
<term>
</term>
</termGrp>
</ntig>
</langSet>
</termEntry>


The template (using select="node()" and not select=".")

   <xsl:template match="seg">
       <term>
           <xsl:value-of select="node()" />
       </term>
   </xsl:template>
</xsl:stylesheet>

is well suited for the data that I have, as the output <term> element will only have content when the source element <seg> has text only (e.g. <seg>text</seg>), which is what I want, that is, I wouldn't want a <term> element with the content of the <seg> element you can see above (<it>, <bpt>, etc.). However a drawback of it is that empty elements are generated. In the example I provide above I would like to avoid generating the whole <termEntry> element if the <term> is only going to contain whitespace.

I've tried using strip-space, but that affects the source, not the output. I've also found a template called copy-unless-empty (http://www.dpawson.co.uk/xsl/sect2/N3328.html#d4887e64), which is what I need, I guess, but I don't know how to use it (I've tried but...).

If you could understand what I need ;) and you know how to help, could you drop a line?

I'm using Saxon6.5.5 with XPath 1.0 in <oXygen/> XML editor.

Thank you so much in advance for your help!

Cheers, Manuel

The whole sample files:

___SOURCE DATA___

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE tmx SYSTEM "tmx14.dtd"> <!-- Transit TMX document -->
<tmx version="1.4">
<header creationtool="Transit NXT" creationtoolversion="4.0" datatype="Transit" segtype="block" adminlang="en" srclang="en-us" o-tmf="Transit" creationdate="20090824T155634Z" creationid="MS9" o-encoding="Unicode">
<prop type="Project" />
</header>
<body>
<tu>
<prop type="FileFormatGUID">{1F8A6315-CDF7-4164-BE09-C4821791D320}</prop>
<prop type="Filename">DicAdmin_ENG</prop>
<tuv xml:lang="en-us" changedate="20090812T170437Z" changeid="Transit">
<seg>Data record structure</seg>
</tuv>
<tuv xml:lang="es-es" changedate="20090505T095118Z" changeid="CV2" creationdate="20090416T102716Z" creationid="MS9">
<seg>Estructura del registro de datos</seg>
</tuv>
</tu>
<tu>
<prop type="FileFormatGUID">{1F8A6315-CDF7-4164-BE09-C4821791D320}</prop>
<prop type="Filename">DicAdmin_ENG</prop>
<tuv xml:lang="en-us" changedate="20090824T155641Z" changeid="Transit">
<seg>
<it>&lt;/Title&gt;</it>
<bpt i="1" type="struct">&lt;Elt type='Groupbox' id='242' coor='0,0,412,112' style='50020007,0' class='#FF#0080'&gt;</bpt>
</seg>
</tuv>
<tuv xml:lang="es-es" changedate="20090824T155642Z" changeid="Transit">
<seg>
<it>&lt;/Title&gt;</it>
<bpt i="1" type="struct">&lt;Elt type='Groupbox' id='242' coor='0,0,412,112' style='50020007,0' class='#FF#0080'&gt;</bpt>
</seg>
</tuv>
</tu>
</body>
</tmx>



___OUTPUT___


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

<!DOCTYPE martif
PUBLIC "ISO 12200:1999A//DTD MARTIF core (DXFcdV04)//EN" "TBXcdv04.dtd">
<martif xml:lang="" type="TBX">
<martifHeader>
<fileDesc>
<sourceDesc>
<p>Generated with TMX2TBX4TermStarNXT_noDupl.xsl</p>
</sourceDesc>
</fileDesc>
<encodingDesc>
<p type="DCSName"/>
</encodingDesc>
</martifHeader>
<text>
<body>
<termEntry id="">
<langSet xml:lang="en-us">
<ntig>
<termGrp>
<term>Data record structure</term>
</termGrp>
</ntig>
</langSet>
<langSet xml:lang="es-es">
<ntig>
<termGrp>
<term>Estructura del registro de datos</term>
</termGrp>
</ntig>
</langSet>
</termEntry>
<termEntry id="">
<langSet xml:lang="en-us">
<ntig>
<termGrp>
<term>
</term>
</termGrp>
</ntig>
</langSet>
<langSet xml:lang="es-es">
<ntig>
<termGrp>
<term>
</term>
</termGrp>
</ntig>
</langSet>
</termEntry>


     </body>
  </text>
</martif>

Current Thread