It can not be done in xsd. It lacks full Xpath support. :(
http://www.xml.com/pub/a/2002/04/10/beyondwxs.html
The solution is to apply xslt to validate complex constraints.
I hope this is integrated into xsd very soon! So I don't need to transform/evaluate a document twice.
I opt for first Xslt and then Xsd validation.
Use the xslt for transforming to constrained xml and then validate this xml with the xsd in order to know weather or not the constraints are all valid.
Doing xslt first, will allow the removal of all redundant data before validation.
e.g.
Xslt
-
<items>
-
<xsl:copy-of select="/Items/Item[@Id='2' and @Value != '']/>
-
<items>
-
Xsd
-
-
<xs:element name="Items">
-
<xs:complexType>
-
<xs:sequence>
-
<xs:element name="Item">
-
<xs:complexType>
-
<xs:attribute name="Id" type="xs:positiveInteger" use="required" />
-
</xs:complexType>
-
</xs:element>
-
</xs:sequence>
-
</xs:complexType>
-
</xs:element>
-