How to link xsl or xslt in xml

Please see: http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex1

The left file, as  Sample01.xml on my computer, shows just as xml in fire fox without the style (the code on the right I have saved as Sample01.xsl).

How can I make it work with style. I do not see any style link in Sample01.xml to Sample01.xsl.

Question: Could you please run this sample code to see if you could make it to show table rendering (titled: My CD Collection) on the bottom of the page as shown on the above link?
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gertone (Geert Bormans)Information ArchitectCommented:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
<head>
<style>
h2 {color:red;}
</style>
</head>
<body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <tr>
        <td>.</td>
        <td>.</td>
      </tr>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gertone (Geert Bormans)Information ArchitectCommented:
Tested in the two pane editor of w3schools, makes the h2 title show red
Gertone (Geert Bormans)Information ArchitectCommented:
you can also make it work with a link to a css, provided you have an URL for the CSS
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Mike EghtebasDatabase and Application DeveloperAuthor Commented:
What you have here is rendered html.

I want to test is on my computer not at w3. So, could you possibly give me your version of:
Sample01Xml.xml and Sample01Xslt.xslt

As it is, when I run Sample01Xml.xml (code on the left) I get xml file content displayed without any instruction in Sample01Xslt.xslt.

Show me how to link Sample01Xslt.xslt in Sample01Xml.xml. It has to work on your computer with these files save also on your computer.

Thanks,

Mike
Gertone (Geert Bormans)Information ArchitectCommented:
I am not sure what you ask here.

I answered your question: "How can I make it work with style?"

If you save the XML and the XSLT on your machine (just copy and paste the XML from W3schools and the XSLT I sent). In order to run the transform then, you need an XSLT processor (is that your question?)
You can download a selection of XSLT processors that run on the command line
http://saxon.sourceforge.net/
https://xml.apache.org/xalan-j/downloads.html
(the docs explain how to use it on the command line)
or you can download an IDE such as oxygenxml.com and run the XSLT in the developer environment

My comments about style still apply, except that you could then make an CSS relative to th eresult file to reference and not on a http server with url
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Geert Bormans,

I have added line 2: <?xml-stylesheet type="text/xsl" href="Sample01Xslt.xslt" ?> to
File name: Sample01Xml.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Sample01Xslt.xslt" ?>
<catalog>
	<cd>
		<title>Empire Burlesque</title>
		<artist>Bob Dylan</artist>
.
.

Open in new window


So that this file will know to use Sample01Xslt.xslt also in the same directory.

now it works.

Thanks
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for eghtebas's comment #a40966456
Assisted answer: 500 points for Gertone's comment #a40966415

for the following reason:

I have added line 2: <?xml-stylesheet type="text/xsl" href="Sample01Xslt.xslt" ?>
So that this file will know to use Sample01Xslt.xslt also in the same directory.

now it works.
Gertone (Geert Bormans)Information ArchitectCommented:
I am happy that this works for you BUT

That is not the answer to your original question.
"How can I make it work with style" was answered in my first comment
So I suggest you give that at least the appreciation of an assist

Your next comment starts with "What you have here is rendered html."
implying that is not what you want.
If you add the xml-stylesheet PI to your XML and you throw it in a browser, you still end up with "rendered" html, not with a transformation result.
Please try to phrase your questions properly next time around, you make it virualy impossible to answer them
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Hi Geert,

At: http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex1
I had two code blocks. When I copied them to my computer, it didn't work (I couldn't produce the table) as expected.

My question was how to make it work. I was suspecting (seeing that) the xml file has no way of knowing how to style. By trial and error I figured out that it was necessary to add:
<?xml-stylesheet type="text/xsl" href="Sample01Xslt.xslt" ?> right after xml declaration tag.

re:> Please try to phrase your questions properly next time around, you make it virualy impossible to answer them
This is a good observation. I am not skilled enough to state my questions clearly sometimes. But I will do my best to be careful and ask make clear statements. I appreciate this reminder.

Also, one thing I will never do is to make you upset because I possibly will need your help in my future questions. Do you want me to reopen this question and do required grading corrections?

Thanks,

Mike
Gertone (Geert Bormans)Information ArchitectCommented:
Hi eghtebas,

First, I think I should have toned down my response. I am not too interested in the points. And I just would have appreciated an assist on what I thought was the best possible answer to your question. Now it appears that is exactly what you did. So I need to apologize.

Second, don't worry about me being upset. My tone sometimes is harsh (blame that on the fact that English is not my mother tongue) but one unhappy feeling is not going to stop me from answering your questions

Most importantly here is some technical background:
There are various ways of executing an XSLT process and XML to HTML transformation is only a small usecase among all possible XSLT usecases. In order to get started you need an XSLT processor.
One early dream of XML was to bring structured information to the browser. Hence browsers have an XSLT processor built in. You can activate that XSLT processor in two different ways: by using the xml-stylesheet mechanism as you found as the answer to your question. The other way is to call the XSLT processor from javascript as you saw in the w3schools example.
There are two issues with this approach. Each browser uses a different implementation of the XSLT processor, so on edge cases there can be cross browser differences (erroneous handling of whitespace in IE is an example). And browsers only support XSLT1... worst issue however is that Chrome is about to get rid of the XSLT support in its attempt to make the browser memory footprint a lot smaller. So your xml-stylesheet calling might break in near future on some browsers. If you would use a javascript call to the XSLT processor, you might as well load a javascript library that does the XSLT with it, to avoid cross browser issues. The Saxon processor I mentioned earlier also has a Javascript variant that is worth checking... it supports XSLT2 in one go. Both browser driven transform build a rendered html
Other ways of running XSLT processes are in code (using libraries) in IDEs or via teh command line. That is the bulk of the transforms out there
In code could be on a server by teh way. In order to choose between a client transform or a server transform, there are quite a bit of parameters to take into account.

So, I hope this is interesting writing for you. And I hope you see where the confusion came from
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Hi Geert,

I am preparing to take exam 70-461 (SQL Server Querying SQL Server 2012). Last exam I took there were lots of xPath questions and I didn't do very good on that part. So, I decided once for all to study and understand XML and its related topic (xpath, xQuery etc.) in detail.

I am, now, studying Beginning XML by Wrox. Currently I am at page 60 out of 750. So, in next month or so, I will have better understanding of the topic thus able to ask smart questions. My questions, I agree, were not clear because I am just starting it.

BTW, points were all yours last time I closed the question. This is because when an asker closes his/her own answer cannot take any points.

Also, I do asp.net c# and vb.net where understanding xml will be very helpful as well.

Thank you for your help,

Mike
Gertone (Geert Bormans)Information ArchitectCommented:
The Beginning XML book from Wrox is a very good (and quiet extensive) book. I usually give a copy to students in my XML classes. However, make sure you have the fifth edition (first one including Liam Quin from W3C among the authors). The older ones tend to have a lot of loose ends

For XSLT and XPath in C# and vb.net you have access to XSLT1 msxml, which has some quirks. We will tackle them when you see them (Note that Saxon has a .net packaging and there is a new .net XSLT processor http://exselt.net/, if you are at some point looking for XSLT2 functionality inside .Net)

If you agree let's forget about the bumpy start of our relation here. Good luck with the preparation of your exam, and you know where to find us
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.