views:

102

answers:

1

Hi coders!

I've spent whole days with PHP's DOM functions but i can't understand how it works yet. :( I have a simple XML file that looks okay but i cannot use it how i think when i've created it's structure.

Sample XML fragment:

-pages //root element
    -page id="1" //we can have any number of pages
        -product id="364826" //we can have any number of products
            -SOME_KIND_OF_VALUE
            -ANOTHER_VALUE
            ...

My original idea was to speed up my client's workflow so i throw out old CSVs and started using XMLs.

Problem 1: When i grouping products into page i'm using setIdAttribute to prevent storing the same page in the tree more than once. This works fine until reading happens because these id's are tied to some kind of DTD's (based on getElementById).

Question 1: How can i write a simple DTD which provides these necessary informations so i can use getElementById at the reading phase too?

Problem 2: Because i have pages i'd like to load as less information as i can. That was why i created the id attribute on pages. Now i cannot access my page id="2" directly because Problem 1 above (getElementById makes no sense currently). Somehow i can managed to retrieve the necessary informations about each product on a given page but my code looks scary:

$doc      = DOMDocument::load('data.xml');
$xpath    = new DOMXPath($doc);
$query    = '/pages/page[' . $page . ']'; //$page is fine: was set earlier
$products = $xpath->query($query);
$_prods   = $doc->getElementsByTagName('product');
foreach($_prods as $product){
    foreach($product->childNodes as $node){
        echo $node->nodeName . ": " . $node->nodeValue . "<br />";
    }
}

Queston 2: I think the code above is the example about how not to parse an XML. But because of my limited knowledge of PHP's DOM functions i cannot write a cleaner one by myself. I tried some trivial solution but none of them worked for me.

Please help me if you can.

Thanks, fabrik

+3  A: 

Solving Problem 1:

The W3C defines: the meaning of the attribute xml:id as an ID attribute in XML documents and defines processing of this attribute to identify IDs in the absence of validation, without fetching external resources, and without relying on an internal subset.

In other words, when you use

$element->setAttribute('xml:id', 'test');

you do not need to call setIdAttribute, nor specify a DTD or Schema. DOM will recognize the xml:id attribute when used with getElementById without you having to validate the document or anything. This is the least effort approach. Note though, that depending on your OS and version of libxml, you wont get getElementById to work at all.

Solving Problem2:

Even with IDs not being fetchable with getElementById, you can still very much fetch them with XPath:

$xpath->query('/pages/page[@id=1]');

would definitely work. And you can also fetch the product children for a specific page directly:

$xpath->query('//pages/page[@id=1]/products');

Apart from this, there is very little you can do to make DOM code look less verbose, because it really is a verbose interface. It has to be, because DOM is a language agnostic interface, again defined by the W3C.


EDIT after comment below

It is working like I explained above. Here is a full test case for you. The first part is for writing new XML files with DOM. That is where you need to set the xml:id attribute. You use this instead of the regular, non-namespaced, id attribute.

// Setup
$dom = new DOMDocument;
$dom->formatOutput = TRUE;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML('<pages/>');

// How to set a valid id attribute when not using a DTD or Schema
$page1 = $dom->createElement('page');
$page1->setAttribute('xml:id', 'p1');
$page1->appendChild($dom->createElement('product', 'foo1'));
$page1->appendChild($dom->createElement('product', 'foo2'));

// How to set an ID attribute that requires a DTD or Schema when reloaded
$page2 = $dom->createElement('page');
$page2->setAttribute('id', 'p2');
$page2->setIdAttribute('id', TRUE);
$page2->appendChild($dom->createElement('product', 'bar1'));
$page2->appendChild($dom->createElement('product', 'bar2'));

// Appending pages and saving XML
$dom->documentElement->appendChild($page1);
$dom->documentElement->appendChild($page2);
$xml = $dom->saveXML();
unset($dom, $page1, $page2);
echo $xml;

This will create an XML file like this:

<?xml version="1.0"?>
<pages>
  <page xml:id="p1">
    <product>foo1</product>
    <product>foo2</product>
  </page>
  <page id="p2">
    <product>bar1</product>
    <product>bar2</product>
  </page>
</pages>

When you read in the XML again, the new DOM instance no longer knows you have declared the non-namespaced id attribute as ID attribute with setIdAttribute. It will still be in the XML, but id attribute will just be a regular attribute. You have to be aware that ID attributes are special in XML.

// Load the XML we created above
$dom = new DOMDocument;
$dom->loadXML($xml);

Now for some tests:

echo "\n\n GETELEMENTBYID RETURNS ELEMENT WITH XML:ID \n\n";
foreach( $dom->getElementById('p1')->childNodes as $product) {
    echo $product->nodeValue; // Will output foo1 and foo2 with whitespace
}

The above works, because a DOM compliant parser has to recognize xml:id is an ID attribute, regardless of any DTD or Schema. This is explained in the specs linked above. The reason it outputs whitespace is because due to the formatted output there is DOMText nodes between the opening tag, the two product tags and the closing tags, so we are iterating over five nodes. The node concept is crucial to understand when working with XML.

echo "\n\n GETELEMENTBYID CANNOT FETCH NORMAL ID \n\n";
foreach( $dom->getElementById('p2')->childNodes as $product) {
    echo $product->nodeValue; // Will output a NOTICE and a WARNING
}

The above will not work, because id is not an ID attribute. For the DOM parser to recognize it as such, you need a DTD or Schema and the XML must be validated against it.

echo "\n\n XPATH CAN FETCH NORMAL ID \n\n";
$xPath = new DOMXPath($dom);
$page2 = $xPath->query('/pages/page[@id="p2"]')->item(0);
foreach( $page2->childNodes as $product) {
    echo $product->nodeValue; // Will output bar1 and bar2
}

XPath on the other hand is literal about the attributes, which means you can query the DOM for the page element with attribute id if getElementById is not available. Note that to query the page with ID p1, you'd have to include the namespace, e.g. @xml:id="p1".

echo "\n\n XPATH CAN FETCH PRODUCTS FOR PAGE WITH ID \n\n";
$xPath = new DOMXPath($dom);
foreach( $xPath->query('/pages/page[@id="p2"]/product') as $product ) {
    echo $product->nodeValue; // Will output bar1 and bar2 w\out whitespace
}

And like said, you can also use XPath to query anything else in the document. This one will not output whitespace, because it will only return the product elements below the page with id p2.

You can also traverse the entire DOM from a node. It's a tree structure. Since DOMNode is the most important class in DOM, you want to familiarize yourself with it.

echo "\n\n TRAVERSING UP AND DOWN \n\n";
$product = $dom->getElementsByTagName('product')->item(2);
echo $product->tagName; // 'product'
echo $dom->saveXML($product); // '<product>bar1</product>'

// Going from bar1 to foo1
$product = $product->parentNode // Page Node
                   ->parentNode // Pages Node
                   ->childNodes->item(1)  // Page p1
                   ->childNodes->item(1); // 1st Product

echo $product->nodeValue; // 'foo1'

// from foo1 to foo2 it is two(!) nodes because the XML is formatted
echo $product->nextSibling->nodeName; // '#text' with whitespace and linebreak
echo $product->nextSibling->nextSibling->nodeName; // 'product'
echo $product->nextSibling->nextSibling->nodeValue; // 'foo2'

On a sidenote, yes, I do have a typo in the original code above. It's product not products. But I find it hardly justified to claim the code does not work when all you have to change is an s. That just feels too much like wanting to be spoonfed.

Gordon
Setting `id` of a page before writing the XML file is working fine. When i reading the XML i cannot/don't want set attributes because i'd like to read the XML source based on this attributes. So Problem 1 isn't solved yet.Problem 2 definitely not solved, your first XPath query fails. Second fails too because i don't have the node `products` instead i have many `product` node inside of a page. (That was defined in my question.)
fabrik
@fabrik both problems are solved. See my update for proof.
Gordon
It's awesome! Thank you for your deep explanation. It's fast and does exactly what i want. Except one thing but it's my fault: I've made a mistake in the sample XML fragment because i need the node's name and value too so i need two foreach yet again :o Of course i'll accept your answer because it's do the trick. Thanks again!
fabrik
@fabrik you're welcome. You can get `$node->tagName` and `$node->nodeValue` from the same node in one go. Also, you can traverse the DOM Tree from the node, e.g. if `$node->tagName` is 'product', you can say `$node->parentNode->tagName` and get 'page'. Have a look at the properties of [DOMNode](http://www.php.net/manual/en/class.domnode.php) for the available properties. DOMNode is what most nodes are extended from, so you want to know these props.
Gordon
I was ambiguous again. I need the node name and the value inside of each product not the page itself. I need each products each property and value inside the given page. :( If it makes sense here's the real XML anyway: http://bit.ly/bJzfK2
fabrik
@fabrik yes, in that case iterating over the childNodes of a product makes most sense with second foreach when using DOM. Just take into account that the childNodes will have DOMText nodes between the actual element nodes when you are using formatted XML. See my update above. On a sidenote, if your aim is just to transform the XML into some HTML or something, then an [XSLT](http://www.w3schools.com/xsl/) might also be of interest to you. [PHP has an XSLT processor built-in](http://www.php.net/manual/en/book.xsl.php).
Gordon
No, my plan isn't transform XML to HTML but saving Excel-documents to my server in an universal format. These documents are assortments for magazines which then compared to the database with images, etc. then finally imported to InDesign.
fabrik
@fabrik ok, cant help with that :)
Gordon
No need for that because the whole thing is working about a year. The only thing has to be changed to underlying layer: CSV to XML but it looks like it's more difficult than i thought.
fabrik