tags:

views:

371

answers:

2

I have already written an XSLT code to extract the numerical characters from the string ..

This is the test xml:
(looks Bit weird but I am expecting much from XSLT)

<xml>
  <tag>10a08bOE9W234 W30D:S</tag>
  <tag>10.233.23</tag>
</xml>

This is the XSLT code I am trying with:

<xsl:template match="tag">
  <tag>
  <xsl:value-of select="number(translate(., 'a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|:| ', ''))"/>
<!-- I am not happy writing this line .. is there any light weight code replacement-->
  </tag>
</xsl:template>

Outputs ..

  <tag>1008923430</tag>
  <tag>1023323</tag>

..
And moreover .. I want second tag to output like 10.23323 ie, allowing only first decimal-point . and ignoring the succeeding ones ..

Is it possible with only with XSLT 1.0 ??

+3  A: 

Firstly, I don't think you need the 'pipe' characters in the translate. The translate function works on single characters, so you just put a single concatenated string of all characters.

To output only the first decimal point, I think you could use substring-before and substring-after to extract the portions before and after the decimal point, and for the bit after the first decimal point you can then remove the remaining decimal points.

Try this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
   <xsl:template match="tag">
      <xsl:variable name="NumberWithDots" select="translate(., 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ: ', '')"/>
      <tag>
         <xsl:choose>
            <xsl:when test="contains($NumberWithDots, '.')">
               <xsl:value-of select="substring-before($NumberWithDots, '.')"/>
               <xsl:text>.</xsl:text>
               <xsl:value-of select="translate(substring-after($NumberWithDots, '.'), '.', '')"/>
            </xsl:when>
            <xsl:otherwise>
               <xsl:value-of select="number($NumberWithDots)"/>
            </xsl:otherwise>
         </xsl:choose>
      </tag>
   </xsl:template>
</xsl:stylesheet>
Tim C
Hats off (I felt so ..) .. :-)
infant programmer
+2  A: 

A single XPath expression for the 1st line:

translate(., translate(.,'0123456789', ''), '')

Note, that any non-numeric character (not known in advance) will be stripped.

For the second line use:

   concat(
    substring-before(
        translate(., translate(.,'.0123456789', ''), ''),
        '.'
              ),

    substring(
        translate(., translate(.,'.', ''), ''),
         1,
         1
        ),

    translate(
        substring-after(
                  translate(., translate(.,'.0123456789', ''), ''),
                  '.'
                  ),
        '.',
        ''
        )
    )

Both solutions can be combined into one single XPath expression: simply substitute in the second solution

   . 

with

concat(., '.0')

Dimitre Novatchev
thanx for the solution, your code gets rid-off all other special characters too (chars other than 0-9 and '.') .. thats great .. :-)
infant programmer