Полномочия два в двоичном файле похожи на это:
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));
}
Для большего количества битового жонглирования см. здесь .
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:
is required and appears first then
isn't required (and appears as second tag)
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>
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.
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).
Это невозможно с XSD .. Но, кстати, вы можете обойти что-то вроде Infant-programmer, если требования немного удобны, как показано в ее примере ..