Page 1 of 1

Serving XML Pages from Apache

Posted: Sat Jul 25, 2009 5:17 pm
by titan
So, I'm working on an XML class now and am trying to figure out how to properly serve an XML file from Apache. My searches have been fruitless, maybe.

I'm not sure what I need to have in order for the XSL to be read when the XML file is loaded. I'm kind of stumbling through XML now, so if there's an error with my own documents that I'm not seeing, here they are:

shoppinglist.xml:
<?xml version="1.0"?>
<!DOCTYPE shoppinglist>

<shoppinglist>
  <item>
    <description>Milk</description>
    <quantity>1</quantity>
    <price type="regular">$3.48</price>
    <price type="sale">$2.48</price>
  </item>
  <item>
    <description>Eggs</description>
    <quantitiy>1</quantitiy>
    <price type="regular">$1.96</price>
  </item>
  <item>
    <description>Coffee</description>
    <quantity>2</quantity>
    <price type="regular">$4.98</price>
    <price type="sale">$2.49</price>
  </item>
  <item>
    <description>Ground Beef</description>
    <quantity>5</quantity>
    <price type="perlb">$2.57</price>
  </item>
</shoppinglist>


shoppinglist.xsl:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
      <head>
   <title>Aaron's Grocery List</title>
   <link rel="stylesheet" type="text/css" href="http://aswenson.userworld.com/ff.css" />
      </head>
      <body>
   <xsl:apply-tmplate select="//item " />
      </body>
    </html>
  </xsl:template>
  <xsl:if test="boolean(price/@type)">
    (<xsl:value-of select="price/@type" />)
  </xsl:if>
</xsl:stylesheet>

Re: Serving XML Pages from Apache

Posted: Sat Jul 25, 2009 6:06 pm
by UberGerbil
It's been quite a while since I had to do anything like this so I'm probably forgetting something, but the easiest way to go about it is have a stylesheet ref tag right in the XML file
<?xml version="1.0"?>
<!DOCTYPE shoppinglist>
<?xml-stylesheet href="shoppinglist.xsl" type="text/xsl"?>
For more elaborate stuff, or when you don't want to do that, I think you need to be using libraries that allow you to transform the XML with the appropriate XSL. Have a look around, I know there are PHP (and probably every other Apache language) kits such as this or this (the first ones that popped up in a google).

Re: Serving XML Pages from Apache

Posted: Sun Jul 26, 2009 8:22 am
by titan
I figured it out. I failed to actually define the DOCTYPE in addition to not defining where to find the stylesheet.

<?xml version="1.0"?>
<!DOCTYPE grocerylist [
<!ELEMENT grocerylist (item+)>
<!ELEMENT item (description,quantity,price+)>
<!ELEMENT description (#PCDATA)>
<!ELEMENT quantity (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST price type CDATA #REQUIRED>
]>

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


No additions to Apache were necessary at all.