简体   繁体   English

XML模式-顺序键

[英]XML Schema - Key in sequence

I'm trying to put a key in sequence and am being bombarded with errors. 我试图按顺序放置一个键,并被错误轰炸。 It seems I can't do this. 看来我做不到。 Additionally, I am being told my key needs a field and selector, which I have been able to find very little documentation on. 另外,有人告诉我我的钥匙需要一个字段和选择器,而我只能在上面找到很少的文档。 (In fact, in desperation, I downloaded Liquid XML Studio [vs. Notepad] and searched it's help files. . . to find NO even reference to the word "selector") Regardless - what is another method I can use to guarantee a unique, not null value for an element, within a sequence? (实际上,无奈之下,我下载了Liquid XML Studio [vs. Notepad]并搜索了它的帮助文件...甚至找不到对“选择器”一词的引用。)不管怎样,我可以使用另一种方法来保证唯一性,不是序列中元素的null值? (The content in question is a string) (有问题的内容是字符串)

Please note that without the key element, the schema validates. 请注意,如果没有key元素,该模式将进行验证。

The error 错误

Invalid per cvc-complex-type.1.2.4: element {http://www.w3.org/2001/XMLSchema}:key not allowed here (1) in element {http://www.w3.org/2001/XMLSchema}:sequence, expecting [{http://www.w3.org/2001/XMLSchema}:annotation, $, {http://www.w3.org/2001/XMLSchema}:element, {http://www.w3.org/2001/XMLSchema}:group, {http://www.w3.org/2001/XMLSchema}:choice, {http://www.w3.org/2001/XMLSchema}:sequence, {http://www.w3.org/2001/XMLSchema}:any]: 每个cvc-complex-type.1.2.4无效:元素{http://www.w3.org/2001/XMLSchema}:此处不允许在元素{http://www.w3.org/2001(1)中使用键/ XMLSchema}:序列,期望[{http://www.w3.org/2001/XMLSchema}:注释,$,{http://www.w3.org/2001/XMLSchema}:元素,{http:/ /www.w3.org/2001/XMLSchema}:组,{http://www.w3.org/2001/XMLSchema}:选择,{http://www.w3.org/2001/XMLSchema}:序列, {http://www.w3.org/2001/XMLSchema}:any]:

The schema 模式

<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 targetNamespace="READOO" elementFormDefault="qualified">

  <xs:element name="ReferralCollection">
    <xs:complexType>
      <xs:sequence>
    <xs:key name="URL" type="string" />
        <xs:element maxOccurs="unbounded" name="keyword">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute name="occurrences" type="xs:int" use="required" />
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

The key element is not supposed to go inside the sequence element. 不应将key元素放入sequence元素内。 Instead, you put it as a child of the xs:element that defines ReferralCollection . 相反,您将其作为定义ReferralCollectionxs:element的子级。 The selector and field are child elements of the key element that define what is supposed to be unique. selectorfieldkey元素的子元素,这些元素定义了应该是唯一的。 Both are XPath expressions. 两者都是XPath表达式。 The selector value sets up the XPath context, and the field elements (there can be more than one) define the combination of values that are supposed to be unique. selector值设置XPath上下文,并且field元素(可以有多个)定义应该唯一的值的组合。 So in your case, the key element might look something like this: 因此,在您的情况下, key元素可能看起来像这样:

<xs:key name="URL">
  <xs:selector xpath="."/>
  <xs:field xpath="rd:keyword"/>
</xs:key>

The selector here selects the element itself, that is, ReferralCollection . 此处的选择selector选择元素本身,即ReferralCollection Then, in that context, the field element denotes the values of the keyword elements, which are thus constrained to be unique. 然后,在该上下文中, field元素表示keyword元素的值,因此将其约束为唯一的。 The value of an element in XPath is its text content. XPath中元素的值是其文本内容。

I haven't done much with key elements (or XML Schema in general), so some details above might not be correct, but the basic idea is what I explained, so it should be easy to fix any mistakes I made. 我没有对key元素(或一般来说为XML Schema)做很多事情,因此上面的一些细节可能不正确,但是基本思想是我所解释的,因此应该很容易解决我犯的任何错误。 Oh, also note that I used the rd prefix for your READOO namespace; 哦,还请注意,我为您的READOO名称空间使用了rd前缀; in my experience using explicit prefixes is always best when dealing with XPath, so you need the appropriate xmlns:rd attribute in your schema to make this work. 以我的经验,在处理XPath时,最好使用显式前缀,因此您需要在架构中使用适当的xmlns:rd属性来使其正常工作。

EDIT: So it seems you want something like this: 编辑:所以看来您想要这样的东西:

<xs:element name="ReferralCollection">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="URL" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="stem" type="xsd:string"/>
            <xs:element name="keyword" maxOccurs="unbounded">
            ...
            </xs:element>
          ...
  </xs:complexType>

  <xs:key name="URL">
    <xs:selector xpath="./URL"/>
    <xs:field xpath="stem"/>
  </xs:key>
</xs:element>

Is this closer to what you meant? 这更接近您的意思了吗? It has URL elements, one or more, each containing a stem element followed by one or more keyword elements. 它具有一个或多个URL元素,每个元素都包含一个stem元素,后跟一个或多个keyword元素。 The key constraint should make the contents of the stem elements unique. key约束应使stem元素的内容唯一。

Please, see if this links can help you: 请查看此链接是否可以帮助您:

EDIT : I built schema below several years ago; 编辑 :我几年前建立架构; please, see it fits in your problem: 请查看适合您的问题:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="urn:asp4d"
    xmlns:asp4d="urn:asp4d">
  <xs:element name="ASP4Developers">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Links" minOccurs="1" maxOccurs="1">
          <xs:keyref name="kUserId" refer="asp4d:UserId">
            <xs:selector xpath="Link"/>
            <xs:field xpath="@UserId"/>
          </xs:keyref>
        </xs:element>
      </xs:sequence>
    </xs:complexType>

    <xs:key name="UserId">
      <xs:selector xpath="Users/User"/>
      <xs:field xpath="@UserId"/>
    </xs:key>

  </xs:element>

</xs:schema>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM