47

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?

2
  • 1
    I agree that some of the W3Schools pages, such as that one, are inadequate. They could have easily included mention of linking the stylesheet, as is done in XSLT - Transformation. Commented Jul 11, 2015 at 20:48
  • stackoverflow.com/questions/3456697/… link is now obsolete. Please update. Commented Nov 26, 2018 at 16:52

4 Answers 4

71

You can add this after the xml declaration

<?xml-stylesheet type="text/xsl" href="yourxsl.xsl"?>
Sign up to request clarification or add additional context in comments.

4 Comments

this is to put into the XML (data) file? Is this the only way or can you also tell an XSLT file to get data from which XML file, or have a 3rd file that includes both an XML file with an XSLT file?
if you use Altova XMLSpy you can add for example <?altova_samplexml gallery.clipflair.net/collection/activities.cxml ?> under the <?xml ...> clause. You can use relative or absolute URL/path there [the comment parser here seems to eat up the http:// prefix from my url]. Other tools may have similar syntax. It would be nice if one could use some standard syntax instead in the XSL file (since there are case like this one where the XML file is remote and you can't touch it)
alternative could be to include the XML file into another XML file somehow that specifies the stylesheet, but not sure if this is straightforward (might need to specially author the XPaths etc.)
That worked for me in Firefox, but did not show anything in Chrome.
6

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>

1 Comment

You got samples at: w3schools.com/xml/xml_parser.asp On how to do it in modern browsers :)
6

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.

Comments

2

You need an external tool or library to apply an Xslt transform to Xml. How you do this depends on your programming environment, however for .Net the XslCompiledTransform is the class used to apply an Xslt transform to a piece of Xml (either a file or Xml in memory).

Alternative you can use the Microsoft command line tool xslt.exe - you will need to research for yourself how to do the same thing in other programming languages / operating systems.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.