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.

example:

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">
<cat>
<![CDATA[ SHOES MEN > Sneakers ]]>
</cat>
<name>
<![CDATA[ Saucony JAZZ_2044 ]]>
</name>
<imgs>
<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=""/>
</imgs>
<desc>
<![CDATA[
New product with tags.
]]>
</desc>
<attrs>
<a name="Brand">
<![CDATA[ Saucony ]]>
</a>
<a name="product_code">
<![CDATA[ JAZZ_2044_410_CHARCOAL-PINK ]]>
</a>
<a name="Size">
<![CDATA[ 41, 42, 42.5, 43, 44, 44.5, 45, 46 ]]>
</a>
</attrs>
</o>
</offers>

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.

@update

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

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

enter image description here

1 Answers

1
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:complexType>
      <xs:sequence>
        <xs:element ref="o"/>
      </xs:sequence>
      <xs:attribute name="version" use="required" type="xs:integer"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="o">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="cat"/>
        <xs:element ref="name"/>
        <xs:element ref="imgs"/>
        <xs:element ref="desc"/>
        <xs:element ref="attrs"/>
      </xs:sequence>
      <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:complexType>
  </xs:element>
  <xs:element name="cat" type="xs:string"/>
  <xs:element name="name" type="xs:string"/>
  <xs:element name="imgs">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="main"/>
        <xs:element maxOccurs="unbounded" ref="i"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="main">
    <xs:complexType>
      <xs:attribute name="url" use="required" type="xs:anyURI"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="i">
    <xs:complexType>
      <xs:attribute name="url" use="required" type="xs:anyURI"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="desc" type="xs:string"/>
  <xs:element name="attrs">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="a"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="a">
    <xs:complexType mixed="true">
      <xs:attribute name="name" use="required" type="xs:NCName"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

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