Submit Your Article Forum Rules

Results 1 to 6 of 6

Thread: Getting unique values from xml using XSL

  1. #1
    Junior Member
    Join Date
    Sep 2008
    Posts
    4

    Getting unique values from xml using XSL

    I have a requirement to dynamically generate a unique list of categories that will be used to populate a drop-down box.

    The XML that is being sent is in a format similar to this...

    <item>
    <title>random text1</title>
    <category>research</category>
    <category>marketing</category>
    <category>industry</category>
    </item>
    <item>
    <title>random text2</title>
    <category>research</category>
    <category>development</category>
    </item>
    <item>
    <title>random text3</title>
    <category>marketing</category>
    <category>fishing</category>
    <category>hunting</category>
    <category>development</category>
    </item>

    What i need is to be able to generate a list of all the unique "category" items, without having any duplicates.... eg in the example above, the list would be "marketing, fishing, hunting, research, development, industry"

    I've been using the following xsl, which works, but only when there is a single <category> in each <item> node, but i cant work out how to extend this to look at ALL the categories in the <item>. When i add additional <category> nodes, the xsl stops processing them. There was a lot of trial and error to get this far, but really struggling to get any further.

    <xslaram name="cat">all</xslaram>
    <xsl:key name="uniqueCategoryKey" match="item" use="./category"/>

    <select name="cat">
    <option value="all">all</option>
    <xsl:for-each select="//item[generate-id() = generate-id(key('uniqueCategoryKey', ./category))]">
    <xsl:choose>
    <xsl:when test="$cat = category">
    <option selected="{category}"><xsl:value-of select="category"/></option>
    </xsl:when>
    <xsltherwise>
    <option value="{category}"><xsl:value-of select="category"/></option>
    </xsltherwise>
    </xsl:choose>
    <br/>
    </xsl:for-each>

    </select>

    Any help appreciated on how to proceed

    Thanks

  2. #2
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    Re: Getting unique values from xml using XSL

    Quote Originally Posted by deadplant View Post
    I have a requirement to dynamically generate a unique list of categories that will be used to populate a drop-down box.
    What do you mean by a unique list of categories? Is it possible to define a unique top list element with the categories as child nodes?

  3. #3
    Junior Member
    Join Date
    Sep 2008
    Posts
    4

    Re: Getting unique values from xml using XSL

    Unfortunately not. The XML that is being sent can't be updated, as is getting used in other systems.

    When i say i need a unique list, i mean that the dropdown box i'm creating dynamically (just using <select><option value="cat1">cat1</option><option value="cat2">cat2</option></select>) can't have any duplicates appearing.

    That help any?

    Thanks

  4. #4
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    Re: Getting unique values from xml using XSL

    First of all this

    Code:
    <item>
    <title>random text1</title>
    <category>research</category>
    <category>marketing</category>
    <category>industry</category>
    </item>
    <item>
    <title>random text2</title>
    <category>research</category>
    <category>development</category>
    </item>
    <item>
    <title>random text3</title>
    <category>marketing</category>
    <category>fishing</category>
    <category>hunting</category>
    <category>development</category>
    </item>
    What i need is to be able to generate a list of all the unique "category" items, without having any duplicates.... eg in the example above, the list would be "marketing, fishing, hunting, research, development, industry"

    I've been using the following xsl, which works, but only when there is a single <category> in each <item> node, but i cant work out how to extend this to look at ALL the categories in the <item>. When i add additional <category> nodes, the xsl stops processing them. There was a lot of trial and error to get this far, but really struggling to get any further.

    Code:
    <xsl:param name="cat">all</xsl:param>
    <xsl:key name="uniqueCategoryKey" match="item" use="./category"/>
     
    <select name="cat">
    <option value="all">all</option>
    <xsl:for-each select="//item[generate-id() = generate-id(key('uniqueCategoryKey', ./category))]">
    <xsl:choose>
    <xsl:when test="$cat = category">
    <option selected="{category}"><xsl:value-of select="category"/></option>
    </xsl:when>
    <xsl:otherwise>
    <option value="{category}"><xsl:value-of select="category"/></option>
    </xsl:otherwise>
    </xsl:choose>
    <br/>
    </xsl:for-each>
     
    </select>
    is the correct code.

    Code:
    <xsl:key name="uniqueCategoryKey" match="item" use="./category"/>  This makes problems?
    Look at this http://www.webproworld.com/search-en...tml#post384523 example. Manufactures are child nodes of the mcatalog node.

    Generally, why do you not use the XML query language XPath 2.0 / XQuery to select nodes?

  5. #5
    Junior Member
    Join Date
    Sep 2008
    Posts
    4

    Re: Getting unique values from xml using XSL

    Thanks for your help... i've never really used any xpath before, but after reading this post, and googling for more help, i found an example that done exactly what i needed it to do, without usings keys.... I'm posting the solution below so that others can share.... Many thanks to all who helped!

    <xsl:for-each select="//category[not(.=preceding::category)]">
    <xsl:choose>
    <xsl:when test="$cat = category">
    <option selected="{category}"><xsl:value-of select="."/></option>
    </xsl:when>
    <xsltherwise>
    <option value="{category}"><xsl:value-of select="."/></option>
    </xsltherwise>
    </xsl:choose>
    <br/>
    </xsl:for-each>

  6. #6
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    Re: Getting unique values from xml using XSL

    Quote Originally Posted by deadplant View Post
    Thanks for your help... i've never really used any xpath before, but after reading this post, and googling for more help, i found an example that done exactly what i needed it to do, without usings keys.... I'm posting the solution below so that others can share.... Many thanks to all who helped!
    So this
    Code:
    <xsl:for-each select="//category[not(.=preceding::category)]">
                        <xsl:choose>
                            <xsl:when test="$cat = category">
                                <option selected="{category}"><xsl:value-of select="."/></option>
                            </xsl:when>
                            <xsl:otherwise>
                                <option value="{category}"><xsl:value-of select="."/></option>
                            </xsl:otherwise>
                        </xsl:choose>
                        <br/>
                    </xsl:for-each>
    was the solution.
    1. Great that my answers made you find the solution.
    2. Also great that you returned and posted the solution.

Similar Threads

  1. PHP, MySQL, a form and 1 too many values!
    By bizgen in forum Database Discussion Forum
    Replies: 3
    Last Post: 05-28-2008, 03:58 PM
  2. Unique gift store looking for gift directories, unique sites
    By regmanabq in forum Marketing Strategies Discussion Forum
    Replies: 5
    Last Post: 10-05-2004, 02:31 AM
  3. Need help with passing values in URL using PHP & MySQL
    By Adamwlad in forum Database Discussion Forum
    Replies: 1
    Last Post: 01-26-2004, 12:53 PM
  4. transparent values for cells
    By webweaver in forum Graphics & Design Discussion Forum
    Replies: 9
    Last Post: 01-22-2004, 05:06 PM
  5. PHP Generic check of URL values
    By Lionelandre in forum Web Programming Discussion Forum
    Replies: 4
    Last Post: 12-21-2003, 12:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •