Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any operation such as trim in xslt?

Tags:

xslt

i wrote a xslt code which converts a xml file to a html file which contains lot of tables, one of the column contains messages(very long messages), but that line starts with either of the two words "Verification Passed" or "Verification failed"

My requirement is to make the entire table row red if verification failed and make entire table row green if verification passed

 <xsl:choose>
  <xsl:when test="contains(@message,'Verification failed:')"><td bgcolor="#FF0000">   <xsl:value-of select="@Message"/></td></xsl:when>
  <xsl:when test="contains(@message,'Verification passed:')"><td bgcolor="#00FF00"><xsl:value-of select="@Message"/></td></xsl:when>   
  <xsl:otherwise><td> <xsl:value-of select="@Message"/></td></xsl:otherwise>
</xsl:choose> 
like image 799
Trini Avatar asked Sep 17 '12 11:09

Trini


2 Answers

is there any operation such as trim in xslt?

I. XSLT 1.0

No, and it is rather difficult to perform "trim" in XSLT 1.0.

Here is the trim function/template from FXSL:

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

  <xsl:import href="trim.xsl"/>

  <!-- to be applied on trim.xml -->

  <xsl:output method="text"/>
  <xsl:template match="/">
    '<xsl:call-template name="trim">
        <xsl:with-param name="pStr" select="string(/*)"/>
    </xsl:call-template>'
  </xsl:template>
</xsl:stylesheet>

When this transformation is performed (you have to download at least a few other stylesheet modules, which comprise the complete import tree) on this XML document:

<someText>

   This is    some text   

</someText>

the wanted, correct result is produced:

'This is    some text'

II In XSLT 2.0 / XPath 2.0

Still a little bit tricky, but very short:

     if (string(.))
       then replace(., '^\s*(.+?)\s*$', '$1')
       else ()

Here is the complete, corresponding transformation:

<xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
     "<xsl:sequence select=
         "if (string(.))
           then replace(., '^\s*(.+?)\s*$', '$1')
           else ()
           "/>"
 </xsl:template>
</xsl:stylesheet>

and when applied on the same XML document (above), the same correct result is produced:

"This is    some text"
like image 194
Dimitre Novatchev Avatar answered Sep 28 '22 11:09

Dimitre Novatchev


Unfortunately you don't say what you expect your "trim()" function to do. But from your description of the requirement, I would guess that normalize-space() is close enough:

starts-with(normalize-space(message), 'Verification passed'))

The XPath normalize-space() function differs from the Java trim() method in that (a) it replaces internal sequences of whitespace characters by a single space, and (b) it has a slightly different definition of whitespace.

like image 25
Michael Kay Avatar answered Sep 28 '22 10:09

Michael Kay