[xquery-talk] returning elements without duplicates, based on an XML schema and using an attribute as context node

Michael Kay mike at saxonica.com
Thu Mar 26 17:15:52 PST 2009



	From: talk-bounces at x-query.com [mailto:talk-bounces at x-query.com] On
Behalf Of Philip Cantin
	Sent: 26 March 2009 15:50
	To: talk at x-query.com
	Subject: [xquery-talk] returning elements without duplicates,based
on an XML schema and using an attribute as context node
	Hello All,
	I am new to this mailing list, as well as fairly new to XQuery and
XPath (having learned it for a month or two). For the most part, I haven't
had too much trouble editing an old XQuery document, until now.
	I am trying to access the 'maxOccurs' attribute within the for-loop,
where $ref is the 'ref' attribute, and return distinct <xf:bind> elements
with no duplicate elements. However, I can't seem to set the $max variable
properly using XPath functions, without getting an error. I imagine it must
have something to do with setting/finding/using the current context. 
	The XQuery code looks like this:
	if (exists($schema//xs:element[exists(@maxOccurs) and @maxOccurs !=

## note the exists(@maxOccurs) is redundant here. @maxOccurs != 1 always
returns false if @maxOccurs does not exist.

	then (
	   for $ref in
distinct-values($schema//xs:element[exists(@maxOccurs) and @maxOccurs != '1'
and @maxOccurs ne 'unbounded']/@ref)

## it might be worth using a variable here to save doing the search twice
(or writing it twice).

## if your query is schema-aware, then @ref is a QName. Otherwise, it is a
string in the lexical form of a QName. Theoretically, you should be looking
for unique QNames, not unique strings.

	      let $name := string($ref)
	      let $max := string($ref//following-sibling::maxOccurs)

## This is pretty strange. $ref is an atomic value, so you can't use it on
the lhs of "/" or "//". Perhaps you wanted the attribute node? But which
attribute node: you're dealing with a group of @ref attributes that have the
same value.

At this point I'm afraid I'm stuck, because you haven't given any clues
about what the query is supposed to do, and it's hard to reverse engineer
the requirements from incorrect code.
>	Can someone show me the proper way to access the maxOccurs attribute
from the ref attribute, and/or provide me with a link that can help me
understand this situation a little more?

If $ref were a ref attribute (which it isn't), then you could get the
maxOccurs attribute as $ref/../@maxOccurs

Michael Kay

More information about the talk mailing list