2011-09-13 29 views
8

我可以搜索包含在特定屬性如果我用下面的XPath /xs:schema/node()/descendant::node()[starts-with(@my-specific-attribute-name-here, 'my-search-string')]Xpath用於搜索具有包含特定字符串的ANY屬性的節點?

但是一個字符串,我想搜索包含*的String

+0

你需要清楚你所說的「含有」的意思。你的意思是「等於」還是「有一個子字符串等於」?或者,如你的例子所示,「開始」? –

+0

*有一個子字符串等於 – emdog4

回答

11

示例XML的任何屬性:

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

假設我們需要選擇具有包含h的任何屬性的元素。在此示例中:element1,element2。我們可以用這樣的XPath:

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

在您的樣本:

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

是'// * [@ * [starts-with(。,'h')]]'與'// * [starts-with(@ *,'h')]相同'? – Eric

+1

@Eric,Nope。在第二個XPath節點集中,傳遞給'starts-with'函數作爲第一個參數('@ *')。 'starts-with'函數通過返回節點集中第一個節點的字符串值,即只有第一個屬性,將節點集轉換爲字符串。 –

+1

啊,這很有道理。謝謝! – Eric

7

你正在尋找的一般模式是:

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

將背景元素匹配任何屬性包含string。所以,如果你只是想搜索包含string屬性整個文檔,你會使用:

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

簡短而甜美 –

相關問題