We are building a website price comparison. For integrate stores with our website we using the .xml file (feed).

The feed file has a specific structure that is required to import products to our site correctly.

What we need:

How we can write a script that will verify the correctness of the provide .xml file.


our file feed have required structure like:

<offers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1">
<o id="2576" url="https://urlshop.com/catalog/product/view/id/2576/s/jazz-2044-410-charcoal-pink/" price="390" avail="1" set="0" weight="2.0000" basket="1" stock="2">
<![CDATA[ SHOES MEN > Sneakers ]]>
<![CDATA[ Saucony JAZZ_2044 ]]>
<main url="https://urlshop.pl/pub/media/catalog/product/2/9/292864.jpg"/>
<i url="https://urlshop.pl/pub/media/catalog/product/2/9/292864_1.jpg"/>
<i url="https://urlshop.pl/pub/media/catalog/product/2/9/292865_1.jpg"/>
<i url="https://urlshop.pl/pub/media/catalog/product/2/9/292866_1.jpg"/>
<i url="https://urlshop.pl/pub/media/catalog/product/2/9/292867_1.jpg"/>
<i url=""/>
New product with tags.
<a name="Brand">
<![CDATA[ Saucony ]]>
<a name="product_code">
<a name="Size">
<![CDATA[ 41, 42, 42.5, 43, 44, 44.5, 45, 46 ]]>

We need build checker, when customer place in checker url link to feed and if feed have the same structure like above then return: The .xml file is correct!

If the file has a different structure, return the error.


Ok,but now how to integrate this code with simple form like:

<form action="/action_page.php">
    <legend>Validation XML Feed:</legend>
    Url to .xml file: <input type="url" name="firstname" value="">
    <input type="submit" value="Submit">

enter image description here

1 Answers

Michael Kay On

The usual approach is to write a schema (XSD) that describes the structure, and then to validate the instance against the schema. There are a number of XML development tools that will generate a first-cut schema from the instance (though it will never capture all the rules precisely). This is what oXygen generates for your instance document:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="offers">
        <xs:element ref="o"/>
      <xs:attribute name="version" use="required" type="xs:integer"/>
  <xs:element name="o">
        <xs:element ref="cat"/>
        <xs:element ref="name"/>
        <xs:element ref="imgs"/>
        <xs:element ref="desc"/>
        <xs:element ref="attrs"/>
      <xs:attribute name="avail" use="required" type="xs:integer"/>
      <xs:attribute name="basket" use="required" type="xs:integer"/>
      <xs:attribute name="id" use="required" type="xs:integer"/>
      <xs:attribute name="price" use="required" type="xs:integer"/>
      <xs:attribute name="set" use="required" type="xs:integer"/>
      <xs:attribute name="stock" use="required" type="xs:integer"/>
      <xs:attribute name="url" use="required" type="xs:anyURI"/>
      <xs:attribute name="weight" use="required" type="xs:decimal"/>
  <xs:element name="cat" type="xs:string"/>
  <xs:element name="name" type="xs:string"/>
  <xs:element name="imgs">
        <xs:element ref="main"/>
        <xs:element maxOccurs="unbounded" ref="i"/>
  <xs:element name="main">
      <xs:attribute name="url" use="required" type="xs:anyURI"/>
  <xs:element name="i">
      <xs:attribute name="url" use="required" type="xs:anyURI"/>
  <xs:element name="desc" type="xs:string"/>
  <xs:element name="attrs">
        <xs:element maxOccurs="unbounded" ref="a"/>
  <xs:element name="a">
    <xs:complexType mixed="true">
      <xs:attribute name="name" use="required" type="xs:NCName"/>

Of course, there's guesswork involved here. Just because none of your a/@name attributes contain spaces doesn't mean spaces aren't allowed.