Apply XSLT to an XML file

前端 未结 7 2047
栀梦
栀梦 2020-12-29 08: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 havin

相关标签:
7条回答
  • 2020-12-29 08:52

    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> 
    
    0 讨论(0)
  • 2020-12-29 08:57

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

    0 讨论(0)
  • 2020-12-29 09:01

    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?

    0 讨论(0)
  • 2020-12-29 09:02

    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.

    0 讨论(0)
  • 2020-12-29 09:04

    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.

    0 讨论(0)
  • 2020-12-29 09:05

    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.

    0 讨论(0)
提交回复
热议问题