DOM XML/XSL questions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I have a requirement to create an XML file which looks approximately like:

<?xml version="1.0" ?>
<?xml-stylesheet type='text/xsl' href='article.xsl' ?>
<article>
  <item>Item Text</item>
  ...
</article>

The file needs to be formatted.

I built a dom using the Dom extension, creating a document, adding nodes, and I got a nicely formatted XML file from $dom->saveXML();

The only problem I had, was that I could see no way to add the stylesheet definition to the XML. I'm not sure that a DOM should know anything about stylesheets, but an XML file probably should.

I managed to bypass the problem, by loading the dom from a file, with the style sheet and the root element included, and then proceeding to build the dom by adding the extra nodes. This also worked fine, but now the output from $dom->saveXML() is no longer formatted.
Here is some sample code:


<?php
$txt =<<<EOT
<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='../../../article.xsl' ?>
<Article
xmlns='http://www.example.com/xml'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation='http://www.example.com/test/xml article.xsd'>
</Article>
EOT;

$dom = new DOMDocument();
$dom->preserveWhiteSpace = FALSE;
$dom->resolveExternals = TRUE;
$dom->loadXML($txt);

$item = $dom->createElement("Item");
$itemText = $dom->createTextNode("Item Text");
$item->appendChild($itemText);
$dom->documentElement->appendChild($item);

$dom->formatOutput = TRUE;

echo $dom->saveXML();
?>

My questions are:
1) How should I add a stylesheet to this type of document? (I do not need to process the stylesheet in php, it is for the guidance of the end user of the document, who may use it or modify it),
and,
2) Is this a (dare I say bug in an experimental extension) that if I load the dom, and perform operations on the dom, I lose formatting(A dom that is just loaded, or just created by dom operations, is correctly formatted, a dom with mixed processes is not).


TIA for any advice...   Dusty

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux