问题描述
我已经写了一个 XSLT 代码来从字符串中提取数字字符 ..
I have already written an XSLT code to extract the numerical characters from the string ..
这是测试 xml:
(看起来有点奇怪,但我对 XSLT 期望很高)
<xml>
<tag>10a08bOE9W234 W30D:S</tag>
<tag>10.233.23</tag>
</xml>
这是我尝试使用的 XSLT 代码:
<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>
输出..
<tag>1008923430</tag>
<tag>1023323</tag>
..
而且..我希望第二个标签像 10.23323
一样输出,即,只允许第一个小数点 .
并忽略后续的..
..
And moreover .. I want second tag to output like 10.23323
ie, allowing only first decimal-point .
and ignoring the succeeding ones ..
是否只能使用 XSLT 1.0 ??
Is it possible with only with XSLT 1.0 ??
推荐答案
第一行的单个 XPath 表达式:
translate(., translate(.,'0123456789', ''), '')
请注意,任何非数字字符(事先未知)都将被删除.
Note, that any non-numeric character (not known in advance) will be stripped.
对于第二行使用:
concat(
substring-before(
translate(., translate(.,'.0123456789', ''), ''),
'.'
),
substring(
translate(., translate(.,'.', ''), ''),
1,
1
),
translate(
substring-after(
translate(., translate(.,'.0123456789', ''), ''),
'.'
),
'.',
''
)
)
两种解决方案都可以组合成一个单一的 XPath 表达式:只需在第二个解决方案中替换
Both solutions can be combined into one single XPath expression: simply substitute in the second solution
.
与
concat(., '.0')
concat(., '.0')
这篇关于使用 XSLT 1.0 从字符串中提取数字(+int/decimal)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!