Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can xml transfer parameters to xsl through it's URL? 1

Status
Not open for further replies.

royboy75

Programmer
Feb 13, 2007
114
GB
Hello,

I am referring to an xsl file from xml in the standard way:
<?xml-stylesheet type='text/xsl' href='"<xsl_location>"'?>
My question is if it is possible to transfer parameters from the xml file to the xsl through the <xsl_location> url and then use them conditionally in the xsl?
If yes, a simple example will be appreciated.

Roy
 
Under the preoccupation of not to change the document data structure, you can use the processing-instruction to do the job. Even though purists have some reserve of pi, but it might stay for still a long while.

Suppose you want to set a top-level parameter nmode (or whatever name) in the xsl document.

[1] Make a new pi.
You give a name (actually quite arbitrarily) the pi, say, adding to the possible confusion, nmode.
[tt]
<?xml-stylesheet type='text/xsl' href='some-url'?>
[blue]<?nmode 1?>[/blue] <!-- the position of this can be very much free -->
[/tt]
Then in the xsl top-level parameter is scripted like this.
[tt]
<xsl:param name="nmode">
<xsl:value-of select="(//processing-instruction()[name()='nmode'])[1]" />
</xsl:param>
[/tt]
For more than one parameters to pass, just add accordingly more pi.

[2] In the same spirit, and if you prefer to handle all in the xml-stylesheet pi, you can similarly do this.
[tt]
<?xml-stylesheet type='text/xsl' href='some-url' [blue]nmode="1"[/blue]?>
[/tt]
and then script the xsl param like this.
[tt]
<xsl:param name="nmode">
<xsl:variable name="x" select="(//processing-instruction()[name()='xml-stylesheet'])[1]" />
<xsl:variable name="y" select="substring-after($x,'nmode=')" />
<xsl:variable name="z" select="substring-before($y,' ')" />
<xsl:variable name="a" select="translate($z,'&#x22;','')" /> <!-- &#x22; double-quote -->
<xsl:variable name="b" select='translate($a,"&#x27;","")' /> <!-- &#x27; single-quote -->
<xsl:value-of select="$b" />
</xsl:param>
[/tt]
(There seems a lot of aux variables just for clarity.) The same for more parameters to pass: just add on more name/value pairs.

[3] You can also in fact making more closely ressemble a query string. It is indeed possible, but you must properly encode & if you have more than one parameter to pass, in contrast to a ordinary querystring. And the parsing complicates further. It is doable.

I would prefer method [1] for its simplicity and clarity.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top