Apply XSLT to an XML file

2019-02-04 17:20发布

问题:

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?

回答1:

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.



回答2:

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#.



回答3:

Simple method here

http://www.devguru.com/content/technologies/xml_dom/16058.html

This method supports both standalone and embedded style sheets, and additionally provides the ability to run a localized style sheet fragment against a particular source node.

This method processes this node and its descendants using the specified XSL stylesheet, and returns the resulting transformation.

<html>
<head>
<script> 
    xmldoc = new ActiveXObject("Microsoft.XMLDOM"); 
    xmldoc.async = false; 
    xmldoc.load("xmldoc.xml"); 
    xsldoc = new ActiveXObject("Microsoft.XMLDOM"); 
    xsldoc.async = false; 
    xsldoc.load("xsldoc.XSL"); 
 </script> 

 <script> 
    document.write(xmldoc.transformNode(xsldoc)); 
 </script>
</head>
<body> 



回答4:

Yes, it is possible. All XSL-T implementations have API to execute XSL-T transformation over an XML file.



回答5:

Well, it seems that you're looking for some sort of ready solution. Instead of writing Java or .NET code to run XSLT on XML file. In Java world there is http://en.wikipedia.org/wiki/Apache_Cocoon library that allows that.



回答6:

Responding to the OP's edit requirement to pick different XSLTs from the browser.

I didn't do any investigating before answering, but way back when IE was the only practical game in town, I remember there was a default CSS that got applied to XML files in the browser. Since CSS and XSLT files are specified in documents with a processing instruction, might there be some way through plugins/extensions to arbitrarily override the browser's default stylesheet - assuming other browsers handle this in a similar way?

Or might there be a way to dynamically add the processing instruction to the document and refresh?



回答7:

All of those responding to this question (including me) seem to be unsure of what exactly you are asking. As you haven't marked any of the answers as being correct yet, I will go out on a limb and suggest that what you might be looking for is a HTML/AJAX(Asynchronous JavaScript and XML) solution. This way you can tell the browser to use a different XSLT style-sheet depending on certain conditions (eg. the browser is IE, or the date is New Years day, etc.).

Instead of pasting the code here, I will point you to a blog post which I believe covers exactly what you are looking for (you will need to add the conditions yourself): http://johanlouwers.blogspot.co.uk/2011/05/render-xml-into-div-using-ajax-and-xsl.html

Note: The example above uses two XML Docs & one XSLT Stylesheet, but can be easily adapted to use two Stylesheets & one XML Doc.

Note: If you run the example do it using your browser directly, as I have found launching the test.html file from an IDE like WebStrom results in the noting happening when the links are clicked.

If this answers your question (or is the closest to answering it), please mark it as the chosen answer so this post can be listed as solved.



标签: xslt