我可以为 XPath 中缺失的标签创建一个值吗? [英] Can I create a value for a missing tag in XPath?

查看:24
本文介绍了我可以为 XPath 中缺失的标签创建一个值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它使用 XPath 从 XML 文件中提取数据.如果该 XML 源文件中的节点丢失,我想返回值N/A"(很像 Oracle NVL 函数).诀窍在于应用程序不支持 XSLT;我想单独使用 XPath 和 XPath 来做到这一点.

I have an application which extracts data from an XML file using XPath. If a node in that XML source file is missing I want to return the value "N/A" (much like the Oracle NVL function). The trick is that the application doesn't support XSLT; I'd like to do this using XPath and XPath alone.

这可能吗?

推荐答案

可以做到,但前提是当节点确实存在时返回值是节点的字符串值,而不是节点本身.XPath

It can be done but only if the return value when the node does exist is the string value of the node, not the node itself. The XPath

substring(concat("N/A", /foo/baz), 4 * number(boolean(/foo/baz)))

如果存在则返回baz元素的字符串值,否则返回字符串N/A".

will return the string value of the baz element if it exists, otherwise the string "N/A".

概括方法:

substring(concat($null-value, $node),
          (string-length($null-value) + 1) * number(boolean($node)))

其中 $null-value 是空值字符串,$node 是选择节点的表达式.请注意,如果 $node 计算结果为包含多个节点的节点集,则使用 第一个 节点的字符串值.

where $null-value is the null value string and $node the expression to select the node. Note that if $node evaluates to a node-set that contains more than one node, the string value of the first node is used.

这篇关于我可以为 XPath 中缺失的标签创建一个值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆