在xslt中有修饰等操作吗?

Tri*_*ini 9 xslt

我写了一个XML文件,将其转换为包含大量的表的HTML文件中的XSLT代码,该列的一个载消息(很长的消息),但该行与任一两个词的开始"核查合格"或"验证失败"

如果验证失败,我的要求是使整个表行变为红色,如果验证通过则使整个表行变为绿色

 <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> 
Run Code Online (Sandbox Code Playgroud)

Mic*_*Kay 18

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

starts-with(normalize-space(message), 'Verification passed'))
Run Code Online (Sandbox Code Playgroud)

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


Dim*_*hev 4

xslt中有trim等操作吗?

一、XSLT 1.0

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

这是FXSLtrim的函数/模板:

<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>
Run Code Online (Sandbox Code Playgroud)

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

<someText>

   This is    some text   

</someText>
Run Code Online (Sandbox Code Playgroud)

产生了想要的正确结果

'This is    some text'
Run Code Online (Sandbox Code Playgroud)

II 在 XSLT 2.0 / XPath 2.0 中

还是有点棘手,但很短:

     if (string(.))
       then replace(., '^\s*(.+?)\s*$', '$1')
       else ()
Run Code Online (Sandbox Code Playgroud)

这是完整的、相应的转换

<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>
Run Code Online (Sandbox Code Playgroud)

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

"This is    some text"
Run Code Online (Sandbox Code Playgroud)