Home > Archive > PHP Language > December 2004 > howto? parse multiple xml strings with domxml
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
howto? parse multiple xml strings with domxml
|
|
|
| Hi,
I'm really with all these extensions and libraries in PHP. We
had a stable setup with Sablotron and could parse multiple XML strings
in one operation, but now we switched over to PHP5, dropped Sablotron
(it's way too slow) and try to get things alive again, but this seems
an impossible mission.
We worked with Sablotron before but switched over to the libxslt
library on PHP5 to perform XSL transformations. We used the wrapper
code found on http://be.php.net/xsl so that our old xslt_ functions
continued to work.
Now, with Sablotron we used to parse multiple XML documents as strings
and one XSL file from file. With libxslt it doesn't seem possible
parsing XML documents as strings.
This is the old method which worked fine, using Sablotron:
$args["/list_docu"] = "<content>some xml</content>";
$result = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $args,
$params);
This node could then be access with XSL:
<xsl:for-each select="document('arg:/list_docu')/content">
When I run this code using libxslt/PHP5 and the wrapper I get the
error:
Warning: I/O warning : failed to load external entity "arg:/list_docu"
This is because the XML documents must be saved on disk I guess.
Isn't there any way to load the XML documents as strings???
Grtz,
Bart
| |
| Janwillem Borleffs 2004-12-21, 3:55 am |
| bc wrote:
> I'm really with all these extensions and libraries in PHP. We
> had a stable setup with Sablotron and could parse multiple XML strings
> in one operation, but now we switched over to PHP5, dropped Sablotron
> (it's way too slow) and try to get things alive again, but this seems
> an impossible mission.
>
Have you visited the XSL manual page at http://nl.php.net/xsl ?
In the user contributes section, I have added some wrapper code a while ago,
which might be useful.
Just search for "jw at jwscripts dot com".
JW
| |
| Janwillem Borleffs 2004-12-21, 3:55 am |
| Janwillem Borleffs wrote:
> Have you visited the XSL manual page at http://nl.php.net/xsl ?
>
> In the user contributes section, I have added some wrapper code a
> while ago, which might be useful.
>
> Just search for "jw at jwscripts dot com".
>
There must be a better way, but until I have found one, the following
workaround could do the trick for now:
In the code mentioned before, replace the following sections:
Change:
// Start the transformation
$processed = $xsltproc->transformToXML($xml);
Into:
// Start the transformation
$processed = @$xsltproc->transformToXML($xml);
Now the call would be as follows:
$html = xslt_process(
$xsltproc,
'arg:/_xml',
'arg:/_xsl',
null,
$arguments,
array('document'=>$arguments['/_xml'])
);
This would assign the XML contents to the fifth argument, setting it as an
XSL parameter.
In the XSLT, you should then able to do the following:
<xsl:param name="document" />
<xsl:template match="/">
<xsl:for-each select="document($document)/content">
....
</xsl:for-each>
This is more like a hack, because of the necessary error surpression. But
perhaps it will help you for now.
JW
| |
| Janwillem Borleffs 2004-12-21, 3:55 am |
| Janwillem Borleffs wrote:
> This would assign the XML contents to the fifth argument, setting it
> as an XSL parameter.
>
That would be the sixth argument, of course...
JW
| |
| Janwillem Borleffs 2004-12-21, 3:55 am |
| bc wrote:
> Hello Janwillem! Thanks for your reply. I tried out your suggestion
> and some variations on it, but without success. document() cannot
> interprete a parameter as XML, it seems.
>
If you would follow my suggestion exactly, you will see that it is possible
(it worked for me).
> Is it conceptually wrong to parse 10 XML strings with one XSL file?
> How do other XML- sites manage this?
>
There is nothing wrong with this approach. Most XML-based systems work like
this, because you will then have to maintain only one XSLT file and the XML
files are maintained by the data they contain.
> The 10 XML strings are returned from a custom PHP function that
> communicates with an external PostGres DB. Should I stick these XML
> strings together? If yes, I'll need to adapt every XSL file... which
> is a huge job.
>
You won't have to stick the XML strings together, but will have to adapt the
XSL file(s) to support the <xsl:param /> scheme.
If you want, you can send the files to my email address (not to this
newsgroup), after which I probably will be able to help you further.
Note that when the attachment is larger than 500kB, I won't be able to
receive it. Put it on a place where I can download it from instead.
JW
| |
|
| Hello Janwillem! Thanks for your reply. I tried out your suggestion
and some variations on it, but without success. document() cannot
interprete a parameter as XML, it seems.
I'm totally clueless...
Is it conceptually wrong to parse 10 XML strings with one XSL file?
How do other XML- sites manage this?
The 10 XML strings are returned from a custom PHP function that
communicates with an external PostGres DB. Should I stick these XML
strings together? If yes, I'll need to adapt every XSL file... which
is a huge job.
Working with XML seems a big hassle to me. More work, and always
relying on external libraries, less support... I wish I could just
connect straight to the DB.
Anyway, thnx for you help,
Barton
On Thu, 16 Dec 2004 18:44:22 +0100, "Janwillem Borleffs"
<jw@jwscripts.com> wrote:
>Janwillem Borleffs wrote:
>
>There must be a better way, but until I have found one, the following
>workaround could do the trick for now:
>
>In the code mentioned before, replace the following sections:
>
>Change:
>
>// Start the transformation
>$processed = $xsltproc->transformToXML($xml);
>
>Into:
>
>// Start the transformation
>$processed = @$xsltproc->transformToXML($xml);
>
>Now the call would be as follows:
>
>$html = xslt_process(
> $xsltproc,
> 'arg:/_xml',
> 'arg:/_xsl',
> null,
> $arguments,
> array('document'=>$arguments['/_xml'])
> );
>
>This would assign the XML contents to the fifth argument, setting it as an
>XSL parameter.
>
>In the XSLT, you should then able to do the following:
>
> <xsl:param name="document" />
> <xsl:template match="/">
>
> <xsl:for-each select="document($document)/content">
> ....
> </xsl:for-each>
>
>This is more like a hack, because of the necessary error surpression. But
>perhaps it will help you for now.
>
>
>JW
>
>
|
|
|
|
|