Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply XSLT to an XML file

Tags:

xslt

I realise that I will probably regret asking about this for the rest of my life, but... Is there some way of applying XSLT to an XML file without the XML file having an explicit reference to the XSLT file?

Personally, I thought the whole point of XSLT is that you can apply several different transformations to the same raw XML file to produce several different results from it. But that doesn't really work if the transformation has to be specified in the source XML file. It seems that to change the transformation, you have to change the underlying raw data file, which just seems wrong...

So is there some way to create some sort of file that says "take this XML and this XSLT and render the result in a browser window"?

Edit:

Perhaps my question was unclear.

If I open Notepad, write an XML file, and mention the name of an XSLT file within it, then when I double-click the XML file, the web browser applies the specified XSLT. Is there some way I can persuade the browser to do this without altering the original XML file? Or am I going to be forced to search for a command-line XSLT processor?

like image 778
MathematicalOrchid Avatar asked Feb 15 '12 16:02

MathematicalOrchid


People also ask

How do you link an XSLT stylesheet to an XML document?

Link the XSL Style Sheet to the XML Documentxml version="1.0" encoding="UTF-8"?> <? xml-stylesheet type="text/xsl" href="cdcatalog.

How do you transform one XML to another XML document using XSLT transform?

The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.

What is XSL how it can be used to present XML?

XSL gives a developer the tools to describe exactly which data fields in an XML file to display and exactly where and how to display them. Like any style sheet language, XSL can be used to create a style definition for one XML document or reused for many other XML documents.

How does an XSLT processor use an XSLT stylesheet with an XML document?

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.


2 Answers

Is there some way of applying XSLT to an XML file without the XML file having an explicit reference to the XSLT file?

Of course. In fact the XSLT specification doesn't rely (mention) at all on the XML file having a reference to the XSLT stylesheet to process it.

Thus it is possible for the same XML file to be processed by many, different XSLT transformations.

In XSLT 2.0 and up it isn't even required for an XSLT transformation to have a corresponding XML document to be applied upon.

How this can be done?

The short answer: This is implementation dependent -- read the corresponding XSLT processor documentation (e.g. XslCompiledTransform for .NET, Saxonica for Saxon, ..., etc).

Also, almost every XSLT processor has a command-line utility for invoking the transformation from the console window -- again check the respective documentation (msxsl.exe for MSXML, nxslt.exe for XslCompiledTransform, ..., etc.)

Here are some comannd-lines for XSLT processors I am using:

This invokes the MSXML 3 processor:

msxsl.exe %xml% %xsl%  -o %out% -u '3.0' -t %param[ name="value"]%

This invokes the MSXML 4 processor:

msxsl.exe %xml% %xsl%  -o %out% -u '4.0' -t %param[ name="value"]%

This invokes the MSXML 6 processor:

msxsl.exe %xml% %xsl%  -o %out% -u '6.0' -t %param[ name="value"]%

This invokes .NET XslCompiledTransform:

nxslt2.exe %xml% %xsl% -t  -o %out% %param[ name="value"]%

This invokes AltovaXML (XML-SPY) for XSLT 10:

 AltovaXML.exe -xslt1 %xsl% -in %xml% -out %out%%param[ name="value"]%

This invokes AltovaXML (XML-SPY) for XSLT 2.0:

 AltovaXML.exe -xslt2 %xsl% -in %xml% -out %out%%param[ name="value"]%

This invokes Saxon 9.x (for XSLT 2.0):

java.exe -Xms512M -Xmx512M  -jar C:\xml\Parsers\Saxon\Ver.9.1.0.5\J\saxon9.jar   -t  -repeat:1 -o %out%  %xml%  %xsl%  %param[ name=\"value\"]%

This invokes XQSharp (XSLT 2.0):

XSLT.exe -s %xml% -o %out% -r 1 -t   %xsl% %param[ name="value"]%

In all of the above, %xml% is the path to the XML file, %xsl% is the path to the primary XSLT file, %out% is the path to the file that will contain the output from the transformation.

%param[ name="value"]% is a list of name = value parameter specifications and this isn't mandatory to use.

like image 54
Dimitre Novatchev Avatar answered Sep 19 '22 07:09

Dimitre Novatchev


Why of course! :)

You simply need to invoke your desired XSLT processor supplying (at a minimum) the XSLT and the XML file to use. In fact internally this is what applications like Internet Explorer do explicitly when they detect that an XML document has referenced an XSLT file.

How you do this will depend on your environment, for example there are command line XSLT processors and you can also apply an XSLT in most programming languages, e.g. Applying an XSLT using C#.

like image 32
Justin Avatar answered Sep 23 '22 07:09

Justin