Forum Moderators: open
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.
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
<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>