本文介绍了与元素内容无关的命名空间XPath查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所见过的与名称空间无关的语法使我感到困惑.

The namespace agnostic syntax I've seen around is confusing me.

说我有

<root>
  <parent attribute="A">A<child>A</child></parent>
  <parent attribute="B">B<child>B</child></parent>
</root>

到目前为止,我知道如何:

So far I see how:

/root/parent/child/text()

翻译为:

/*[local-name()='root']/*[local-name()='parent']/*[local-name()='child']/text()

但是我正在为这样的事情而苦苦挣扎:

but i'm struggling with things like this:

/root/parent[@attribute="A"]/child/text()

或:

/root/parent[text()="B"]/child/text()

或:

/root/parent[1]/child/text()

这些如何翻译?

谢谢

还有一个:-)

<root>
        <parent>
            <childName>serverName</childName>
            <childValue>MyServer</childValue>
        </parent>
        <parent>
            <childName>ServerLocation</childName>
            <childValue>Somewhere</childValue>
         </parent>
</root>

这是怎么翻译的?

/root/parent[childName="serverName"]/childValue/text()

推荐答案

首先,我建议您不要使用此语法,尤其是在混淆时.它还可能导致错误-有关详细信息,请参见我的答案的结尾.

First, I would advise you not to use this syntax, especially if it is confusing. It can also result in errors -- see the end of my answer for details.

在XPath表达式中指定名称空间中名称的标准方法是在您的XPath引擎中注册名称空间(请参阅相应的特定于供应商的文档),然后使用绑定到已注册名称空间的前缀(例如"x"),其名称类似于x:someName

The standard way to specify in an XPath expression names that are in a namespace is to register a namespace with your XPath engine (see the respective, vendor-specific documentation) and then to use the prefix bound to the registered namespace (say "x") with names like x:someName

关于此主题的答案很多-只需使用其中之一即可.

There are plenty of good answers on this topic -- jus t use one of them.

现在,如果由于某些原因您仍然决定使用令人困惑的语法,那么:

Now, if due to some reason you still decide to use the confusing syntax, then:

/root/parent[@attribute="A"]/child/text()

使用:

/*[local-name()='root']/*[local-name()='parent' and @attribute='A']

然后:

/root/parent[text()="B"]/child/text()

使用:

/*[local-name()='root']/*[local-name()='parent' and text()='B']
                                    /*[local-name()='child']/text()

然后:

/root/parent[1]/child/text()

使用:

/*[local-name()='root']/*[local-name()='parent'][1]
                                 /*[local-name()='child']/text()

然后:

<root>
  <parent>
      <childName>serverName</childName>
      <childValue>MyServer</childValue>
  </parent>
  <parent>
      <childName>ServerLocation</childName>
      <childValue>Somewhere</childValue>
  </parent>
</root>

这是怎么翻译的?

/root/parent[childName="serverName"]/childValue/text()

使用:

/*[local-name()='root']
      /*[local-name()='parent'][*[local-name()='childName"]='serverName']
                                           /*[local-name()='childValue']/text()

记录:

如果在XML文档中有相同名称的元素属于两个不同的名称空间,则此类表达式可能不会选择所需的节点.

Such expressions may not select the wanted nodes if in the XML documents there are elements with the same local-name that belong to two different namespaces.

这篇关于与元素内容无关的命名空间XPath查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 14:18
查看更多