È possibile passare un parametro a XSLT tramite un URL quando si utilizza un browser per trasformare XML?

StackOverflow https://stackoverflow.com/questions/65926

Domanda

Quando si utilizza un browser per trasformare XML (Google Chrome o IE7) è possibile passare un parametro al foglio di stile XSLT tramite l'URL?

esempio:

dati.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<root>
    <document type="resume">
        <author>John Doe</author>
    </document>
    <document type="novella">
        <author>Jane Doe</author>
    </document>
</root>

campione.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:output method="html" />
    <xsl:template match="/">
    <xsl:param name="doctype" />
    <html>
        <head>
            <title>List of <xsl:value-of select="$doctype" /></title>
        </head>
        <body>
            <xsl:for-each select="//document[@type = $doctype]">
                <p><xsl:value-of select="author" /></p>
            </xsl:for-each>
        </body>
    </html>
</<xsl:stylesheet>
È stato utile?

Soluzione

È possibile generare il lato server XSLT, anche se la trasformazione è lato client.

Ciò consente di utilizzare uno script dinamico per gestire il parametro.

Ad esempio, potresti specificare:

<?xml-stylesheet type="text/xsl"href="/myscript.cfm/sample.xsl?paramter=something" ?>

E poi in myscript.cfm produrresti il ​​file XSL, ma con lo script dinamico che gestisce il parametro della stringa di query (questo varierebbe a seconda del linguaggio di scripting utilizzato).

Altri suggerimenti

Sfortunatamente no: non puoi passare i parametri a XSLT solo sul lato client.Il browser web prende le istruzioni di elaborazione dall'XML;e lo trasforma direttamente con XSLT.


È possibile passare valori tramite l'URL querystring e quindi leggerli dinamicamente utilizzando JavaScript.Tuttavia questi non sarebbero disponibili per l'uso in XSLT (espressioni XPath), poiché il browser ha già trasformato XML/XSLT.Potrebbero essere utilizzati solo nell'output HTML renderizzato.

Basta aggiungere il parametro come attributo al file sorgente XML e utilizzarlo come attributo con il foglio di stile.

xmlDoc.documentElement.setAttribute("myparam",getParameter("myparam"))

E la funzione JavaScript è la seguente:

//Get querystring request paramter in javascript
function getParameter (parameterName ) {

   var queryString = window.top.location.search.substring(1);

   // Add "=" to the parameter name (i.e. parameterName=value)
   var parameterName = parameterName + "=";
   if ( queryString.length > 0 ) {
      // Find the beginning of the string
      begin = queryString.indexOf ( parameterName );
      // If the parameter name is not found, skip it, otherwise return the value
      if ( begin != -1 ) {
         // Add the length (integer) to the beginning
         begin += parameterName.length;
         // Multiple parameters are separated by the "&" sign
         end = queryString.indexOf ( "&" , begin );
      if ( end == -1 ) {
         end = queryString.length
      }
      // Return the string
      return unescape ( queryString.substring ( begin, end ) );
   }
   // Return "null" if no parameter has been found
   return "null";
   }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top