Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying specific xml data

Status
Not open for further replies.

Beething

Technical User
Jun 30, 2005
5
GB
Hi,
I'm having trouble displaying a select bit of data from my xml in html.

the xml is a load of galleries, each containing paintings:

<galleries>
<gallery name="greydisplay">
<painting>
<photo>/paintings/kitchen.jpg</photo>
<title>Table Under Window</title>
<year>2002</year>
<media>Acrylic on canvas board</media>
<dimensions>10" x 14"</dimensions>
<price>&#163;90</price>
</painting>
...
</gallery>
...
</galleries>

The xslt is intended to just display the images from one of the galleries:
------------------------------------------------------
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl=" version="1.0">

<xsl:template match="//gallery[@name='greydisplay']">
<xsl:for-each select="painting">
<div class="painting" >
<a href="#"><img src="{photo}" /></a>
</div>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
-------------------------------------------------------

The results works well, except that straight after the images I get a load more of the xml, in raw form:

../paintings/kitchen.jpgRed Room2003Acrylic on canvas board12" x 16"£140../paintings/kitchen.jpgKitchen Scene2004Acrylic on canvas board16" x 12"£140../paintings/kitchen.jpgPlant on Chair2003Acrylic on canvas board12" x 16"£140../paintings/kitchen.jpgRed Interior2002Acrylic on canvas board14" x 10"£90../paintings/kitchen.jpgBlue Interior2002Acrylic on paper9" x 11"£40../paintings/kitchen.jpgRooftops in Prague2001Oil on canvas board14" x 18"sold../paintings/kitchen.jpgGreen Courtyard2000Oil on canvas board14" x 18"sold../paintings/kitchen.jpgGreen House2002Acrylic on stretched canvas18" x 14"£100../paintings/kitchen.jpgPalazzo2005Oil on stretched canvas18" x 14"£160

Not sure why this is displaying. Any ideas?
 
You havent told the XSL what to do with those nodes, so it will use the default template. Try:
Code:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0">
<xsl:template match="/">
  <xsl:apply-templates select="galleries/gallery[@name='greydisplay']"/>
</xsl:template>
<xsl:template match="gallery">
  <xsl:for-each select="painting">
      <div class="painting" >
        <a href="#"><img src="{photo}" /></a>
      </div>
  </xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Avoid use of "//". Very lazy and very inefficient.

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 

Thanks Jon,

That worked a treat.
I was wondering whether you (or anyone) knew if it's possible to pass in a variable to the xsl (from ASP)?. What I'd like is the name of the gallery (in the above case = 'greydisplay') to be dynamic.
Ben
 
XSL:
Code:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0">
<xsl:param name="name"/>
<xsl:template match="/">
  <xsl:apply-templates select="galleries/gallery[@name=$name]"/>
</xsl:template>
<xsl:template match="gallery">
  <xsl:for-each select="painting">
      <div class="painting" >
        <a href="#"><img src="{photo}" /></a>
      </div>
  </xsl:for-each>
</xsl:template>
</xsl:stylesheet>
ASP:
Code:
<%@LANGUAGE="VBScript"%>
<%
set xmlDoc = Server.CreateObject("Msxml2.FreeThreadedDOMDocument")
xmlDoc.async = false
xmlDoc.load(Server.MapPath("myxml.xml"))

set xslDoc = Server.CreateObject("Msxml2.FreeThreadedDOMDocument")
xslDoc.async = false
xslDoc.load(Server.MapPath("myxsl.xsl"))

set xslt = Server.CreateObject("Msxml2.XSLTemplate") 
xslt.stylesheet = xslDoc
set xslProc = xslt.createProcessor()
xslProc.input = xmlDoc
xslProc.addParameter("name", "some gallery")
xslProc.transform()
%>

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 

I'm having trouble getting this to work. I get the error:

msxml3.dll (0x80004005)
The stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed XML document.

and it points to the line:
xslt.stylesheet = xslDoc

Confusing because the xsl stylesheet is definately there and seems ok.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top