简体   繁体   中英

How to go through each node in a xml file and return true if the name of the node matches a string and false otherwise using XQuery and XPath?

I want to loop through each node in a xml document and return true if the node is equal to a certain string value and false if not. Output should be similar to:

True False False True True

True being where the node name matches a string.

Thanks.

If we take your question literally, it's

(//node()) ! (if (. = 'a certain string value') then 'True' else 'False'

But I suspect that by "node" you really mean element (in which case it's //* ), and it's possible that by "matches" you mean something other than "=".

I understand your question as //*/(. instance of element(foo) or for $el in //* return $el instance of element(foo) (where foo would be the name of the element you are looking for).

You couldn't pass it in as a string however, for that you would need declare $name as xs:string external := 'foo'; //*/(local-name() = $name) declare $name as xs:string external := 'foo'; //*/(local-name() = $name) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM