On Friday, March 29, 2002, at 01:56 PM, Erik Price wrote:
> My, there have been a lot of questions about XSLT in the past 24 hrs... > admittedly most of them coming from me. In addition to my first > question (repeated below for clarity), I have a second one -- how do I > perform an XSLT transformation on multiple XML documents? Do I need to > perform a separate XSLT transformation on each one? The reason I ask > is b/c I am pulling my XML from a DB, so there may be more than one > based on the results from the query. If anyone can answer this, that'd > be great. I did discover over the weekend that it is possible to use a single XSLT stylesheet to perform a transformation on multiple XML documents, but this requires that each XML document have its own URI to do so -- obviously this won't work in my case, since I'm pulling the XML documents as multiple results from a database query. So that option is out. I have thought about this problem and the answer is simple -- no, there is no way to perform a single XSLT transformation on multiple concatenated XML documents if those documents contain XML declarations at the top of them. Why? Because multiple concatenated XML documents with XML declarations != a well-formed XML document, which is a prerequisite for the XSLT processor. (Essentially, the processor didn't like encountering a second XML declaration in the concatenated string, since a rule of well-formedness is that if there are any XML declarations, there can be only one and there cannot be any characters before that declaration in the document.) So that leaves me with this question -- (A) should I run the XSLT process on each individual result that comes from the query? (B) Or should I concatenate the XML documents as I have, use substr_replace() to change all XML declarations to "", and then prepend a single XML declaration to the beginning of the string and perform the transformation on this new string? Much thanks to Sean Scanlon for showing me how to create HTML attributes from XML with XSLT. Erik ---- Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] > From: Erik Price <[EMAIL PROTECTED]> > Date: Fri Mar 29, 2002 01:56:55 PM US/Eastern > To: [EMAIL PROTECTED] > Subject: [PHP] IMPORTANT question for anyone using XSLT > > My, there have been a lot of questions about XSLT in the past 24 hrs... > admittedly most of them coming from me. In addition to my first > question (repeated below for clarity), I have a second one -- how do I > perform an XSLT transformation on multiple XML documents? Do I need to > perform a separate XSLT transformation on each one? The reason I ask > is b/c I am pulling my XML from a DB, so there may be more than one > based on the results from the query. If anyone can answer this, that'd > be great. > > If not, perhaps you can help with this situation, which I believe will > probably plague just about any PHP programmer who will ever use XSLT > with PHP (or possibly any other language): > > I am still unsure of the best way to mix PHP & [X]HTML together in an > XSLT stylesheet, because regardless of whether you specify the output > method as "text" or "xml", if you are using HTML tags they must be > well-formed, because Sablotron or expat (not sure which) will want the > XSLT stylesheet to be a well-formed document. Only, we often interrupt > our HTML code when using PHP, like this: > > $output_to_browser = "<a href='index.php'>Go"; > $output_to_browser .= "home</a>"; > > (of course, the output to the browser will by a hyperlink to index.php > that says "Go home".) > > The above looks fine as PHP code, but if you try to manipulate the data > from an XSLT process in this fashion, you won't be able to use HTML > tags -- the greater-than and less-than symbols can't be used, since an > XSLT sheet is technically an XML document and these are not well-formed > tags. In the XSLT sheet, the above might look like: > > <xsl:template match="location"> > <a href="<xsl:value-of select="php_document" />">Go Home</a> > </xsl:template> > > I thought that perhaps if I specified text as the output method, then > the greater-than and less-than signs wouldn't be parsed, so I could use > them as such: > > <xsl:output method="text" /> > > <xsl:template match="location"> > <xsl:text> > <a href=" > </xsl:text> > <xsl:value-of select="php_document" /> > <xsl:text> > ">Go Home</a> > </xsl:text> > </xsl:template match="location"> > > See what's happening in the above? I thought I had "escaped" my <a> > tags by placing them within the <xsl:text> tags, but this is not so -- > they are parsed, and the document is then interpreted as not being > well-formed. > > > So unless you want to do a straight XML-to-XML or XML-to-XHTML > transformation, OR you don't want to use ANY XML or XHTML tags in your > output document, you're kind of up a river. Unless someone on this > list can help me find a way to "escape" the HTML tags when creating PHP > code. > > And the only way I can think of doing it (which I still haven't tested, > but might have to use) is to use variables to represent the HTML tags > so that instead of > > <a href=" and ">Go Home</a> > > I could use > > $astartag = "<a href='"; > $aendtag = "'>Go Home</a>"; > > and then make the style sheet like this: > > <xsl:output method="text" /> > > <xsl:template match="location"> > <xsl:text> > $astartag > </xsl:text> > <xsl:value-of select="php_document" /> > <xsl:text> > $aendtag > </xsl:text> > </xsl:template match="location"> > > > That should work in theory. But it's incredibly crude. > > > What do you all think? > > > > > > Erik > > > > > > > > ---- > > Erik Price > Web Developer Temp > Media Lab, H.H. Brown > [EMAIL PROTECTED] > > > -- PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php