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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Provide XSLT Options From HTML Link 1

Status
Not open for further replies.

fawkes

Technical User
Sep 12, 2003
343
GB
Is it possible to open an xml file using a link from another page that selects the appropriate xslt stylesheet?

For instance, I have data in an xml file that contains metric measurements. I want to display this data in either metric or imperial (inch) units and all I want to do is allow someone to click on a link on an intranet page and open the data in table showing the units they prefer.

I can create the xml file and stylesheets easily enough it's just getting the link to use the correct stylesheet.

I'm trying to avoid use scripts.
 
There's several ways to do this. One is to create two XML files with same data, but different stylesheet references. Or you could use a querystring parameter and use javascript to do the transformation client-side. The best solution would be to do the transformation server-side with PHP, ASP or similar.

Jon

"I don't regret this, but I both rue and lament it.
 
I was trying to avoid any coding if possible.

Is it possible to get one xml file to call the data from another xml file?
 
How are you doing the transformation at the moment? How do you determine whether its metric or imperial?

Jon

"I don't regret this, but I both rue and lament it.
 
I'm not doing it yet, I haven't written the files, I'm waiting to find the best method.

The way I had assumed to do it was to have a single data file, with metric data for example, then an xslt file that placed the data into tables followed by a second xslt file that placed the data into tables but converted to inch units .
 
You having two seperate links? Or a checkbox or radio buttons or what? How will your app know whether its metric or imperial?

Why not have a table that has both?

You don't need to write two different stylesheets, you could do it in one.

The best way is server-side transform.

Jon

"I don't regret this, but I both rue and lament it.
 
I think I would use seperate links, I'm creating the menu screens from xml and stylesheets.

Here's my overriding problem:

I'm creating an intranet with data for people to use. This data has to be available to everyone through the internet and through calculation programs that might access the dimensions in the data.

It would be good practice to create a single data file and provide the user with a converted output.

If I leave the company I work for there is no one who could rewrite any hard coding I did and so I think that using xml and stylesheets would make a cheaper option for the company.
 
What will your links be then?

You could link to 2 separate XML files, that have same data but different stylesheet links:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="metric.xsl"?>
<mydata>......

and

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="imperial.xsl"?>
<mydata>......
Or you could have links that pass a parameter:
Code:
table.htm?units=metric

and

table.htm?units=imperial
This page could then perform the transform using javascript (server-side would be better tho). You could use a form to pass the querystring variables.

Jon

"I don't regret this, but I both rue and lament it.
 
What I've managed to do is use a parameter

Code:
<!-- Provide a parameter to hold the conversion factor -->
<xsl:param name="factor"/>

This is placed before any templates, I can then use the factor to peform a calculation

Code:
<xsl:value-of select="Dimension * $factor"/>

When I set a value for the factor it works fine.

Now, how do I pass a value to this parameter.

I've tried

Code:
<a href="myxml.xml?factor='25'">My XML with Factored Values</a>

but it doesn't pass the factor the stylesheet.

Any ideas?
 
You can't do it like that, unless you have some server-side progamming. How are you doing the transform? With an XML header reference?

Jon

"I don't regret this, but I both rue and lament it.
 
My stylesheet starts like this

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!-- Define the stylesheet -->
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0">

<!-- Set the output to html -->
<xsl:output method="html"/>

<!-- Provide a parameter to hold the conversion factor -->
<xsl:param name="factor"/>

and then continues with templates.
 
Where would I find resources to explain how I would provide a server-side program to pass the parameter
 
That doesnt explain how you are doing the transformation. Does you XML file look like:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="metric.xsl"?>
<mydata>......
Or are you using a programmatic method?

To do it server-side is fairly simple. The implemenation depends on what technology you decide to use. Microsoft ASP's are the norm, but lots of people also use the open-source PHP and less coldfusion.

Jon

"I don't regret this, but I both rue and lament it.
 
Sorry, newbie mistake.

My xml file looks the same as you've got it above.

I've noticed from trawling through the web that ASP is often used.
 
Is there a way of using java rather than asp?
 
Yes, you can do a client-side transformation using javascript (bear in mind that you cannot guarantee that a user has javascript enabled).

Another server-side option is to use a JSP (Java Servlet Page), similar to ASP.

Which do you wanna use?

Jon

"I don't regret this, but I both rue and lament it.
 
I'm unsure of which is better.

There are issues of whether the server will run asp or jsp and, as you say, whether the client has javascript enabled.

More research I think.
 
Here's what I did, I got this from


Code:
<!-- Server side script -->
<%

'Load the xml
set xml = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
xml.async=false
xml.load(Server.MapPath("myxml.xml"))

'Load the styelsheet
set xsl=Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
xsl.async=false
xsl.load(Server.MapPath("myxls.xsl"))

'Add parameters to the stylesheet
set XSLTemplate=server.CreateObject("MSXML2.XSLTemplate")
set XSLTemplate.stylesheet=xsl
set proc=XSLTemplate.createProcessor

'Set the source of the data
proc.input=xml
proc.addParameter "factor", 25.4

'Transform the file
proc.transform
response.Write proc.output

'Clean up
set proc=nothing
set XSLTemplate=nothing
set xls=nothing
set xml=nothing

%>

All I need to do now is to understand how I pass a parameter to an ASP file and extract that to set the value of the factor parameter.

Thanks for all your help Jon, I've awarded you a star for your effort.
 
Final answer

Code:
<!-- Server side script -->
<%

'Load the xml
set xml = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
xml.async=false
xml.load(Server.MapPath("IndexMenu.xml"))

'Load the styelsheet
set xsl=Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
xsl.async=false
xsl.load(Server.MapPath("IndexMenu.xsl"))

'Add parameters to the stylesheet
set XSLTemplate=server.CreateObject("MSXML2.XSLTemplate")
set XSLTemplate.stylesheet=xsl
set proc=XSLTemplate.createProcessor

'Set the source of the data
proc.input=xml
if request(trim("units"))="mm" then
	proc.addParameter "factor", 25.4
else
	proc.addParameter "factor", 1
end if

'Transform the file
proc.transform
response.Write proc.output

'Clean up
set proc=nothing
set XSLTemplate=nothing
set xls=nothing
set xml=nothing

%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top