XSLT converting string number with whitespace to number

时间:2017-06-19 14:01:30

标签: xml xslt

I have a problem with xslt trasformation.
How to convert number: "2 402.83" to numeber. Unfortunely when I use function number(string(.)) I get NaN. I try to use replace function number(replace(string(.), ' ', '')), but it returns nothing (exception in js: Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.).

My xml:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
   <cd number="1" price="2 402.82"/>
</catalog>

My xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <xsl:for-each select="catalog/cd">
      <xsl:for-each select="@price">
        <span>                                      
          <xsl:value-of select="number(replace(string(.),' ', ''))" />
        </span>
      </xsl:for-each>
     </xsl:for-each>                                
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:2)

replace函数在XSLT 1.0中不可用,而是使用translate

number(translate(string(.), ' ', ''))