xslt - Pass source xml file name as a parameter to the stylesheet -


as have understood there no way xslt 1.0 in stylesheet accept direct funtions "base-uri()" or simliar , have read method when "pass source xml file name parameter stylesheet".

what mean?

my situation have large number of xml-files , 1 stylesheet use of these files.

in head section, when xslt transformed, display source xml filename (i.e. file invoked stylesheet).

how done in simplest way?

many thanks!

/paul

assuming xslt transforms xml input html in html can use javascript read out window.location.href respectively components. usual way output client-side javascript in position document.write, not supported in gecko browsers in html result of xslt transformation. can use dom methods createtextnode , replacechild or appendchild, following tries , mimic effect of document.write replacing script element text node:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">  <xsl:template match="/">   <html>     <head>       <title>test</title>       <script>       function outputurl() {       var currentscripts = document.getelementsbytagname('script');         var lastscript = currentscripts[currentscripts.length - 1];         var fileurl = window.location.href;         var steps = fileurl.split('/');         var filename = steps[steps.length - 1];         lastscript.parentnode.replacechild(document.createtextnode(filename), lastscript);       }       </script>     </head>     <body>       <h1>test</h1>       <section>         <h2>file : <script>outputurl();</script></h2>       </section>       <xsl:apply-templates/>     </body>   </html> </xsl:template>  <xsl:template match="footer">   <section>     <h2>footer of file : <script>outputurl();</script></h2>   </section> </xsl:template>  </xsl:stylesheet> 

in online test http://home.arcor.de/martin.honnen/xslt/test2015071502.xml current versions of firefox, chrome , ie (on windows 8.1) way file name show in right places have not tested approach when more script elements present src attributes or defer or async. make sure put <span id="filename"></span> need file name , use document.getelementbyid('filename').textcontent = filename;, way output not depend on perhaps brittle approach of hoping document.getelementsbytagname('script') collection has current script last item.


Comments