我有以下字符串作为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的新手,获取这些数据似乎有点复杂,因为每种情况的验证似乎都不同。
我感谢任何人的帮助…
如果你确信每次你可以做这样的事情时,你的字符串都会像你的例子一样。
<xsl:value-of select="substring-before(substring-after(mystringvalue,': '),',')"/>
要获得XSLT函数的良好来源,请检查w3schools
此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(., 'nr?')"/>
<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
'