Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link up XML file with XSLT file?

Tags:

xml

xslt

The examples at

http://en.wikipedia.org/wiki/XSLT

or

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

seem to be independent XML and XSLT files. Don't they have to be linked? Or do you somehow put them into a same file? Otherwise, how does one file know how to suck in data from the other file?

like image 245
nonopolarity Avatar asked Aug 11 '10 08:08

nonopolarity


People also ask

How do I link XML to XSLT?

Execute an XSLT transformation from an XML fileOpen an XML document in the XML editor. Associate an XSLT style sheet with the XML document. Add an xml-stylesheet processing instruction to the XML document.

How do you link an XSLT file with HTML file?

The regular way of doing this is to run an XSLT processor, giving it the XSLT and XML files. There is no need to modify the XML file in order to do this. You feed the XSLT and XML files to the processor, and it gives you the transformed output file, which will be HTML in your case.

How does XSL work with XML?

XSLT Processor takes the XSLT stylesheet and applies the transformation rules on the target XML document and then it generates a formatted document in the form of XML, HTML, or text format. This formatted document is then utilized by XSLT formatter to generate the actual output which is to be displayed to the end-user.


3 Answers

You can add this after the xml declaration

<?xml-stylesheet type="text/xsl" href="yourxsl.xsl"?>
like image 158
user414661 Avatar answered Oct 24 '22 11:10

user414661


One file doesn't know to "suck in data" the other file, because the files aren't what will do the processing.

Some sort of XSLT processor will do this, and the way it will be told what to work on varies so it can handle different use cases.

In the case of rendering the entire transform of an XML document when it is displayed in a browser, then processing-instruction:

<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>

(Really it should have been "text/xml" for the type as that's the mime-type of an XSL document, but this was in the tail-end of the browser wars and browser feature implementation was still often happening faster than the speed of common-sense).

If you are controlling the transform programatically using a library of some sort (there are objects for client-side javascript and libraries in any language you're likely to want to do this from), then you've got enough control to detail what gets transformed by what. Some interesting cases here include.

  1. You could even have a document with a node of content and a node of transforms, pick them out and run the transform.

  2. If you are running the same transformation on multiple XML documents, it is very often more efficient to call some sort of "PreCompile()" method or similar, which takes a hit on that call to benefit all the subsequent transforms.

  3. You can pass in values to top-level parameters in the XSLT.

like image 6
Jon Hanna Avatar answered Oct 24 '22 11:10

Jon Hanna


You can also make the transformation in an html page:

<script type="text/javascript">
  var xml = new ActiveXObject("Microsoft.XMLDOM")
  xml.async = false
  xml.load("some_xml.xml")
  var xsl = new ActiveXObject("Microsoft.XMLDOM")
  xsl.async = false
  xsl.load("some_xsl.xsl")
  document.write(xml.transformNode(xsl))
</script>
like image 5
Ido Weinstein Avatar answered Oct 24 '22 11:10

Ido Weinstein