non existent empty attribute

Bug #1783012 reported by Dmitry Melnikov on 2018-07-22
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
lxml
Undecided
Unassigned

Bug Description

Hello,
I have a env.xml like:

<?xml version="1.0"?>
<environment>
   <var name="A" value="1" />
   <var name="B" value="2" />
</environment

and env.xsd

<?xml version=1.0?>
<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema>

   <xs:simpleType name=non_empty_string_type>
      <xs:restriction base=xs:string>
         <xs:minLength value=1 />
      </xs:restriction>
   </xs:simpleType>

   <xs:element name=environment>
      <xs:complexType>
         <xs:sequence>
            <xs:element name=var minOccurs=0 maxOccurs=unbounded>
               <xs:complexType>
                  <xs:attribute name=name type=non_empty_string_type use=required />
                  <xs:attribute name=value type=xs:string use=required />
               </xs:complexType>
            </xs:element>
         </xs:sequence>
      </xs:complexType>
      <xs:unique name=uniqueName>
         <xs:selector xpath=var/>
         <xs:field xpath=@name/>
      </xs:unique>
   </xs:element>

</xs:schema>

It works well with
xmllint --schema env.xsd env.xml

<?xml version="1.0"?>
<environment>
   <var name="A" value="1"/>
   <var name="B" value="2"/>
</environment>
env.xml validates

but failed with lxml

from lxml import etree

env_xsd="env.xsd"
env_xml="env.xml"

schema = etree.XMLSchema(file=env_xsd, attribute_defaults=True)
xmlparser = etree.XMLParser(schema=schema)
with open(env_xml) as f:
        etree.fromstring(f.read(), xmlparser)

Traceback (most recent call last):
  File "tmp.py", line 9, in <module>
    etree.fromstring(f.read(), xmlparser)
  File "src/lxml/lxml.etree.pyx", line 3213, in lxml.etree.fromstring (src/lxml/lxml.etree.c:82934)
  File "src/lxml/parser.pxi", line 1819, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:124533)
  File "src/lxml/parser.pxi", line 1700, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:122964)
  File "src/lxml/parser.pxi", line 1040, in lxml.etree._BaseParser._parseUnicodeDoc (src/lxml/lxml.etree.c:116705)
  File "src/lxml/parser.pxi", line 573, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:110510)
  File "src/lxml/parser.pxi", line 683, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:112276)
  File "src/lxml/parser.pxi", line 613, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:111124)
lxml.etree.XMLSyntaxError: Element 'var': Duplicate key-sequence [''] in unique identity-constraint 'uniqueName'.

Python : sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
lxml.etree : (3, 5, 0, 0)
libxml used : (2, 9, 4)
libxml compiled : (2, 9, 2)
libxslt used : (1, 1, 28)
libxslt compiled : (1, 1, 28)

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers