Validating XML files against an XML schema

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Validating XML files against an XML schema

Post by jayshields »

Basically, I've written up an XML file and an XSD and I want to see if the XML validates against the XSD. I've already validated my XSD file via the W3C validator.

Google seems to be pointing me to code snippets which can do this, but I just want a standalone program. So far I've found one, but it's only a trial version and you have to install .NET framework amonst other things before you attempt to install it.

Anyone got any programs to do this?

Thanks.

Also, I have no experience with XSL; but I'm wanting to output my XML file in a simple HTML table. Just two columns with element name in the left and value in the right. Shouldn't be too hard but I can't find any good information on the net anywhere. Can someone point me to a tutorial or anything about this?
Last edited by jayshields on Sat Mar 31, 2007 10:48 am, edited 1 time in total.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

If you use firefox to view the xml file, and as long as it can read the DTD, then shouldn't it complain if it finds an error?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

I thought it might do, but how and where would it report errors?
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Right in the content window... I remember seeing a yellow background with red text, but it's been a while since I've had to work directly with xml
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

What a coincidence... In searching for an online css compressor, I came across http://mathiasbynens.be/archive/2005/09/css-compressors Which shows what I was thinking about.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Hmmm. I can't seem to generate anything like that. Even if I completely screw up my XSD.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

isn't there a validate() method on DOMDocument?

I'm curious, is there a difference between DTD and XSD?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

ole wrote:'m curious, is there a difference between DTD and XSD?
Yes... And google can easily tell you the exact the differences...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

libxml2 comes with the program xmllint
Usage : xmllint [options] XMLfiles ...
[...]
--relaxng schema : do RelaxNG validation against the schema
--schema schema : do validation against the WXS schema
--schematron schema : do validation against a schematron
and btw php 5 can do it, too
relaxNG example:

Code: Select all

<?php
$docA = DOMDocument::loadxml(
'<addressBook>
  <card>
    <name>John Smith</name>
    <email>js@example.com</email>
  </card>
  <card>
    <name>Fred Bloggs</name>
    <email>fb@example.net</email>
  </card>
</addressBook>');

$b = $docA->relaxNGValidateSource(
'<element name="addressBook" xmlns="http://relaxng.org/ns/structure/1.0">
  <zeroOrMore>
    <element name="card">
      <element name="name">
        <text/>
      </element>
      <element name="email">
        <text/>
      </element>
    </element>
  </zeroOrMore>
</element>');


var_dump($b);
Post Reply