Subject: Re: XSL include problem From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx> Date: Tue, 15 Aug 2000 20:16:02 +0100 |
Athur, >I want to put my XML doc. within a "nested" table. If the header file is >well-formed, the table must be closed. It means the XML doc cannot be placed >within the table. Do you have any suggestion? There are two ways that you can go about doing this. The first way uses XML entities, but this time inserts the XML part of your document into the larger HTML file. The second way involves having the HTML file as a 'template', with some kind of element marking where the XML should be inserted. METHOD 1 -------- The HTML file that you produce using this method is actually an XML file. That means that in order to view it, you have to use a browser that understands XML so that it knows how to use XML entities. Your HTML file should look something like: ---- <?xml version="1.0"?> <!DOCTYPE html [ <!ENTITY xml-content SYSTEM "content.xml"> ]> <html> <body> <table> <tr><td>xxx</td></tr> <tr><td><table> &content; </table></td></tr> <tr><td>xxx</td></tr> </table> </body> </html> ---- As long as your XML content is a well-formed external parsed entity, then it should be automatically slotted into the relevant place by an XML-aware browser. METHOD 2 -------- This method is a little more complex. In this method you use an HTML-like XML file to give the template for the XML to be slotted into, and then use XSLT to merge the two files. Let's say you use a <xml-content /> element to indicate where you want the xml content to be inserted. Your template HTML file will look like: ---- template.html ---- <html> <body> <table> <tr><td>xxx</td></tr> <tr><td><table> <xml-content /> </table></td></tr> <tr><td>xxx</td></tr> </table> </body> </html> ---- Now, your XSLT stylesheet needs to access that document and copy most of the elements, aside from the xml-content element. First, create varaibles to hold the root nodes of the input document and the template document so that you know where you are: <xsl:variable name="xml-source" select="/" /> <xsl:variable name="html-template" select="document('template.html')" /> Creating copies involves using xsl:copy to copy the element, xsl:copy-of to copy all the attributes, and then processing all the content (so that the child elements are copied as well). You need to do it this way so that when you get to the exception (xml-content), you can do a different kind of processing. I'm going to make these templates be in 'copy mode', so that they're not mixed up with normal processing: <xsl:template match="*" mode="copy"> <xsl:copy> <xsl:copy-of select="@*" /> <xsl:apply-templates mode="copy" /> </xsl:copy> </xsl:template> Note that this copying copies the *elements* - you get the start and end tags of each of them, wrapped around their content. When you get to the xml-content element, you want to start processing the XML source: <xsl:template match="xml-content" mode="copy"> <xsl:apply-templates select="$xml-source" /> </xsl:template> Now, to start the whole thing off. Your root-node-matching template will be matching on the root node of the source XML. You want it to start processing the html-template nodes, in copy mode: <xsl:template match="/"> <xsl:apply-templates select="$html-template" mode="copy" /> </xsl:template> Of course, you can always just write the template HTML within your stylesheet, just inside the root-node-matching template, and process the XML inside it at the appropriate place. In the past I've thought that this was the best option, but you can imagine using a template technique to give extremely different structures to a page very quickly and easily, if that was what you were after. I hope that this helps, Jeni Jeni Tennison http://www.jenitennison.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: XSL include problem, Arthur Yeung | Thread | RE: Memory-saving Prescription for , John Robert Gardner |
RE: Any ideas, Chris Bayes | Date | RE: New book on XSLT, Darrin Bishop |
Month |