Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to detect numeric string in xslt?

I am now doing a html to xml xslt transformation, pretty straigh-forward. But I have one slight problem that is left unsolved.

For example, in my source html, a node looks like:

<p class="Arrow"><span class="char-style-override-12">4</span><span class="char-style-override-13"> </span>Sore, rash, growth, discharge, or swelling.</p>

As you can see, the first child node < span> has a value of 4, is it actually rendered as a arrow point in the browser (maybe some encoding issue, it is treated as a numeric value in my xml editor).

So my question is, I wrote a template to match the tag, then pass the text content of it to another template match :

 <xsl:template match="text()">
    <xsl:variable name="noNum">
        <xsl:value-of select="normalize-space(translate,'4',''))"/>
    </xsl:variable>
 <xsl:copy-of select="$noNum"/>    
  </xsl:template>  

As you can see, this is definitely not a good solution, it will replace all the numbers appearing in the string, not only the first character. So I wonder if there is a way to remove only the first character IF it is a number, maybe using regular expression? Or, I am actually going the wrong way, should there be a better way to think of solving this problem(e.g, changing the encoding)?

Any idea is welcomed! Thanks in advance!

like image 269
Kevin Avatar asked Jan 19 '23 08:01

Kevin


1 Answers

Just use this :

<xsl:variable name="test">4y4145</xsl:variable>
<xsl:if test= "not(string(number(substring($test,1,1)))='NaN')">
    <xsl:message terminate="no">
              <xsl:value-of select="substring($test,2)"/>
    </xsl:message>
</xsl:if>

This is a XSLT 1.0 solution. I think regex is an overkill for this.

Output :

[xslt]                           y4145
like image 69
FailedDev Avatar answered Jan 28 '23 09:01

FailedDev