Visual Studio 2010中的XSD key / keyref intellisense验证支持

时间:2022-11-14 08:07:08

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?
  • 在Visual Studio 2010中是否通过Intellisense提供XSD密钥/ keyref验证支持?
  • 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?
  • 如果不是,Visual Studio中是否有(内置)方法在具有XSD架构的XML中进行密钥/引用验证?

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.

更新:请注意,问题不在于如何验证具有XSD文件的XML。我特别询问了关键/ keyref intellisense / Visual Studio中的任何支持,它似乎根本没有添加。

3 个解决方案

#1


2  

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

Visual Studio 2012现在支持验证XML文档实例,这些实例受引用模式中定义的key / keyref约束。

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.

但是,当架构文档错误地使用key / keyref时,Visual Studio不会为架构文档本身提供任何错误 - 与某些文档是否符合架构无关。

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

具体来说,模式中定义的key / keyref元素必须在选择器xpath语句中使用名称空间,根据以下SO帖子:

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

https://*.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.

此外 - 这是一个问题 - 密钥约束无法识别默认命名空间。您必须始终在选择器xpath的每个部分前面添加您正在查找的元素的名称空间前缀。如果你没有命名空间前缀 - 很难,你需要添加一个。这是标准的限制。

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:

以下SO帖子提供了使用key / keyref,XML文档实例和基于C#的手动验证程序的模式的完整示例。架构和XML文档实例在Visual Studio中正确验证 - 如果文档违反架构的key / keyref约束,Visual Studio将生成错误:

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

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

#2


1  

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

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

Maybe they will support it in future versions...

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

#3


0  

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

刚刚在VS 2013和VS 2015中尝试过。他们现在确实验证了key / refkey。尽管如此,它在父级出现了警告。

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.

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

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.

花了我几个小时才弄清楚一个简单的样本。甚至MSDN上的示例都不起作用。我不得不稍微修改一下。

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

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

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.

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

The xsd file looks like this:

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>

And the xml file looks like this:

并且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-->

#1


2  

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

Visual Studio 2012现在支持验证XML文档实例,这些实例受引用模式中定义的key / keyref约束。

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.

但是,当架构文档错误地使用key / keyref时,Visual Studio不会为架构文档本身提供任何错误 - 与某些文档是否符合架构无关。

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

具体来说,模式中定义的key / keyref元素必须在选择器xpath语句中使用名称空间,根据以下SO帖子:

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

https://*.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.

此外 - 这是一个问题 - 密钥约束无法识别默认命名空间。您必须始终在选择器xpath的每个部分前面添加您正在查找的元素的名称空间前缀。如果你没有命名空间前缀 - 很难,你需要添加一个。这是标准的限制。

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:

以下SO帖子提供了使用key / keyref,XML文档实例和基于C#的手动验证程序的模式的完整示例。架构和XML文档实例在Visual Studio中正确验证 - 如果文档违反架构的key / keyref约束,Visual Studio将生成错误:

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

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

#2


1  

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

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

Maybe they will support it in future versions...

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

#3


0  

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

刚刚在VS 2013和VS 2015中尝试过。他们现在确实验证了key / refkey。尽管如此,它在父级出现了警告。

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.

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

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.

花了我几个小时才弄清楚一个简单的样本。甚至MSDN上的示例都不起作用。我不得不稍微修改一下。

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

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

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.

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

The xsd file looks like this:

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>

And the xml file looks like this:

并且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-->