Turning XSD into documentation file

This XSD stuff is not for me, f*cking unreadable :/ Thanks god there is a stylesheet that makes it prettier, like a documentation.
First download it http://www.w3.org/2008/09/xsd.xsl due browser cross domain restriction, so you can avoid (fastest solution for me now)

Unsafe attempt to load URL https://www.w3.org/2008/09/xsd.xsl from frame with URL http://localhost/kovoinox/temp/stock.xsd. Domains, protocols and ports must match.

After download put it next to the XSD file and insert the following line as the 2nd row in the XSD:

<?xml version="1.0" encoding="Windows-1250"?>
<?xml-stylesheet type="text/xsl" href="xsd.xsl"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 ... the rest of the XSD ...

After opening the XSD in the browser you get a much nicer interpretation.

ss_2016-08-03-16-16-23

Solution from http://stackoverflow.com/questions/6686124/how-to-turn-xsd-files-into-documentation-file

Solving No matching global declaration available for the validation root.

Had to create a custom XML for Pohoda accounting software, but hit some errors so I decided to make a PHPUnit test for validating it against its XML schema definition (XSD) with PHPs DOMDocument::schemaValidate

/** @test */
public function validateStockXml()
{
$this->pohoda->createStockXml();
$xml = new \DOMDocument();
$xml->load(Pohoda::STOCK_XML_PATH);

$isValid = $xml->schemaValidate('http://www.stormware.cz/schema/version_2/stock.xsd');
$this->assertTrue($isValid);
}

Stuck with the following error for a couple of hours 😦

Element ‘{http://www.stormware.cz/schema/version_2/data.xsd}dataPack’: No matching global declaration available for the validation root.

After a lot of trial and error decided to google more and more I finally found the solution at http://www.scriptscoop.me/t/81762e7e42e7/php-xml-validation-no-matching-global-declaration-available-for-the-valida.html

Changing the schema URL to http://www.stormware.cz/schema/version_2/data.xsd instead using http://www.stormware.cz/schema/version_2/stock.xsd solved the problem.

So easy, huh 🙂 Always learning.

Parsing XML with nested namespaces in PHP

I received a XML with categories in it from Pohoda software from a client.
I struggled a bit with it, beacuse it contained nested namespaces what was new to me.
After figuring out it on my own I realized I could try google it and with luck I found ideas and code to reuse.

I came up with the following code to parse the XML:

$xml = file_get_contents(POHODA_CATEGORIES.xml);
$xml = simplexml_load_string($xml);
$ns = $xml->getNameSpaces(true);

foreach ($xml->xpath('//lst:categoryDetail') as $categories) {
    $ctg = $categories->children($ns['ctg']);
    foreach ($ctg->category as $category) {

        insert_page($category->id, $category->name);
        foreach ($c->subCategories->category as $subcategory) {
            insert_page($subcategory->id, $subcategory->name);
        }
    }
}

I also published a gist for it https://gist.github.com/mikaelz/288d1611eec0f80c1293