logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: wrap xml files
Author
a_jtim



Joined: 05 May 2010
Posts: 8

PostPosted: Wed 05 May '10 17:34    Post subject: wrap xml files Reply with quote

Hi, very new to this so I'm sure this is newbie question.
I've got a url like this: http://localhost/xml/myfile.xml

The file contains xml and I want to return it to the user in html that specifies a css stylesheet and puts the contents of the xml file in a <pre> block.

What I want is for the users to be able to view the raw xml with line numbers (which my stylesheet will handle). If there's a better way to accomplish that, please let me know.

If that's the way to go about it, what should I be reading about? Handlers? Filters?

thanks,
--Tim
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7373
Location: Germany, Next to Hamburg

PostPosted: Sat 08 May '10 23:41    Post subject: Reply with quote

I think the easiest way is using XSL. Than the browser would render the xml itself to html or you could use PHP that renders the xml with xsl to html
See http://www.php.net/manual/en/xsl.examples.php
Back to top
a_jtim



Joined: 05 May 2010
Posts: 8

PostPosted: Sun 09 May '10 15:43    Post subject: Reply with quote

Thanks for the suggestion, but what I need to display to the user is actually the line-numbered raw xml.

The reason is that it's part of a documentation build report--if there's an xml error, I report the line number and I want them to be able to see exactly where the error was found.

My guess at this point is to intercept all *xml files and use cgi to wrap them, but I really don't know.
thanks,
--Tim
Back to top
tdonovan
Moderator


Joined: 17 Dec 2005
Posts: 611
Location: Milford, MA, USA

PostPosted: Sun 09 May '10 16:18    Post subject: Reply with quote

If you are just looking for a 'cheap hack' to display XML files, try using mod_substitute like this:
Code:
LoadModule substitute_module modules/mod_substitute.so
<Directory htdocs/xml>
   <Files *.xml>
      AddType text/html .xml
      AddOutputFilterByType SUBSTITUTE text/html
      Substitute "s~<~\&lt;~"
      Substitute "s~>~\&gt;~"
      Substitute "s~\&lt;\?xml.*\?\&gt;~<HTML><HEAD><LINK href='mystylesheet.css' rel='stylesheet' type='text/css'></HEAD><BODY><PRE>$0~"
   </Files>
</Directory>

What it does is:
    changes the type of XML files to HTML
    changes all '<' to '&lt;'
    changes all '>' to '&gt;'
    prepends the include for your stylesheet and the <PRE> tag before the XML header (<?xml ... ?>). This presumes your XML files have proper XML headers.

Note that:
    The ~ character is used as the delimiter for mod_substitute's s command.
    $0 means put the regex that was found (i.e. put the XML header) into the output.
    This presumes your stylesheet is in the same directory as the XML file, and is named mystylesheet.css
    The <PRE> tag (and also the <BODY> and <HTML> tags) are never closed. This is incorrect, but most browsers will accept this anyway without complaint. That's why this is a 'cheap hack' rather than a good solution.


Hope this helps...

-tom
Back to top


Reply to topic   Topic: wrap xml files View previous topic :: View next topic
Post new topic   Forum Index -> Apache