8

I've been searching for the answer and could not find one:

  • Is there a XSD key/keyref validation support via Intellisense in Visual Studio 2010?
  • If so, how to make it work?
  • If no, is there a (built-in) way in Visual Studio to do key/references validation in an XML having an XSD schema at all?

Thanks!

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

Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
  • possible duplicate of [can you validate xsd against xml in vs 2010?](http://stackoverflow.com/questions/8988593/can-you-validate-xsd-against-xml-in-vs-2010) – DaveShaw Mar 09 '12 at 18:05
  • 2
    Did you ever find an answer to this? It seems to support this with the XSD XSD after all... – Duncan Watts Mar 15 '12 at 12:25
  • No answer yet, I am thinking that as more and more people upvote this, there is no answer. Unfortunately. – Trident D'Gao Oct 29 '12 at 00:37
  • 1
    are you trying to get and then see in VS only the enumerate options? – Liran Nov 05 '12 at 16:13
  • @liran, no I am trying to get the key/keyref mechanism of the XSD standard to work in the XML editor of the VS2010 – Trident D'Gao Nov 06 '12 at 09:12
  • by my answer, I meant, whether there is any way to dump all the key values into the intellisence combobox, once I am filling in the reference - the actual validation IS supported - if you fill in the ref that has not been added as a key, you will see a warning – Serge Apr 08 '13 at 13:32

3 Answers3

2

Visual Studio 2012 now supports validation of XML document instances that are subject to key/keyref constraints as defined in a referenced schema.

However, Visual Studio doesn't give any errors for the schema document itself, when that schema document uses key/keyref incorrectly - independent of whether some document meets the schema.

Specifically, key/keyref elements as defined in the schema must use namespaces in the selector xpath statements, according to the following SO post:

https://stackoverflow.com/a/4492004/344638

To quote:

Furthermore - this is a gotcha - key constraints don't recognise the default namespace. You must always prefix every part of the selector xpath with the namespace prefix of the element you're looking up. If you don't have a namespace prefix - tough, you'll need to add one. This is a limitation of the standard.

The following SO post provides a complete example of a schema that uses key/keyref, an XML document instance, and a manual C#-based validater. The schema and the XML document instance validate correctly in Visual Studio - Visual Studio will generate errors if the document violates the schema's key/keyref constraints:

https://stackoverflow.com/a/2866428/344638

Community
  • 1
  • 1
antiduh
  • 11,853
  • 4
  • 43
  • 66
1

The functionality is not currently supported in VS2010, neither it is in VS2012 (as per MS technical support).

Maybe they will support it in future versions...

Serge
  • 1,027
  • 14
  • 21
1

Just tried in VS 2013 and VS 2015. They did validate key/refkey now. It showed up warnings at the parent level though.

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

It took my hours to figure out a simple sample. Even the sample on MSDN does not work. I had to modify it a little bit.

First, make sure you know how to let Visual Studio validate an xml against an xsd of your choice.

Then use the following sample xsd and xml for key/refkey validation. Mind that the warning is on the closing of root element, instead of on the element that violates the key/ref rule.

The xsd file looks like this:

<?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-->
Community
  • 1
  • 1
Bruce Wang
  • 81
  • 2