在xslt中有修饰等操作吗?

时间:2012-09-17 11:49:50

标签: xslt

我编写了一个xslt代码,它将xml文件转换为包含大量表的html文件,其中一列包含消息(非常长的消息),但该行以“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> 

4 个答案:

答案 0 :(得分:16)

不幸的是,你没有说你期望你的“trim()”函数做什么。但是根据您对需求的描述,我猜想normalize-space()足够接近:

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

XPath normalize-space()函数与Java trim()方法的不同之处在于:(a)它用单个空格替换空白字符的内部序列,(b)它对空格的定义略有不同。 / p>

答案 1 :(得分:3)

  

是否有任何操作,例如xslt中的trim?

<强>予。 XSLT 1.0

不,在XSLT 1.0中执行“修剪”相当困难。

以下是 FXSL 中的trim功能/模板:

<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>

执行此转换时(您必须至少下载一些其他样式表模块,其中包含完整的导入树)此XML文档

<someText>

   This is    some text   

</someText>

产生了想要的正确结果

'This is    some text'

II在XSLT 2.0 / XPath 2.0中

还是有点棘手,但很短:

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

以下是完整的相应转化

<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>

,当应用于同一XML文档(上图)时,会产生相同的正确结果:

"This is    some text"

答案 2 :(得分:1)

使用带有registerLangFunctions的

的XSLT1

今天,在(复杂的) XSLT2标准发布后大约10年,许多项目还在使用(更快) XSLT1 。也许问题不仅仅是&#34;简单的 vs 复杂&#34;,但XSLT1对于Perl,PHP,Python,PostgreSQL和许多其他社区都是事实。

因此,Perl,PHP和Python的解决方案:使用您的主要语言执行trim以及XSLT1中不存在的其他常用函数。

这是PHP的一个例子:  https://en.wikibooks.org/wiki/PHP_Programming/XSL/registerPHPFunctions

答案 3 :(得分:-10)

 <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>
相关问题