Forum Moderators: open

Message Too Old, No Replies

XSLT and images

         

nightqueen

2:59 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



I have a php page and i want to display the result of a XSL file in it.

Part of the XML file looks like this:
<xml>
<restaurants>
<name>
<type>
</restaurants>
<restaurants>
<name>
<type>
</restaurants>...
</xml>

The type value could be 1 of a few values. If I want to display a list of these values how would I do this with XSL? To display just one of each value of type alphabetically. Similar to the way group works in sql.

brucec

4:37 pm on Mar 7, 2005 (gmt 0)

10+ Year Member



nightqueen, you said that you want this to work similar to SQL. Consider using XQUERY, which works just like SQL, but uses XPath. You can read up on XQuery, but consider this:

For distinct $r in //restaurants/type
Let $n :=//restaurants/name
Order by $r
return
<restaurants>
<type>{$r}</type>
<name>{$n}</name>
</restaurants>

As you can see, a lot of this is similar to SQL. In Xquery, they call it the FLWOR method (for, let, where, order by, return).

I hope this helps. Check out the basics of XQuery on any XML dedicated web site.

I hope this helps,
Bruce

irnbru

1:22 pm on Mar 8, 2005 (gmt 0)

10+ Year Member



Your looking for something called the Muench method.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>

<xsl:key name="types" match="restaurants" use="type"/>

<xsl:template match="/">
<xsl:call-template name="muench"/>
</xsl:template>

<xsl:template name="muench">
<xsl:for-each select="//restaurants[generate-id(.)=generate-id(key('types',type)[1])]">
<xsl:sort select="type"/>
<xsl:text>Type is </xsl:text>
<xsl:value-of select="type"/>
<xsl:value-of select="$newline"/>

<!--
<xsl:for-each select="key('types',type)">
<xsl:sort select="name"/>
<xsl:text>Name is </xsl:text>
<xsl:value-of select="name"/>
<xsl:value-of select="$newline"/>
</xsl:for-each>
-->
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

nightqueen

4:37 pm on Mar 8, 2005 (gmt 0)

10+ Year Member



Thanks for the responses. I had a look at XQuery but couldn't find any way to group the results.

The muench method worked perfectly. Thank you.