Need to clarify the "all" tag in relation to the xml scheme (is this a libxml2 error?)

When upgrading from libxml2 2.6 to 2.7, some changes changed for me. I found a bug report on my site that is considering this change, https://bugzilla.gnome.org/show_bug.cgi?id=571271 .

Interestingly, they report that "and, I think, we misinterpreted the expected behavior of these options (although I'm still not 100% sure)" - they were not sure whether they were reading the specification correctly, but they made a correction.

I think the previous behavior is correct, so I wanted to see if anyone has knowledge in any direction.

Basically, it <xs:all>elem1, elem2, ..<xs:all>means that "all or none of the elements 1, elem2 .. should not be present", or "any element elem1, elem2 .. may be present?" Although this is similar to the first, two sources do not make it clear:

http://www.w3.org/TR/xmlschema-0/#ref18 - "All elements in a group can appear once or not be displayed at all, and they can be displayed in any order."

http://www.w3schools.com/Schema/el_all.asp - "The example above indicates that the elements" firstname "and" lastname "can be displayed in any order and each CAN element appears zero or one time!"

The script below, using lxml, reports success with libxml2 2.6, but the second schema check fails on 2.7. Can anyone confirm that 2.7 is doing right or wrong here?

from lxml import etree
from StringIO import StringIO

schema = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element type="parent-type" name="parent"/>
 <xs:complexType name="parent-type">
   <xs:all maxOccurs="1" minOccurs="0">
     <xs:element type="xs:int" name="int-attr"/>
     <xs:element type="xs:string" name="str-attr"/>
   </xs:all>
 </xs:complexType>
</xs:schema>
"""

xmlschema = etree.XMLSchema(etree.parse(StringIO(schema)))

# passes
doc1 = """
<parent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.example.com/xml/schemas">
 <str-attr>some value</str-attr>
 <int-attr>12</int-attr>
</parent>
"""

# fails.  it wants both "int-attr" and "str-attr" to be present.
# didn't think this was how "xs:all" worked ?
doc2 = """
<parent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.example.com/xml/schemas">
 <int-attr>12</int-attr>
</parent>
"""

for i, doc in enumerate((doc1, doc2, )):
   doc = etree.parse(StringIO(doc))
   try:
       xmlschema.assertValid(doc)
       print "document %d is valid." % i
   except Exception, e:
       print "document %d is not valid." % i
       print e

output:

document 0 is valid.
document 1 is not valid.
Element 'parent': Missing child element(s). Expected is ( str-attr )., line 2
+3
source share
2 answers

User Jorn Horstmann has already answered your question correctly, but formatting may make the answer a bit unclear. I hope these examples help those who were puzzled.

What means minOccursand maxOccursthe element<xs:all>

Remember that <xs:all>and <xs:element>have a default value of "1" for minOccursand maxOccurs. therefore

<xs:all>
  <xs:element type="xs:int" name="int-attr"/>
  <xs:element type="xs:string" name="str-attr"/>
</xs:all>

This is actually the same as

<xs:all minOccurs="1" maxOccurs="1">
  <xs:element type="xs:int" name="int-attr" minOccurs="1" maxOccurs="1"/>
  <xs:element type="xs:string" name="str-attr" minOccurs="1" maxOccurs="1"/>
</xs:all>

, <xs:all> , , , - . , XML-

<parent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="http://www.example.com/xml/schemas">
  <int-attr>12</int-attr>
</parent>

. minOccurs="0" <xs:all> , , <parent/>. , , : " ". , , w3schools . " CAN ", " ".

maxOccurs <xs:all> "1" .

, , , . <xs:all> minOccurs="0" .

<xs:all minOccurs="1" maxOccurs="1">
  <xs:element type="xs:int" name="int-attr" minOccurs="0" maxOccurs="1"/>
  <xs:element type="xs:string" name="str-attr" minOccurs="0" maxOccurs="1"/>
</xs:all>

XML

<parent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="http://www.example.com/xml/schemas">
  <int-attr>12</int-attr>
</parent>

( minOccurs="0"), <parent/>. "" , <xs:all>, spec : " , minOccurs maxOccurs 0 1", , , , , , <xs:all> .

+4

, , libxml . , xsd spec

, .

, minOccurs="0", , , . . , w3schools .

+1

Source: https://habr.com/ru/post/1753556/


All Articles