Is there a way to define a numeric string in xslt?

Now I am doing the html to xml xslt conversion, pretty tucked forward. But I have one small problem that remains unresolved.

For example, in my original html, node looks like this:

<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, it actually displays as an arrow point in the browser (maybe some encoding problem, it is considered as a numeric value in my xml editor).

So my question is: I wrote a template to match the tag, and then passed the text content to another template:

  <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 certainly not a good solution, it will replace all the numbers that appear in the string, and not just the first character. So I wonder if there is a way to remove only the first character, if that number, possibly using a regular expression? Or, I'm really mistaken, should there be a better way to solve this problem (for example, change the encoding)?

Any idea is welcome! Thanks in advance!

+4
source share
2 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 an XSLT 1.0 solution. I think regex is redundant for this.

Output:

 [xslt] y4145 
+6
source

Use this one XPath expression :

 concat(translate(substring(.,1,1), '0123456789', ''), substring(.,2) ) 
+3
source

Source: https://habr.com/ru/post/1379656/


All Articles