Using regex in XSLT 1.0 (exslt)

444 views Asked by At

I have to use a regular expression to remove the useless text from a string and be able to build the correct one from it. The problem is that I don't seem to make EXLT's regex:match function work.

The regular expression I need to match the strings to is /(\d{4})-0*(\d{0,4})/ (4 numbers followed by a dash (-), followed by as many zeros as it may have, followed by up to 4 numbers). Then finally I need to grab the first 4 numbers, replace the dash with a colon, and remove all the zeros.

Some input examples could be somethingCool_SFS2000-120#somethingEle, uselessText1995-0001234moreNon-sense, 3500-0950blabl!abla. The output for these examples should be 2000:120, 1995:1234 and 3500:950

I tried using a JS function, but Nokogiri complained. Same issue for EXSLT regex:match function. What is really weird is that in another file I used EXSLT's string:encode-uri without any issues.

Worst case scenario I'll write a recursive function to check for every character of the string to build the correct one, but I think there should be an easier way.

This is my xslt file

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:regexp="http://exslt.org/regular-expressions">

  <xsl:template match="metadata-entry">
    <xsl:variable name="chunks" select="regexp:match(@value, '/(\d{4})-0*(\d{0,4})/')"/>
    <xsl:variable name="prettyName">
      <xsl:value-of select="concat($chunks[2], ':' , $chunks[3])" />
    </xsl:variable>
      <div class="x-paragraph">
        <xsl:value-of select="$prettySfsName"/>
      </div>
  </xsl:template>

</xsl:stylesheet>

And this is the other file where I used EXSLT string function the same way, but this time it actually works fine

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:str="http://exslt.org/strings">
  <xsl:template match="authors">

    <xsl:variable name="authorUrl" select="concat('/b/search?q=', str:encode-uri(@full-name, 'true', 'UTF-8' ))"/>
    
    <a href='{$authorUrl}'>
      <xsl:value-of select="@full-name" />
    </a>

  </xsl:template>
</xsl:stylesheet>
0

There are 0 answers