Skip to content Skip to sidebar Skip to footer

Create Link To Xml With Xsl Stylesheet

I have an XML file and 3 xsl files that tranform the same xml. I want to create a home page, with three buttons. Each button will redirect to one of three tranformations. How can I

Solution 1:

From your description ("home page, ...") I infer that all this should happen on the Web; in that case the answer will most likely involve the rules for configuring your Web server, so it's going to be a question about Apache, or IIS, or nginx, or Jetty, or whatever server is actually serving your documents.

There are many ways to achieve the goal; these are the first three or four that occur to me. For concreteness I will assume you are using Apache (many people do), know how to find and edit Apache configuration files, and can adapt relative references to suit your directory layout.

  • Assuming that what you want is precisely what @Stefan Hegny assumes you do not want.

    You save three copies of the XML document. The one named example.1.xml begins

    <?xml-stylesheet href="t1.xsl" type="text/xsl"?><example>
      ...
    

    The one named example.2.xml begins

    <?xml-stylesheet href="t2.xsl" type="text/xsl"?><example>
      ...
    

    And similarly example.3.xml begins with a reference to t3.xsl.

    The three buttons point to these three documents.

    If example.xml is still changing, you will want to automate the process of updating the three near-identical copies of it whenever the master document changes; I use Make and sed for such tasks, myself.

  • Another way to achieve the same thing, with a single copy of example.xml

    Another way to achieve the same effect is to maintain a single copy of example.xml, with a reference to t1.xsl (so it looks like the example.1.xml described above), and tell your server

    • Whenever a user requests the URI example.1.xml, serve document example.xml.
    • Whenever a user requests the URI example.2.xml, run the command sed -e s/t1.xsl/t2.xsl/ < example.xml and send the result (stdout) to the client.
    • Whenever a user requests the URI example.3.xml, run the command sed -e s/t1.xsl/t3.xsl/ < example.xml and send the result (stdout) to the client.

    In Apache, I use the Rewrite module to redirect these three URIs to a CGI script which inspects the URI by which it was called and runs the appropriate command.

    The three buttons continue to point to the three URIs example.1.xml, example.2.xml, and example.3.xml.

  • Running the stylesheet on the server

    If the three displays must work even with browsers that don't support XSLT, then you want to run the stylesheet on the server.

    Here, again, I use Rewrite to redirect the URIs to a CGI script, but instead of running sed, the CGI script runs xsltproc, or whatever XSLT processor is available on my server.

  • Running the stylesheet in the browser

    Another way to handle this requirement is to make index.xhtml be an XForms document, suited for a processor which supports the transform() extension function (e.g. XSLTForms). The document example.xml is referred to by an xf:instance element, and the three buttons invoke the three stylesheets on that instance. They might update an auxiliary instance, or they might simply cause different cases in an xf:switch to display. (If this mostly makes sense to you but you need more details, ask a question tagged XForms; if it doesn't make sense to you, then you probably don't know XForms and this is not the simplest path to the goal you describe.)

    Some people would use Javascript instead of XForms for this task, but browsers vary a lot in how they want their internal XSLT processor to be invoked, so unless you enjoy working around browser inconsistencies in Javascript, you might not want to go that way, either.

Post a Comment for "Create Link To Xml With Xsl Stylesheet"