XPath:默认为“节点A",如果“节点B"不为空,则选择“节点B" [英] XPath: Default to 'Node A', select 'Node B' instead if 'Node B' is not empty

查看:149
本文介绍了XPath:默认为“节点A",如果“节点B"不为空,则选择“节点B"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个执行以下操作的XPath表达式:

I need to create an XPath expression that does the following:

  • 默认情况下返回"NodeA"内部的元素
  • 如果不为空,则返回"NodeB"内部的元素.

这里有一些示例XML,以便可以清楚地看到我的目标结构(我正在使用MS InfoPath):

Here is some sample XML so that my target structure can be clearly seen (I am using MS InfoPath):

<?xml version="1.0" encoding="UTF-8"?><?mso-infoPathSolution solutionVersion="1.0.0.10" productVersion="14.0.0" PIVersion="1.0.0.0" href="file:///C:\Documents%20and%20Settings\Chris\Local%20Settings\Application%20Data\Microsoft\InfoPath\Designer3\9016384cab6148f6\manifest.xsf" ?><?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.3"?>
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-09-07T14:19:10" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-us">
<my:NodeASection>
    <my:NodeA>2012-09-13</my:NodeA>
</my:NodeASection>
    <my:NodeBSection>
        <my:NodeBGroup>
            <my:NodeB>2012-09-14</my:NodeB>
        </my:NodeBGroup>
    </my:NodeBSection>
</my:myFields>

此XPath表达式可用于评估NodeB是否存在文本:boolean(//my:NodeB[(text())])

This XPath expression can be used to evaluate NodeB for the existence of text: boolean(//my:NodeB[(text())])

我听说过贝克尔方法",但是当两个节点都存在时,我不确定该如何应用.我非常是XPath的新手,感谢能提供的任何帮助.

I have heard of the "Becker Method" but I'm not sure how that applies when both nodes exist. I'm very new to XPath and appreciate any help that can be offered.

推荐答案

如果该XPath表达式存在(并且具有文本内容),则该XPath表达式返回NodeB;在其他情况下,该XPath表达式返回NodeA:

This XPath expression returns NodeB if it exists (and has text content) and NodeA in the other case:

//my:NodeB[text()] | //my:NodeA[text() and not(//my:NodeB[text()])]

如果要获取所有子元素,可以将/*附加在所选节点之后,像这样

If you want to get all sub-elements you can append /* after the selected node, like this

//my:NodeB[text()]/* | //my:NodeA[text() and not(//my:NodeB[text()])]/*

这篇关于XPath:默认为“节点A",如果“节点B"不为空,则选择“节点B"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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