<tfoot id='gjISD'></tfoot>
  1. <small id='gjISD'></small><noframes id='gjISD'>

    <i id='gjISD'><tr id='gjISD'><dt id='gjISD'><q id='gjISD'><span id='gjISD'><b id='gjISD'><form id='gjISD'><ins id='gjISD'></ins><ul id='gjISD'></ul><sub id='gjISD'></sub></form><legend id='gjISD'></legend><bdo id='gjISD'><pre id='gjISD'><center id='gjISD'></center></pre></bdo></b><th id='gjISD'></th></span></q></dt></tr></i><div id='gjISD'><tfoot id='gjISD'></tfoot><dl id='gjISD'><fieldset id='gjISD'></fieldset></dl></div>

      • <bdo id='gjISD'></bdo><ul id='gjISD'></ul>

      <legend id='gjISD'><style id='gjISD'><dir id='gjISD'><q id='gjISD'></q></dir></style></legend>
    1. 在 xsd 中使用密钥的正确方法

      时间:2023-06-02

      <legend id='PNQgs'><style id='PNQgs'><dir id='PNQgs'><q id='PNQgs'></q></dir></style></legend>
        • <bdo id='PNQgs'></bdo><ul id='PNQgs'></ul>

            <tfoot id='PNQgs'></tfoot>

              • <i id='PNQgs'><tr id='PNQgs'><dt id='PNQgs'><q id='PNQgs'><span id='PNQgs'><b id='PNQgs'><form id='PNQgs'><ins id='PNQgs'></ins><ul id='PNQgs'></ul><sub id='PNQgs'></sub></form><legend id='PNQgs'></legend><bdo id='PNQgs'><pre id='PNQgs'><center id='PNQgs'></center></pre></bdo></b><th id='PNQgs'></th></span></q></dt></tr></i><div id='PNQgs'><tfoot id='PNQgs'></tfoot><dl id='PNQgs'><fieldset id='PNQgs'></fieldset></dl></div>

                  <tbody id='PNQgs'></tbody>
              • <small id='PNQgs'></small><noframes id='PNQgs'>

                本文介绍了在 xsd 中使用密钥的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我正在为我正在进行的项目编写 XSD 架构.下面的架构是我从一个 microsoft 示例中获取并稍作修改的架构.

                I'm writing an XSD schema for a project I'm working on. The schema below is one I took from a microsoft example and modified slightly.

                我正在尝试使用 key 和 keyref 为一组项目声明唯一键,然后在另一部分中引用该键.

                I am trying to use the key and keyref to declare a unique key for one set of items and then refer to that key in another section.

                很长一段时间我都无法让它工作.我会编写架构并设置一个测试文档,该文档应该验证失败,因为(1)重复键和(2)引用不存在键但它一直通过的 refkeys.

                I couldn't get it to work for a long time. I would write the schema and setup a test doc which should fail validation because of (1) duplicate keys and (2) refkeys that refer to non-existant keys but it kept passing.

                在对一个示例进行了大量的修补和工作之后,我得到了它的工作.因此,我尝试将其范围缩小到示例中有效的范围,但导致它在我最初的尝试中不起作用.

                After a bunch of tinkering and working off of an example, I got it to work. So I tried to narrow it down to what worked in the example but was causing it to not work in my original attempt.

                我正在使用 .NET XmlDocument 和 XmlSchema 进行验证.我将在底部粘贴我的测试验证代码.

                I am validating using the .NET XmlDocument and XmlSchema. I'll paste my test validation code at the bottom.

                我的问题是,为什么键在下面声明时有效,但在评论中声明时无效?

                My question is, why does the key work if declared as it is below but not work if declared as it is in the comment?

                XSD:

                <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                      targetNamespace="namespace1"
                      xmlns="namespace1"
                      xmlns:r="namespace1"
                      elementFormDefault="qualified">  
                
                <xs:element name="root">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="A" type="r:AType" maxOccurs="unbounded">
                        <xs:keyref name="dummy" refer="r:pNumKey">
                          <xs:selector xpath="part"/>
                          <xs:field xpath="@ref-number"/>
                        </xs:keyref>
                      </xs:element>
                      <xs:element name="B" type="r:BType"/>
                    </xs:sequence>
                  </xs:complexType>
                
                  <!-- This works. -->
                  <xs:key name="pNumKey">
                    <xs:selector xpath="r:B/r:part"/>
                    <xs:field xpath="@key-number"/>
                  </xs:key>
                  <!--
                  This doesn't work.
                
                  <xs:key name="pNumKey">
                    <xs:selector xpath="B/part"/>
                    <xs:field xpath="@key-number"/>
                  </xs:key>
                  -->
                </xs:element>
                
                <xs:complexType name="AType">
                  <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="BType">
                  <xs:sequence>
                    <xs:element name="part" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:attribute name="key-number" type="xs:integer"/>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
                </xs:schema>
                

                验证码:

                    private static void ValidateXml(string root, string xsdFileName, string xmlFileName)
                    {
                        ValidationEventHandler veh = new ValidationEventHandler(Program_ValidationEventHandler);
                        XmlSchema schema = XmlSchema.Read(new XmlTextReader(root + xsdFileName), veh);
                
                        XmlDocument xdoc = new XmlDocument();
                        XmlReaderSettings settings = new XmlReaderSettings();
                        settings.Schemas.Add(schema);
                
                        settings.ValidationType = ValidationType.Schema;
                
                        settings.ValidationEventHandler +=
                            new ValidationEventHandler(Program_ValidationEventHandler);
                
                        XmlReader reader = XmlReader.Create(root + xmlFileName, settings);
                
                        xdoc.Load(reader);
                
                        Console.WriteLine(xdoc.SchemaInfo.Validity);
                    }
                
                    private static void Program_ValidationEventHandler(object sender, ValidationEventArgs e)
                    {
                        Console.WriteLine(string.Format("-Message:{0}", e.Message));
                    }
                

                推荐答案

                我不是 100% 确定,但我相当有把握的猜测是 selector 元素中的 XPath 表达式并不关心目标命名空间,只是您使用 xmlns 属性声明的命名空间.因此,由于您没有声明默认命名空间,它会尝试确保没有命名空间中的元素 part 是唯一的.由于文档中的 part 元素似乎位于 namespace1 命名空间中,因此在任何命名空间中都没有任何 part 元素,所以这个唯一性约束成功了.

                I'm not 100 % sure, but my pretty confident guess is that the XPath expression in the selector element does not care about the target namespace, just the namespaces you've declared with xmlns attributes. So, since you haven't declared a default namespace, it tries to ensure elements part in no namespace are unique. And since the part elements in your document seem to be in the namespace1 namespace, you don't have any part elements in no namespace, so this uniqueness constraint succeeds.

                您可以通过将属性 xmlns="namespace1" 添加到您的 schema 元素来验证我的猜测.(因为架构文档中的所有元素都有前缀,所以这是您唯一需要做的事情.)如果您的注释掉的 key 元素有效,那么这似乎是正确的解释.

                You can verify my guess by adding to your schema element the attribute xmlns="namespace1". (Since all your elements in the schema document have prefixes, this is the only thing you need to do.) If your commented-out key element works then, this would seem to be the correct explanation.

                好的,我找到了答案.事实证明,在 XPath 1.0 中,当您有一个不带前缀的名称时,它会被自动解释为不在命名空间中.在 XPath 1.0 中无法设置默认名称空间,因此当 XPath 表达式中的所有名称位于某个名称空间中时,您总是需要为其添加前缀.因此,无论您是否在架构文档中声明了默认命名空间,注释掉的 key 定义都会尝试匹配没有命名空间中的名称,这与您的文档不匹配.

                Okay, I found the answer. Turns out that, in XPath 1.0, when you have a non-prefixed name, it is automatically interpreted as being in no namespace. There is no way to set the default namespace in XPath 1.0, so you always need to prefix all names in XPath expressions when they are in some namespace. So no matter whether you've declared a default namespace in your schema document, the commented out key definition is trying to match names in no namespace, which doesn't match your document.

                这篇关于在 xsd 中使用密钥的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:从 .net 中的 xsd 模式文件中提取枚举值 下一篇:XSD 文件,从哪里获取 xmlns 参数?

                相关文章

                最新文章

                <i id='3Ltfq'><tr id='3Ltfq'><dt id='3Ltfq'><q id='3Ltfq'><span id='3Ltfq'><b id='3Ltfq'><form id='3Ltfq'><ins id='3Ltfq'></ins><ul id='3Ltfq'></ul><sub id='3Ltfq'></sub></form><legend id='3Ltfq'></legend><bdo id='3Ltfq'><pre id='3Ltfq'><center id='3Ltfq'></center></pre></bdo></b><th id='3Ltfq'></th></span></q></dt></tr></i><div id='3Ltfq'><tfoot id='3Ltfq'></tfoot><dl id='3Ltfq'><fieldset id='3Ltfq'></fieldset></dl></div>
                <tfoot id='3Ltfq'></tfoot>

                  <small id='3Ltfq'></small><noframes id='3Ltfq'>

                    <legend id='3Ltfq'><style id='3Ltfq'><dir id='3Ltfq'><q id='3Ltfq'></q></dir></style></legend>

                      <bdo id='3Ltfq'></bdo><ul id='3Ltfq'></ul>