开发者

element occur at specified number

here is my xml document

<root>
 <num>3</num>
 <a>
  <b>
   <c n="m">开发者_Go百科</c>
   <c></c>
   <c n="e"></c>
  </b>
  <b>
   <c n="s"></c>
   <c n="w"></c>
   <c></c>
  </b>
  <b>
   <c n="q"></c>
   <c></c>
   <c n="u"></c>
  </b>
 </a>
</root>

I want to created a xml Schema.

Element c in b must occurs only n time.(n is in num element)

I think it use key, keyref or unique.


It is not possible.

Key, keyref and unique will not help. Unique will ensure that you have no duplicate items. And key and keyref will ensure that the elements in place B match those in place A.

The maxOccurs restriction requires a simple integer argument. It is not possible to calculate the integer by any kind of Xpath expression. See here: http://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints

Update

If you want to solve your problem with unique, key and key ref you have to create an number index in an attribute. The following image illustrates the schema:

element occur at specified number

You have to add an attribute to your c nodes which references a master attribute index. In this example it is stored in multiple num elements. You can put an integer restriction, a unique and a key constrain on /root/num@count. After that you create a reference constrain from /root/a/b/c@count to /root/num@count. This will create a max constrain. You can have as many c nodes as num nodes but you can have less, too.

This is the schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="root">
    <xs:annotation>
      <xs:documentation>Comment describing your root element</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="num" maxOccurs="unbounded">
          <xs:complexType>
            <xs:attribute name="count" type="xs:unsignedInt" use="required"/>
          </xs:complexType>
        </xs:element>
        <xs:element name="a" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="b" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="c" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:attribute name="count" type="xs:integer" use="required"/>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="num_unique">
      <xs:selector xpath="num"/>
      <xs:field xpath="@count"/>
    </xs:unique>
    <xs:key name="num_key">
      <xs:selector xpath="num"/>
      <xs:field xpath="@count"/>
    </xs:key>
    <xs:keyref name="num_ref" refer="num_key">
      <xs:selector xpath="a/b/c"/>
      <xs:field xpath="@count"/>
    </xs:keyref>
  </xs:element>
</xs:schema>

And this would be a valid file:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="root.xsd">
  <num count="1"/>
  <num count="2"/>
  <num count="3"/>
  <a>
    <b>
      <c count="1"/>
    </b>
    <b>
      <c count="1"/>
      <c count="2"/>
    </b>
  </a>
</root>

It will become invalid if you add a <c count="4"/>. Right now I have no idea how to make it an identity reference. Maybe it is possible to reverse the reference direction...

But never the less it is no good idea to place a constrain into the same file as the data itself. If you do not trust your data source it does not make much sense to ask your data source for a self given constrain. You must put the constrain into the schema or extend the validation by some kind of program logic. For example you can count the number of all c nodes in their parent and check if they are all the same or are all equal to a given number. That would make much more sense, because that is the only secure way to enforce the constrain.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜