Support for XSD / keyref intellisense key verification in Visual Studio 2010

I searched the answer and did not find it:

  • Is there any support for XSD / keyref validation using Intellisense in Visual Studio 2010?
  • If so, how to do it?
  • If not, is there a (built-in) way in Visual Studio to check key / links in XML that has an XSD schema at all?

Thanks!

UPDATE: Please note that the question is not how to validate XML having an XSD file. I am specifically asking about key / keyref intellisense / any support in Visual Studio that doesn't seem to be added at all.

+8
xml visual-studio-2010 xsd intellisense keyref
source share
3 answers

Visual Studio 2012 now supports validating instances of XML documents that are subject to key / keyref constraints, as defined in the referenced schema.

However, Visual Studio does not give any errors for the schema document itself when this schema document uses key / keyref incorrectly - regardless of whether any document matches the schema.

In particular, the key / keyref elements defined in the schema must use namespaces in xpath select statements, according to the following SO message:

stack overflow

Quote:

Additionally, this key constraint does not recognize the default namespace. You should always prefix each part of the xpath with the namespace prefix of the element you are looking for. If you don't have a namespace prefix - tough, you need to add it. This is a limitation of the standard.

The next SO post is a complete example of a schema that uses key / keyref, an instance of an XML document, and a manual validator in C #. The schema and instance of the XML document are validated correctly in Visual Studio - Visual Studio will generate errors if the document violates the restrictions on the schema key / keyref:

stack overflow

+2
source share

Functionality is currently not supported in VS2010, and it is not in VS2012 (according to MS technical support).

Perhaps they will support it in future versions ...

+1
source share

Just tried in VS 2013 and VS 2015. They really confirmed the / refkey key now. He found warnings at the parent level.

But, as @antiduh said, they still do not check the xsd file. So you really need to make sure the / refkey in xsd is correct .

It took me a while to figure out a simple example. Even the sample in MSDN does not work. I had to change it a bit.

First, make sure you know to let Visual Studio check the xml on xsd of your choice .

Then use the following xsd and xml example to verify the / refkey switch. Keep in mind that the warning is on closing the root element, and not on the element that violates the / ref key rule.

The xsd file is as follows:

<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="namespace1" xmlns:r="namespace1" elementFormDefault="qualified"> <xs:element name="root"> <xs:complexType> <xs:sequence> <xs:element name="A" type="r:A" maxOccurs="unbounded"/> <xs:element name="B" type="r:B"/> </xs:sequence> </xs:complexType> <xs:keyref name="dummy" refer="r:pNumKey"> <xs:selector xpath="r:A/r:part"/> <xs:field xpath="@ref-number"/> </xs:keyref> <xs:key name="pNumKey"> <xs:selector xpath="r:B/r:part"/> <xs:field xpath="@key-number"/> </xs:key> </xs:element> <xs:complexType name="A"> <xs:sequence> <xs:element name="part" maxOccurs="unbounded"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="ref-number" type="xs:integer"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> <xs:complexType name="B"> <xs:sequence> <xs:element name="part" maxOccurs="unbounded"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="key-number" type="xs:integer"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:schema> 

And the xml file looks like this:

 <?xml version="1.0" encoding="utf-8" ?> <root xmlns="namespace1"> <A> <!-- if the ref-number is equal to one of the key-number, the validation will pass --> <part ref-number="1"/> </A> <A> <!-- if the ref-number is not equal to one of the key-number, the validation will give error --> <part ref-number="5"/> </A> <B> <part key-number="1"/> <part key-number="2"/> <part key-number="3"/> </B> </root><!--you will see warnings here--> 
0
source share

All Articles