is there any operation such as trim in xslt?

2019-01-25 10:19发布

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> 

标签: xslt
4条回答
The star\"
2楼-- · 2019-01-25 10:30
 <xsl:variable name="Colour">
 <xsl:choose>
 <xsl:when test="contains(@Message,'Verification failed:')">background-color:red;       </xsl:when>
 <xsl:when test="contains(@Message,'Verification passed:')">background-color:green</xsl:when>
 <xsl:otherwise> </xsl:otherwise>
 </xsl:choose>
 </xsl:variable>

 <tr style="{$Colour}">
 <td> <xsl:value-of select="@Time"/></td>
 <td>Line <xsl:value-of select="@Line"/></td>
 <td> <xsl:value-of select="@Type"/></td>
 <td> <xsl:value-of select="@Message"/></td>
 </tr>
查看更多
萌系小妹纸
3楼-- · 2019-01-25 10:31

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.

查看更多
小情绪 Triste *
4楼-- · 2019-01-25 10:31

using XSLT1 with registerLangFunctions

Today, ~10 years after (complex) XSLT2 standard released, many projects yet use (faster) XSLT1. Perhaps the problem is not only "simple vs complex", but XSLT1 is a fact for Perl, PHP, Python, PostgreSQL, and many other communities.

So, a solution for Perl, PHP and Python: use your main language to do trim and another usual functions that not exists into XSLT1.

Here an example of PHP: https://en.wikibooks.org/wiki/PHP_Programming/XSL/registerPHPFunctions

查看更多
贼婆χ
5楼-- · 2019-01-25 10:35

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"
查看更多
登录 后发表回答