2015-10-13 40 views
2

如果只知道部分值,那麼使用SQL您可以使用LIKE語句來獲取結果。但在XQuery中,我似乎無法以同樣的方式搜索我的XML文件。部分字符串搜索XQuery/XPath(如SQL中的LIKE語句)

這是我試過,但不起作用:

for $c in /parent/child where $c/element="*partial strin*" return $c/element 
for $c in /parent/child where $c/element="%partial strin%" return $c/element 

回答

5

您根本不需要for表達式,您可以只使用謂詞。對於一個確切的字符串匹配還有的contains功能:

/parent/child/element[contains(., "partial strin")] 

對於更復雜的匹配,你可以使用正則表達式的matches功能:

/parent/child/element[matches(., "^partial\s+strin")] 
1

你應該看看在開始 - 用()和包含()函數,這些都能夠局部搜索。鑑於你上面的例子,這將是

//parent/child[contains(@element, 'partial stri')] 

注意到我相信@Element是孩子的屬性,如果你只想要搜索的子元素的文本與單取代@Element「」