8

我一直在寻找答案,但找不到:

  • 在 Visual Studio 2010 中是否有通过Intellisense的 XSD 密钥/keyref 验证支持?
  • 如果是这样,如何使它工作?
  • 如果没有,Visual Studio 中是否有(内置)方法可以在具有 XSD 架构的 XML 中进行键/引用验证?

谢谢!

更新:请注意,问题不是关于如何验证具有 XSD 文件的 XML。我专门询问 Visual Studio 中的key/keyref intellisense/whatever 支持,这似乎根本没有添加。

4

3 回答 3

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

于 2015-09-03T21:56:26.667 回答
1

VS2010 目前不支持该功能,VS2012 也不支持(根据 MS 技术支持)。

也许他们会在未来的版本中支持它......

于 2012-11-07T19:28:37.327 回答
1

刚刚在 VS 2013 和 VS 2015 中尝试过。他们现在确实验证了密钥/引用密钥。但它在父级显示警告。

但正如@antiduh 所说,他们仍然不检查 ​​xsd 文件。所以你真的需要确保 xsd 中的 key/refkey 是正确的

我花了几个小时才找出一个简单的样本。即使是MSDN 上的示例也不起作用。我不得不稍微修改一下。

首先,确保您知道如何让 Visual Studio 针对您选择的 xsd 验证 xml

然后使用以下示例 xsd 和 xml 进行密钥/引用密钥验证。请注意,警告是关闭根元素,而不是违反 key/ref 规则的元素。

xsd 文件如下所示:

<?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>

xml 文件如下所示:

<?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-->
于 2016-09-08T21:20:51.233 回答