Re: DOM XML/XSL questions
| From: | Christian Stocker | Date: | Wed, 27 Oct 2004 15:36:53 +0000 |
| Subject: | Re: DOM XML/XSL questions | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-200451@lists.php.net to get a copy of this message | ||
On Wed, 27 Oct 2004 13:52:13 +0100, Dusty Bin <lixo@argus.pt> wrote:
> 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,
Use $dom->createProcessingInstruction($target, $data) ;
and then append this to the document.
that could maybe work
see
http://ch.php.net/manual/en/function.dom-domdocument-createprocessinginstruction.php
for more details.
> 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).
The extension is not experimental anymore ;)
And the formatting is not lost. you didn't provide any ;) The DOM
Extension doesn't make any assumptions about the formatting of your
XML document (or correctly said, it doesn't insert whitespace
"automagically" ) but you can try to set the property formatOutput
just before saveXML:
$doc->formatOutput = true;
Never tested, but should work
chregu
> TIA for any advice... Dusty
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
christian stocker | Bitflux GmbH | schoeneggstrasse 5 | ch-8004 zurich
phone +41 1 240 56 70 | mobile +41 76 561 88 60 | fax +41 1 240 56 71
http://www.bitflux.ch | chregu@bitflux.ch | gnupg-keyid
0x5CE1DECB