用于搜索具有包含特定字符串的ANY属性的节点的Xpath?

时间:2011-09-13 16:24:16

标签: xml xslt search xpath

如果我使用以下XPath,我可以搜索特定属性中包含的字符串 /xs:schema/node()/descendant::node()[starts-with(@my-specific-attribute-name-here, 'my-search-string')]

但是,我想搜索包含* a String

的任何属性

3 个答案:

答案 0 :(得分:13)

示例XML:

<root>
  <element1 a="hello" b="world"/>
  <element2 c="world" d="hello"/>
  <element3 e="world" f="world"/>
</root>

假设我们需要选择包含h的任何属性的元素。在此示例中:element1element2。我们可以使用这个XPath:

//*[@*[starts-with(., 'h')]]

在您的样本中:

/xs:schema/node()/descendant::node()
    [@*[starts-with(@my-specific-attribute-name-here, 'my-search-string')]]

答案 1 :(得分:10)

您正在寻找的一般模式是:

@*[contains(., 'string')]

将匹配包含string的上下文元素的任何属性。因此,如果您只想在整个文档中搜索包含string的属性,则可以使用:

//@*[contains(., 'string')]

答案 2 :(得分:-1)

这是您要寻找的:

//*[@*[starts-with(., 'h')]]