0

I have searched everywhere for an answer to this but cannot seem to find one.

I am trying to validate an XML file against an XSD file. I have so far written this.

$x_validate = new DOMDocument();
$x_validate->load($xml_path);

$x_validate->schemaValidate($xsd_path);

At first the paths were set to a remote location. I then realised that these remote files require authentication to be seen. So to avoid this I downloaded the files into my directory so the paths were like the following.

$xml_path = "./test-v1.xml";
$xsd_path = "./test-v1.xsd";

The only problem with this is that it didn't work and I got some errors. One of them being this. I blanked out the URL for security purposes.

Warning: DOMDocument::schemaValidate() [domdocument.schemavalidate]: Element '{http://www.w3.org/2001/XMLSchema}include': Failed to load the document 'https://****' for inclusion. in C:\Xampp\htdocs\cdes\xml-validation\run-validation.php on line 15

So because of this error I am back to my original remote referencing of the URL.

Can someone please point me in the right direction to send the authentication username and password to the XSD and XML file before validating the schema?

Thank you.

Brad Bird
  • 737
  • 1
  • 11
  • 30

2 Answers2

2

You should download the files to your local harddisk including those that are referenced via the includes and then use a so called Catalog to automatically use the local files instead of the URIs.

This will also greatly improve validation speeds. I have this exemplary outlined in a different Q&A material:

For the authentication problems you write about with the error information you have provided it's not entirely clear what exactly causes this and how to solve it (apart from using the catalog). When you're able to download the files with your browser, go for the local copies. Most often you can download a set of XSD files as well in a zip package or similar.

If you can't manage to download, then you would need to troubleshoot the HTTP connection(s) which requires you to either trace the requests with a network sniffer or you inject your own handling with the external loader callback (see libxml_set_external_entity_loader()) which is available with PHP 5.4 and also you can inject callback into the stream via libxml_set_streams_context() and notifications on stream_notification_callback().

TLDR: Go for catalogs.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Thanks a lot. I resorted to my original plan and just downloaded the files locally. I also had another problem with the encoding so I had to change it to ISO-8859-1 but it's validating perfectly now. – Brad Bird Mar 06 '13 at 21:21
0

Not sure if that's supported by the DOMDocument class, but you can normally add authentication data to a URL in this format:

http://username:password@cooldomain.com/bla.xsd

Have you tried adding that to the XSDs path?

grobmotoriker
  • 469
  • 1
  • 6
  • 14
  • Hello and thanks for the quick reply. I tried putting the username and password in the format you provided and it did not seem to work. The domain also has a subdomain and uses HTTPS. Will this affect the authentication? – Brad Bird Mar 06 '13 at 16:56
  • Okay I have now got the first XSD file loading using the URL changes you suggested. I now have another problem. Because the XSD file has other XSD includes and they also require authentication. Because I cannot edit the lines of code with the username and password it is throwing an authentication error. Warning: DOMDocument::schemaValidate(https://****/cdes/services/cdes2_ws?xsd=CDEX_ISO_languages.xsd) [domdocument.schemavalidate]: failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required in C:\Xampp\htdocs\cdes\xml-validation\run-validation.php on line 16 – Brad Bird Mar 06 '13 at 17:21
  • Well, I've been running some tests, setting up an HTTP authentication via https. The authentication does not seem to work. On second thought, it would not be very "secure" to put a username into a URL that's supposed to be HTTPS. Works fine over http though. – grobmotoriker Mar 06 '13 at 17:29