How to Use not contains with Attributes and Elements in XPath/XQuery
Автор: vlogize
Загружено: 2025-10-11
Просмотров: 0
Discover how to effectively implement `not contains` in XPath and XQuery to extract elements based on attributes. Learn with practical examples here!
---
This video is based on the question https://stackoverflow.com/q/67552146/ asked by the user 'Lyaso' ( https://stackoverflow.com/u/10668686/ ) and on the answer https://stackoverflow.com/a/67552614/ provided by the user 'David Denenberg' ( https://stackoverflow.com/u/6295455/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: How to use not contains with attributes and elements
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
How to Use not contains with Attributes and Elements
When working with XML data, it’s common to face challenges in extracting specific elements based on conditions. A frequent scenario arises when we need to filter out items that do or do not exist in a particular list. This guide addresses how to use not contains in XPath and XQuery to filter elements based on attributes, specifically when you want to find numbers not present in a given set.
The Problem
Let’s consider an XML snippet where we have two sets of numbers: a list of new products and another list of products. For instance:
[[See Video to Reveal this Text or Code Snippet]]
In this scenario, we are looking to identify which numbers from the newproducts list are not present in the products list. In this case, numbers 1 and 5 are the numbers we want to extract.
Why Your Current Approach Doesn't Work
A common first attempt might use an inequality operator, such as:
[[See Video to Reveal this Text or Code Snippet]]
However, this approach doesn’t yield the desired results because it doesn’t accurately check whether there are no matching items in the list.
The Correct Solution
To correctly filter out the number elements where their id does not exist in the products list, we need to use the not function instead. Here’s how to do it effectively:
Step-by-Step Code Solution
Wrap Input in a Root Node: Integrate your XML snippet within a single root node for proper parsing.
Use not Function: Instead of using !=, we will use not(@ id/string() = /products/number/text()) to achieve the intended filtering.
Here’s the revised code:
[[See Video to Reveal this Text or Code Snippet]]
Explanation of the Code
Let Block: We encapsulate our XML structure into a let block for structure clarity.
Filtering New Products: The for $z in loop iterates through each number in newproducts and applies the filtering condition.
Return Clause: The return clause specifies what format the output will have. Notice how we are now returning just the id of the numbers.
Expected Output
Using this correct code will yield the following output:
[[See Video to Reveal this Text or Code Snippet]]
This effectively lists the numbers from the newproducts that do not exist in the products list.
Conclusion
Using not in XPath and XQuery is a practical way to filter elements based on attributes. By following the structured approach outlined in this post, you can easily address scenarios where you need to find elements that do not match against a defined list. This method is both efficient and straightforward for anyone dealing with XML data manipulations.
If you have any further questions or need more examples, feel free to reach out or leave a comment below!
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: