Open XML page with reference to XSL stylesheet based on form submission

Posted on 2011-05-11
Last Modified: 2012-05-11
Hello Experts,

I have been trying to figure out how to even ask this question because I wasn't familiar with XML and XSL stylesheets until now. Now that I understand what needs to happen, I'm hoping that someone can help me with the code to do this. I'm assuming that i need to use PHP, but I'm open to suggestions.

I want to provide people with a form that has a couple of fields, but for now, let's just say that I'm only giving them one code. They enter a zip code, they click submit. What I'm looking to do is to take the information typed into that input field and add it on to a URL string that will always be the same. So, for instance, if someone types in 89436 the URL would be or if someone types in 75757 the URL would be

Now, the URL that is created above is returning a ton of XML data that I want to apply an XSL stylesheet to. So, ultimately what I'm trying to do is once that submit button is clicked I want to open the URL that is created (which is NOT on a server that I control) and apply a stylesheet to it.

Any ideas? I've tried asking this before, but again, I didn't fully understand what I was trying to do. Please provide as much detail as you can.....thank you!
Question by:ambientsbs
    LVL 10

    Accepted Solution

    What i would in this situation is use cURL for requesting the XML from the remote server.
    Once you have a copy of it, then you can apply your own XSL stylesheet to it, then output it.

    $zip = isset($_GET['zip']) ? trim($_GET['zip']) : '89436';

    $url = ''.$zip;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $xml_output = curl_exec($ch);

    /* Apply your stylesheet reference by manipulating the XML output */

    // echo $xml_output;

    LVL 107

    Expert Comment

    by:Ray Paseur
    Absent the specifics like the actual URL and the test data, we can only tell you about the general terms of the design pattern.

    For a form to add information to a URL, you would use a GET method request.  Try this script:
    <?php // RAY_temp_ambientsbs.php ?>
    <form action="RAY_bounce_get.php" method="get">
    <input type="text"   name="location" />
    <input type="submit" name="whatever" value="Go!" />

    Open in new window

    That script makes a post to another script that shows you how to get the contents of the URL.
    <?php // RAY_bounce_get.php
    echo "<pre>" . PHP_EOL;

    Open in new window

    The XML/XSL part of this is really a separate learning exercise.  Start with var_dump() to visualize whatever the remote site produces.  If is valid XML, you might try using SimpleXML methods to put it into an object so you can use parts of the XML string without having to use all of it.

    This may be helpful:

    Author Comment

    Thank you both for your replies! I will try these solutions out and get back shortly with any questions or to let you know if one of the solutions worked. MUCH appreciated!!

    Author Comment

    Hi Ray Soloman,

    I've finally had a chance to get to this piece and am wondering what my code needs to look like in the sections where you indicated:

    /* Apply your stylesheet reference by manipulating the XML output */

    // echo $xml_output;

    Sorry, I'm not great at PHP so any insight you can give would be helpful. Thanks!
    LVL 107

    Expert Comment

    by:Ray Paseur
    Please read this brief piece, then use the Google search suggested after it.  There is no short answer unless you have budget to pay a professional programmer to write your code for you.  If you do, then time is money and you can exchange one for the other.

    When the question includes, I didn't fully understand what I was trying to do that is almost always a good time to call in the pro!  There is nothing wrong with getting professional help.  I do not work on my cars or design the interiors of my office -- these are things better left to those with direct subject matter experience.

    Best of luck with it, ~Ray

    Author Comment

    Thanks for your input, but honestly, I have to disagree a little bit Ray Paseur. Being new to this type of project, but still having a basic understanding of coding in general, sometimes it just takes a little guidance. Both of your comments were helpful but I ended up figuring out how to do this on my own. Here is the code that I created:

    $zip = Trim(stripslashes($_POST['Zip']));

    $xsl = new DOMDocument();
    $xsl -> load("");
    $xml = new DOMDocument();
    $xml -> load(''.$zip);

    $proc = new XSLTProcessor();
    $proc -> importStylesheet($xsl);

    echo $proc -> transformtoXML($xml);


    And it works PERFECTLY.......

    Author Closing Comment

    This solution got me to the final solution which I've posted....

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
    In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
    HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now