is there any operation such as trim in xslt?

前端 未结 4 1960
再見小時候
再見小時候 2020-12-17 19:31

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

相关标签:
4条回答
  • 2020-12-17 20:04

    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

    0 讨论(0)
  • 2020-12-17 20:09

    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"
    
    0 讨论(0)
  • 2020-12-17 20:10

    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.

    0 讨论(0)
  • 2020-12-17 20:16
     <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>
    
    0 讨论(0)
提交回复
热议问题