Ajay Srikanth
Ajay Srikanth

Reputation: 1185

Regex substring in XSLT

I'm trying to create a regex to get a substring in XSL 2.0 and this is the first time i'm working on XSL.

This is the expression that I'm trying to use to get the substring /(GS\.?\d{4}-\d{4}-\d{4})/g.

If I get any other value like GS.4354-4354-4543-5 or GS.4354-4354-4543-556 I want to extract the value that matches my regex.

If I use 'matches' it is just returning true or false, but my expectation is to trim the additional data. Any help is much appreciated.

I've also tried the following

<xsl:analyze-string select="$messageValue" 
       regex="(GS\.?\d{4}-\d{4}-\d{4})">
    <xsl:matching-substring>
        <bug><xsl:value-of select="regex-group(1)"/></bug>
    </xsl:matching-substring>
</xsl:analyze-string>

whereas my $messageValue = GS.4354-4354-4543-5 and it is giving empty response.

Input - XML

<?xml version="1.0" encoding="UTF-8"?>
<log>
    <logentry revision="265">
    <author>dre</author>
    <date>2015-04-13T02:35:25.246150Z</date>
    <msg>GS.4554-0504-2089-4545</msg>
</logentry>
<logentry revision="73283">
    <author>john</author>
    <date>2015-04-13T14:10:20.987159Z</date>
    <msg>GS.4554-0504-2089-2</msg>
</logentry>
<logentry revision="73290">
    <author>ron</author>
    <date>2015-04-13T14:24:57.475711Z</date>
    <msg>GS-S.4554-0504-2089</msg>
</logentry>
</log>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
  <html>
  <body>
  <h2>SVN Issues</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th style="text-align:left">ver</th>
        <th style="text-align:left">author</th>
        <th style="text-align:left">date</th>
        <th style="text-align:left">ticket</th>
      </tr>
      <xsl:for-each select="log/logentry">
      <tr>
        <td><xsl:value-of select="@revision"/></td>
        <td><xsl:value-of select="author"/></td>
        <td><xsl:value-of select="date"/></td>
        <td>            
                <xsl:variable name="messageValue" select="msg"/>
                <xsl:analyze-string select="$messageValue" 
                  regex="(GS\.?\d{4}-\d{4}-\d{4})">
                      <xsl:matching-substring>
                         <xsl:value-of select="regex-group(1)"/>
                      </xsl:matching-substring>
                </xsl:analyze-string>
        </td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Expected Output -

<html>
   <body>
      <h2>SVN Issues</h2>
      <table border="1">
         <tr bgcolor="#9acd32">
            <th style="text-align:left">ver</th>
            <th style="text-align:left">author</th>
            <th style="text-align:left">date</th>
            <th style="text-align:left">ticket</th>
         </tr>
         <tr>
            <td>265</td>
            <td>dre</td>
            <td>2015-04-13T02:35:25.246150Z</td>
            <td>GS.4554-0504-2089</td>
         </tr>
         <tr>
            <td>73283</td>
            <td>john</td>
            <td>2015-04-13T14:10:20.987159Z</td>
            <td>GS.4554-0504-2089</td>
         </tr>
         <tr>
            <td>73290</td>
            <td>ron</td>
            <td>2015-04-13T14:24:57.475711Z</td>
            <td>GS-S.4554-0504-2089</td>
         </tr>
      </table>
   </body>
</html>

Solution from Martin is working when I test it in FreeFormatter.com, but for some reason when I deploy it on my cloud that is running linux this doesn't work. It is always returning empty string. Any one had any idea about it ?

Upvotes: 0

Views: 216

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

The rexeg attribute can take attribute value templates which use {} as delimiters so to use the literally you need to double them

<xsl:analyze-string select="$messageValue" 
       regex="(GS\.?\d{{4}}-\d{{4}}-\d{{4}})">
    <xsl:matching-substring>
        <bug><xsl:value-of select="."/></bug>
    </xsl:matching-substring>
</xsl:analyze-string>

Upvotes: 1

Related Questions