Subject:How in XSD do I reference the element in which a UNIQUE constraint is defined in? Author:Chilly Guevara Date:21 Jul 2011 06:53 AM Originally Posted: 21 Jul 2011 06:23 AM
I'm building an XML Schema and I've set up a unique value constraint on the NAME attribute of a certain element - but I'm unable to figure out which selector to use to reference the current context element (where UNIQUE is defined). Because of this, I've had to move the UNIQUE constraint to the parent element, and reference the target element from there. I'm really wanting to have the UNIQUE defined INSIDE the element it's constraining.
(two elements in question: CONFIG (parent) and INTERROGATOR (target/context), for reference)
Basically, I'm wanting to have the UNIQUE constraint defined inside the INTERROGATOR element, but can't figure out what selector references the context element (the element the UNIQUE constraint is defined in) - instead, I've had to move the UNIQUE constraint into the CONFIG element (INTERROGATOR's PARENT element), and use "interrogator" as the selector.
I've used ., ./, .// to reference the context element, to no avail. The issue I have with this is, that to do it in the parent element - when an error is thrown up about the NON-uniqueness of the NAME attribute in INTERROGATOR, it says that CONFIG is complaining... which can be unclear to developers.
Not too great when it comes to XPath, but there has to be a way to set up a UNIQUE constraint with a selector that references the element that the constraint is defined in, so the error message is clear that INTERROGATOR is complaining instead of it's parent.
Here is a snippet of the XML file I'm using to test the schema:
declare function local:checkUnique($e as element()) as xs:string
let $return :=
for $i at $p in $e/interrogator
if ($i/preceding-sibling::interrogator[@name = $i/@name]) then
concat( "Duplicate element 'interrogator' at position " ,$p , " value ", $i/@name, " ")
let $errors := local:checkUnique(/config)
if(count($errors) = 0) then "No assertions found"
Duplicate element 'interrogator' at position 5 value b3
Duplicate element 'interrogator' at position 6 value a1