Skip to content

Instantly share code, notes, and snippets.

@BobbyMcWho
Last active September 12, 2018 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BobbyMcWho/30390c6a473ba2b4e36cfd28f7015f4d to your computer and use it in GitHub Desktop.
Save BobbyMcWho/30390c6a473ba2b4e36cfd28f7015f4d to your computer and use it in GitHub Desktop.
libxml2 bug when validating against a complexType in an XSD

I came across this error when validating XML files against an XSD in Nokogiri.

When validating a complexType XML element, if a node fails validation due to a Occurence restriction, and it is the last node in the complex element, the validation error will not raise, and will pass as a valid document. If there is another node in the sequence that is allowed, then at that point the document will fail validation. However, the error that is thrown is misleading, because it will say that the second element is the one that is out of place, rather than the one that is actually out of place.

The easiest way to reproduce this is with xmllint:

$xmllint --noout --schema example_xsd.xsd incorrectly_passing.xml
incorrectly_passing.xml validates
$xmllint --noout --schema example_xsd.xsd failing_wrong_error.xml
example.xml:4: element Wheel: Schemas validity error : Element '{http://foo.bar/example}Wheel': This element is not expected.
example.xml fails to validate
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema elementFormDefault="qualified" targetNamespace="http://foo.bar/example" xmlns:example="http://foo.bar/example" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Car">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Wing" type="xsd:string" minOccurs="0" maxOccurs="0"/>
<xsd:element name="Wheel" type="xsd:string" minOccurs="0" maxOccurs="4"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<?xml version="1.0" encoding="UTF-8"?>
<example:Car xmlns:example="http://foo.bar/example">
<!-- This should not pass, min/max Wings is set to 0 -->
<example:Wing>This doesn't go here</example:Wing>
<!-- The error libxml gives actually says the Wheel -->
<!-- element is not expected, even though it is min 0 -->
<!-- max 4, as defined in the XSD -->
<example:Wheel>This is fine</example:Wheel>
</example:Car>
<?xml version="1.0" encoding="UTF-8"?>
<example:Car xmlns:example="http://foo.bar/example">
<!-- This should not pass, min/max Wings is set to 0 -->
<example:Wing>This doesn't go here</example:Wing>
</example:Car>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment