I have the below XML structure :
<Books>
<Book>
<ItemReference>
<ClassificationCode listID="Name">Book1</ClassificationCode>
<ClassificationCode listID="Property2"/>
<ClassificationCode listID="Property3"/>
</ItemReference>
</Book>
<Book>
<ItemReference>
<ClassificationCode listID="Name">Book2</ClassificationCode>
<ClassificationCode listID="Property2"/>
<ClassificationCode listID="Property3"/>
</ItemReference>
</Book>
<Book>
<ItemReference>
<ClassificationCode listID="Name">Book1</ClassificationCode>
<ClassificationCode listID="Property2"/>
<ClassificationCode listID="Property3"/>
</ItemReference>
</Book>
<Book>
<ItemReference>
<ClassificationCode listID="Name">Book4</ClassificationCode>
<ClassificationCode listID="Property2"/>
<ClassificationCode listID="Property3"/>
</ItemReference>
</Book>
</Books>
What is the proper way to form my for loop using XQuery,in order to create a validation rule to return as log a validation error when it founds duplicates in ClassificationCode[@listID = 'Name']/text()
where the /text() = ('Book1', 'Book4')
? That means i don't want to use the same validation for Book2. my problem is that i care about particular items if they will be double or not. To give more details my Validation XQuery looks like this :
declare function local:BooksValidation (
$books as element()*,
{
for $book in $books
let $ItemRef := $book/ItemReference
return (
if (fn:exists($ItemRef)) then ()
else
local:Issue($error, "No Items found", $currentPath)
)
}
declare function local:Issue (
$errorCode as xs:string,
$message as xs:string,
$xpath as xs:string) as xs:string*
{
concat($errorCode, ': ', $message, ' (XPath : ', $xpath, ')')
};
and i want to include in this for-loop an exact same if-statement that will check if there are double items
ClassificationCode[@listID = 'Name'][. = ('Book1', 'Book4')]
as the first item hasClassificationCode listID="FulfillmentItemCode"
. So please explain in more detail what you are looking for and how the usual XQuery tools with grouping and/or distinct-values don't help. - Martin Honnen