1

Is it possible to use a query string inside an XML file?

I did not think it was possible and then I saw a url like this http://site.com/file.xml?tag=howdoiusethis

I can think of a lot of ways that this could be useful, but it is a new concept to me and I am not finding any information on how to use this tag/ query string in the document. Ideally I would like to use it to filter out the xml that is readable in the document for the given tag/ query string.

Thanks

4
  • Did the browser scroll to that tag in the markup? Commented Feb 10, 2011 at 17:38
  • 1
    http://site.com/file.xml isn't any kind of file, it's a URL. Commented Feb 10, 2011 at 17:38
  • its a file on a server................ Commented Feb 10, 2011 at 17:40
  • it's not necessarily a file. The response to visiting that URL may be constructed by the server on the fly, without it being stored as a file anywhere. Commented Feb 10, 2011 at 17:58

3 Answers 3

2

Though it's an XML file, it's most likely a file that's being parsed by a server-side language (through either a handler, .htaccess assignment (e.g. AddType application/x-httpd-php .xml), or otherwise)

As long as the server knows that a file of type XML (in this case) needs to go through the parser, any server-side language (such as PHP, ASP) can then handle the file and output a valid XMl document (using query strings) and it appear as though it was a normal file.

A great example of this are .rss files. They are dynamic content that have a classic extension, but something server-side is rendering the information as it becomes available.


Case in point. Suppose you're running PHP on your server. You have a directory called "feeds" (/public_html/feeds/) which contains an XML feed. Within that directory, you create a file called .htaccess and tell apache it needs to send the .XML extension to the PHP processing engine:

AddType application/x-httpd-php .xml

Then, in that same directory you have stories.xml which generates a list of content based on database information and every query always renders the latest information from the server. This file could look something like the following:

<?php
  // this tells the client what kind of document this is
  header('Content-Type: application/xml');

  // pseudo database connection
  include_once('db.php');

  // setup the header:
  echo '<?xml version="1.0" encoding="utf-8"><stories>';

  // pseudo story-gatherer
  $stories = Stories::Fetch($_GET['filter_by']); // use of a GET variable
  foreach ($stories as $story){
    echo '<story>'
          .'<author>'.$story['author'].'</author>'
          .'<title>'.$story['title'].'</title>'
          .'<date>'.$story['date'].'</date>'
        .'</story>';
  }

  // close the file
  echo '</stories>';
?>

And now you have a file that ends in .XML and is filtered by a GET variable (Accessible via http://mysite.com/feeds/stories.xml?filter_by=Brad+Christie). To the user, it would only look something like this:

<?xml version="1.0" encoding="utf-8">
<stories>
  <story>
    <author>Brad Christie</author>
    <title>Making .XML render dynamic content</title>
    <date>2011-02-10 12:52:00</date>
  </story>
</stories>

Very primitive example, but just showing concepts not proper coding style. ;-)

Sign up to request clarification or add additional context in comments.

1 Comment

Great example. Thanks a lot. I am going to try and locate a full featured example and post a link on this page.
0

In your given example there is a URL rewriting mechanism behind the scenes which delegates this request to some controller which queries the given file using XPath or simple XML processing and search.

You would want to start here: http://www.w3schools.com/xpath/xpath_intro.asp

And then find XPath processing tools for you chosen programming language.

Comments

0

No, the tag variable is not available in the XML in any way. If it were, you couldn't process it, as XML is just a data format; it doesn't do anything on its own.

The presence of tag and its value are available to the webserver when it constructs the response to the HTTP request GET /file.xml?tag=howdoiusethis. The .xml part of that response is a clue to the client (browser) that XML will be sent back, but there need not exist a file.xml on the server. The server can do anything it wants with the information that you want /file.xml, and that tag is set to howdoiusethis.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.