我将以下字符串作为XML文档中的文本节点:
OK - load average: 0.34,0.02,0.34
USERS OK - 0 users currently logged in,100,200
HTTP WARNING: HTTP/1.1 403 Forbidden
PING OK - Packet loss = 0%
DISK OK - free space: / 34285 MB (82% inode=94%):
从上面的字符串我想有xslt代码只获取第一个数字数据,例如预期输出如下:
0.34
100
null or blank
null or blank
null or blank
由于上述验证是在以下情况下检索完成的:或,
我是XSLT的新手,并且看起来有点复杂以获取这些数据,因为每种情况的验证似乎都不同。
感谢任何人的帮助...
答案 0 :(得分:0)
如果您确定每次可以执行此类操作时,您的字符串将类似于您的示例。
<xsl:value-of select="substring-before(substring-after(mystringvalue,': '),',')"/>
对于XSLT函数checkout w3schools
的良好来源答案 1 :(得分:0)
此XSLT 2.0转换:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="text()">
<xsl:variable name="vLines" select="tokenize(., '\n\r?')"/>
<xsl:for-each select="$vLines">
<xsl:value-of select=
"if(matches(.,':\s*(\d+(\.\d+)?)'))
then
(replace(., '^.*?:\s*(\d+(\.\d+)?).*$','$1'),'
')
else
if(matches(.,',\s*\d+(\.\d+)?'))
then
(replace(., '^.*?,\s*(\d+(\.\d+)?).*$','$1'),'
')
else
'null
'
"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
应用于以下XML文档时:
<t>OK - load average: 0.34,0.02,0.34
USERS OK - 0 users currently logged in,100,200
HTTP WARNING: HTTP/1.1 403 Forbidden
PING OK - Packet loss = 0%
DISK OK - free space: / 34285 MB (82% inode=94%):</t>
生成想要的正确结果:
0.34
100
null
null
null
单个XPath 2.0表达式解决方案:
for $vLine in
('OK - load average: 0.34,0.02,0.34'
, 'USERS OK - 0 users currently logged in,100,200'
,'HTTP WARNING: HTTP/1.1 403 Forbidden'
, 'PING OK - Packet loss = 0%'
, 'DISK OK - free space: / 34285 MB (82% inode=94%):'
)
return
if(matches($vLine,':\s*(\d+(\.\d+)?)'))
then
(replace($vLine, '^.*?:\s*(\d+(\.\d+)?).*$','$1'),'
')
else
if(matches($vLine,',\s*\d+(\.\d+)?'))
then
(replace($vLine, '^.*?,\s*(\d+(\.\d+)?).*$','$1'),'
')
else
'null
'