xml 用于查找标记名称包含“名称”的元素的 XPath 表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14931499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:53:07  来源:igfitidea点击:

XPath expression to find elements whose tag name contains 'Name'

xmlxpathcontains

提问by user2082317

I am a newcomer to XPath.

我是 XPath 的新手。

I am looking for a way to get all elements whose tag name contains a particular string.

我正在寻找一种方法来获取标签名称包含特定字符串的所有元素。

For example, if I have XML like below, I want to get all the elements whose tag name contains the word 'Name'. i.e., I want to fetch the following elements: <SquareName>, <RectangleName>, and <ParallelogramName>.

例如,如果我有像下面这样的 XML,我想获取其标记名称包含单词“名称”的所有元素。也就是说,我希望获取以下元素:<SquareName><RectangleName>,和<ParallelogramName>

I tried some combinations of name(), contains()etc., but it did not work. Please suggest.

我试过的一些组合name()contains()等等,但没有奏效。请建议。

<Objects>
 <Four-Sided>
   <Square>
      <SquareName>ABCD</SquareName>
      <Length>4</Length>
      <Height>4</Height>
      <Colour>Blue</Colour>
   </Square>
   <Rectangle>
      <RectangleName>EFGH</RectangleName>
      <Length>10</Length>
      <Height>6</Height>
      <Colour>Brown</Colour>
   </Rectangle>
   <Parallelogram>
      <ParallelogramName>WXYZ</ParallelogramName>
      <Length>12</Length>
      <Height>4</Height>
      <Colour>Black</Colour>
   </Parallelogram>
</Four-Sided>
</Objects>

回答by Jens Erat

For an XPath solution:

对于 XPath 解决方案:

//*[contains(local-name(), 'Name')]

回答by farooq

Since there is no Namespace prefix, you can also use

由于没有命名空间前缀,您也可以使用

//*[contains(name(), 'Name')]