1

I have a python script that validates my xml file (file "a") using an xml schema. It works great, and now I want to include another xml file (file "b") in the original xml (file "a"). The issue is, I want to validate file "a" without validating the included file "b".

Is this possible?

Python code:

from lxml import etree

xsd_doc=etree.parse('shiporder.xsd')
xsd = etree.XMLSchema(xsd_doc) 
xml_file = etree.parse('file_a.xml')
xsd.validate(xml_file)  
print(xsd.error_log)

XML schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="shiporder">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="file_input" minOccurs="0">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="xi:include" minOccurs="0"/>
          </xs:sequence> 
        </xs:complexType>
      </xs:element>
      <xs:element name="orderperson" type="xs:string"/>
      <xs:element name="shipto">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="address" type="xs:string"/>
             <xs:element name="city" type="xs:string"/>
            <xs:element name="country" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
   <xs:attribute name="orderid" type="xs:string" use="required"/>
  </xs:complexType>
</xs:element>

</xs:schema>

XML file "a":

<shiporder orderid="889923"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xi="http://www.w3.org/2003/XInclude"
 xsi:noNamespaceSchemaLocation="shiporder.xsd">


  <file_input>
    <xi:include href="shiporder2.xml" parse="xml"/>
  </file_input>

  <orderperson name=" John Smitha">John Smith</orderperson>
  <shipto>
    <name>Ola Nordmann</name>
    <address>Langgt 23</address>
    <city>4000 Stavanger</city>
    <country>Norway</country>
  </shipto>
  <item>
    <title>Hide your heart</title>
    <quantity>1</quantity>
    <price>9.90</price>
  </item>
</shiporder>
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
GoldenEagle
  • 115
  • 1
  • 2
  • 15

0 Answers0