Jump to content

Utterly newbie question


GaryC

Recommended Posts

Dear all,I am new to XML/XSLT , have following questions, correct me if I am wrong(while it would be nice to have sample codes for some questions, others are just not sure about the basic concept)1. XSL is an umberlla term which consists XSLT, XPath and XSL-FO, but very often when we talk about using XSLT, we are referring to the .xsl file, right?2. One xml can use many xsl files at the same time, by calling <xsl:include href="??.xsl"/> for each xsl file? If so, where must the include statement be? in the beginning of file or anywhere? When to use import rather than include?3. calling include is not enough, must call <xsl:call-template name="??"/> with the ?? as the template name defined in that xsl file? If so, when and where I should call this call-template statement?4. very often in xsl file, you see this line <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">what is the difference between the first and second "xsl"? Which one is optional or can be changed to any other words? The URL is meaningless except for being as an unique identifier, so it does not matter whether the URL is valid or not?!5. Will you use XSL-FO to layout a web page or its only for printing? (I know you can use XSL-FO for on screen display, but practically do you use it for web pages layout? If not, how you do that/what you use?) 6. Does XSL-FO file .fo always saved as seperate file? or it is embedded into xml? xsl? file? If embedded, how? Any example?7. In real world, when do you use element base xml and when to use attribute base xml? Or in other words, the pros and cons8. Is it true that when talking aout XML database, we save data records in one xml file? If yes, then won't it be too big for a text file? If not, then what/how?9. Data -> XML -> (through)XSL -> (output as)XHTML Thats fine, but how many tools/applications involve in order to achive this? For example, what you need to get Data into XML? What you need to take XSL and XML and output as XHTML?10. XQuery for XML as SQL for DataBase? XPath is just how you perform XQuery, not a standalone thing? I so, why not using one name as 'SQL' but seperate into two?11. So far what I understand, in order to have formatted XML data output, you mix many HTML codes and XSLT/XSL-FO code in one file, making it very ugly(unclear) in my personal opinion (may be just because I have not get use to it). Is it the right way? Anyone with sample code to show me cleaner xml xsl and xsl-fo?Guess enough for now, may be post more from time to time.Thanks all in advanceRegards,GC

Link to comment
Share on other sites

1. Yes. Orginally there was only XSL, but it was then devided into XSLT and XSL-FO. Both of theese languages use XPath as a way of selecting elements and both refer to files with *.xsl extensions.2. The include statement can be called anywhere within the file as long as the included file doesn't make the whole thing invalid. For example, if there is a template inside the included stylesheet with match="/", and you call that stylesheet inside a template with the same match, this will give an error. Import is the sme as include with the only difference that it can only be called at the top of the document, before any templates.3. You just have to type the name of the desired template. XSLT doesn't allow duplicate templates, so there's no way of confusion. I haven't tryed calling a template from within an included file, but I think it will work as well.4. <xsl:stylesheet> is the beginning of the XSLT stylesheet. It's other equivalent is <xsl:transform>. There is no actual difference between the two, but W3C recommends using <xsl:stylesheet>. xmlns:xsl defines the XSL namespace, which you then on use inside the XSLT documet for every XSLT command. You can adjust the namespace URL, but if you do, it's likely that you'll encounter some problems. You can adjust the thing after the xmlns (the "xsl"), but then you would have to write every XSLT command with the new namespace instead. For example xmlns:xslt="http://www.w3.org/1999/XSL/Transform" would mean that all xslt elements must look like for example <xslt:stylesheet>.5. I personally don't like XSL-FO. I don't see any usage of it. XSLT can transform XML into XHTML and from then on, with different CSS stylesheets, you can make it good for printing, screen display and more.6. Normally XSL-FO is called as another file, the same way as XSLT (they were originally one language after all). Just like any language in the XML family, you can embed it inside an XML documet by calling the namespace, but there's no point, scince this will be messy and invalid and all. Don't embed any languages inside XML documents.7. Any data which should be extracted or has a possibility of expanding, should be written inside an element, and data, used for filtering, finding and other kind of "functional" purposes should be used inside an attribute. Of cource, this is not a "must". It's just a recommendation for a good and clean code.8. Yes. All the records are saved inside one file, each inside it's element. That's why we say that XML can't replace SQL databases. You can however, store small XML documents inside records of SQL database, use a server side scripting language to generate XML file from it and use XSLT to display that data. Cool, isn't it.9. You just write the data inside the XML document. That's what XML's purpose is. To carry data. Any text editor (such as notepad) can write all of the documents involved, so there's no "must" for any additional tools. If you view the XML file with previously applyed stylesheet, you'll see how the end result will look like. To see the XHTML source though, you'll need a server side script that would execute the transformation. See the "XSLT on the server" example to see what I mean.10. XQuery is a bit different then XPath. XPath is what URLs are for the browser and XQuery is what SQL statements are for a database. They have different purposes. I personally don't know much about XQuery, because I haven't got a need for it.11. XSLT files contain XHTML that would be outputted and their own XSLT elements, that are used to select and transform XML data. That's the way you bind the two. It's not ugly looking, especially if you format (indent) your code well. Refer to my previous posts in this forum and the XSLT tutorial for examples.Oh... that were a lot of questions. I felt as if I'm on an exam or something :) .

Link to comment
Share on other sites

First of all, thanks boen_robot for your time and effort.There are a few points I would like to add or mention after further surfing on the web.2. You said "...if there is a template inside the included stylesheet with match="/", and you call that stylesheet inside a template with the same match, this will give an error...."Somewhere I read even in the same xsl file you can have more than one template with same match, you can set the mode and/or priority properties so the processor knows which one to use, or simply by their order in the file. About the "/" error, not sure.4. I was in fact asking ...4.1 Valid

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

4.2 Valid or Not?

<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">

4.3 Valid or Not?

<stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

4.4 Valid or Not?

<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">

To test whether valid or not is simple, just type it and run, but doesn't mean I understand why so.Thanks again

Link to comment
Share on other sites

2. I have tryed adding a template inside a template with the same match, and I know it gives an error. As for duplicate templates in one stylesheet. Yes. There isn't an error, but just one of the templates is processed depending on the parsers' settings. IE and FF both render the last template with that match, because normally things written below have a higher priority.4. I don't think that understanding this matters really. It's well defined, and we're suppose to follow the model, not reinventing it. But if you really need to understand it, you should know that "xmlns" applyes the namespace to the element it's used on and it's childs. "xmlns:{some extension}" is only valid if it's used at the root of the document which in our case is xsl:stylesheet. It applyes the namespace to every element with the selected extension. Scince the <xsl:stylesheet> has the funcition of defining the start and end of an XSLT transformation, it needs to have the xsl prefix. From all of theese you can see the following:

<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">

Valid, however, every element in it must be an XSLT element or either have a reference to another namespace every time you need it. This practically disables you to transform XML into another XML.

<stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Invalid. The stylesheet element will not be applyed, thus every XSLT element inside will also not apply. The only way you can overcome this is if this is the root and you include <xsl:transform> below this.

<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">

I'm not sure I can answer this one actually, but I think that "xmlns" would override even previously defined namespaces, so therefore, the result will be pretty much like 4.1.

Edited by boen_robot
Link to comment
Share on other sites

Hi,very useful questions and answeres! Can I ask some more about XSL:FO.Im pretty new to XSLT and XSL-FO. Im working on a meta data solution for my integration project. It will be built using XSDs to define the business objects, XSDs or XML to define some base information and then some functionality to generate a contract; a document in preferrably PDF format. The contract should include the XML information. So I started looking at FO. My questions are:Is XSL-FO the best technique to use? Alternatives?Can I import or include an XML document containg data into my *.Fo document?Or must I enter the content in the *.fo file? I.e how do I separate content and format in a XSD-FO solution?Best regards,Lisa"5. I personally don't like XSL-FO. I don't see any usage of it. XSLT can transform XML into XHTML and from then on, with different CSS stylesheets, you can make it good for printing, screen display and more.6. Normally XSL-FO is called as another file, the same way as XSLT (they were originally one language after all). Just like any language in the XML family, you can embed it inside an XML documet by calling the namespace, but there's no point, scince this will be messy and invalid and all. Don't embed any languages inside XML documents."

Link to comment
Share on other sites

Hi,very useful questions and answeres! Can I ask some more about XSL:FO.Im pretty new to XSLT and XSL-FO. Im working on a meta data solution for my integration project. It will be built using XSDs to define the business objects, XSDs or XML to define some base information and then some functionality to generate a contract; a document in preferrably PDF format. The contract should include the XML information.  So I started looking at FO. My questions are:Is XSL-FO the best technique to use? Alternatives?Can I import or include an XML document containg data into my *.Fo document?Or must I enter the content in the *.fo file? I.e how do I separate content and format in a XSD-FO solution?Best regards,Lisa"5. I personally don't like XSL-FO. I don't see any usage of it. XSLT can transform XML into XHTML and from then on, with different CSS stylesheets, you can make it good for printing, screen display and more.6. Normally XSL-FO is called as another file, the same way as XSLT (they were originally one language after all). Just like any language in the XML family, you can embed it inside an XML documet by calling the namespace, but there's no point, scince this will be messy and invalid and all. Don't embed any languages inside XML documents."

First stop: XSD is XML Schema Definition as far as I know. I presume you were mistyping XSL the second and third time (I can't say about the first)?In my personal opinion, again there's no point of using XSL-FO, scince you could process the XSLT with a server side script and transform the XHTML output into PDF then. Infact, I don't know if there's a "clean" way with which you could execute XSL-FO as PDF. I haven't heard of such a thing, but if there is, then XSL-FO is truly the easiest way to go.Well, of course you could use data from XML inside an XSL-FO document. The idea of XSL-FO is to format an XML data after all. If XSL-FO alone can't do it, then a combination of XSLT and XSL-FO surely can.
Link to comment
Share on other sites

Hi!Well we are using schemas to define our business objects but that might not be important. What I want to do is to create a document in pdf including a template of a contract with some predefined chapters and then include xml information in pre defined places. "I don't know if there's a "clean" way with which you could execute XSL-FO as PDF."I dont know what you mean by clean, but I'm using Altova XMLSpy, I have downloaded a FO engine (fop.bat) from Apache and integrated it into XML Spy so that when I choose "XSL-FO transformation" for my *.fo file I get a PDF output very neatly.According to "XMLSpy Help" it should also be possible to define XSLT and the tool should handle a two step processing from XSLT to XSL-FO to PDF. I cant get it to work though. (The pdf is empty or I get a java exception.)Comments?Lisa

Link to comment
Share on other sites

By "clean" I mean without composing the full application for yourself. For example, XSLT could be used for transforming an RSS feed and then a server side code could execute that transformation. The other way is to use the server side script to read and transform the XML and execute it in the page. The second way is not "clean" because it relyes on heavy programming which takes lots and lots of code (not to mention CPU power).The same way, I don't know about any server side XSL-FO processors that would execute the XSL-FO file and generate a PDF file from the output but I'm completely positive that a heavy and overcrowded server side script could somehow process the whole XML after you have predefined each function of XSL-FO inside the server side script itself and execute the XSL-FO as if it's a test file with predefined by script functions.That apache extension you are talking about sounds a bit different though. And I don't get it completely. Anyway, if you could preview the XSL-FO output instead of getting error there, I don't think there should be a problem. But if the error is not in the XSLT or XSL-FO, I don't know what can you do.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...