XSD: Как проверить XML-файл согласно значению некоторого тега?

Полномочия два в двоичном файле похожи на это:

1: 0001
2: 0010
4: 0100
8: 1000

Примечание, что существует всегда точно 1 набор битов. Единственное исключение с целым числом со знаком. например, 8-разрядное целое число со знаком со значением-128 похоже:

10000000

Поэтому после проверки, что число больше, чем нуль, мы можем использовать умное, немного взламывают для тестирования того, и только один бит установлен.

bool is_power_of_2(int x) {
    return x > 0 && !(x & (x−1));
}

Для большего количества битового жонглирования см. здесь .

5
задан 23 February 2010 в 04:21
поделиться

4 ответа

It's a known fact that this is a handycap of XML schema. But I would appreciate your approach of trying the tag. It could be successful if your conditions were something like this:

  1. If is required and appears first then isn't required (and appears as second tag)
  2. If is required and appears first then isn't required (and appears as second)

The code is:

<xs:element name="parent">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="a" maxOccurs="unbounded">
        <xs:complexType>
          <xs:choice>
            <xs:sequence>
              <xs:element name="tag1" type="xs:boolean" />
              <xs:element name="tag2" type="xs:string" minOccurs="0" />
            </xs:sequence>
            <xs:sequence>
              <xs:element name="tag2" type="xs:string" />
              <xs:element name="tag1" type="xs:boolean" minOccurs="0" />
            </xs:sequence>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
3
ответ дан 13 December 2019 в 22:09
поделиться

You cannot validate things like that with XSD.

XML schema is not designed and not intended to check "intra-tag" relationships, e.g. "tag2 must be present if tag1's value is 'Y'" - just cannot be done, sorry.

If you need to check these kind of conditions, you'll have to look at Schematron to do that.

6
ответ дан 13 December 2019 в 22:09
поделиться

Unfortunately this problem cannot be fixed using XSD. The reason is that XSD can only be used to define the structure (syntax) of XML-Files. What you would like to do is to couple the syntax to some semantic properties (some TAG must have a certain content to decide on the syntax of some TAGS nearby).

1
ответ дан 13 December 2019 в 22:09
поделиться

Это невозможно с XSD .. Но, кстати, вы можете обойти что-то вроде Infant-programmer, если требования немного удобны, как показано в ее примере ..

0
ответ дан 13 December 2019 в 22:09
поделиться
Другие вопросы по тегам:

Похожие вопросы: