Re: [xsl] Unique list of elements based in attribute

Subject: Re: [xsl] Unique list of elements based in attribute
From: David Carlisle <davidc@xxxxxxxxx>
Date: Wed, 7 Mar 2007 20:56:30 GMT
you havent said what output ypu wanted or what your stylesheet did wrong
but it has several lines that look strange 


<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <Category_List>
            <xsl:apply-templates select="category_list"/>
        </Category_List>
    </xsl:template>
    <!-- Any occurrence of category element in current context is applied -->
    <xsl:template match="category">
        <!-- check whether, category with same id is already processed or not -->
        <!-- Here I am trying to find any sibling categories with same id or not.-->
        <xsl:if test ="not($id=preceding-sibling::category/@id)">

You have not defined a variable $id, perhaps you meant @id.
You really want to group all elements with teh same ID in which case
google for xslt grouping, the xslt faq and jeni tennisons's site both
have many examples of this.

        
        <!--xsl:if test="@id!=ancestor::category/@id"-->

This is commented out, but beware never use != unless you are sure you
really mean that (and even then, use not(!=) instead.
@id != ancestor::category/@id
is true if the current id is not equal to any id on an ancestor, so that
will always be true if there is more then one id in the ancestor list.
I suspect you wanted
not(@id = ancestor::category/@id)
which is true if the current id is not equal to the id of an ancestor.

            <Category xmlns:YMIA="urn:schemas-music-yamaha-com:ymia">
Did you mean a capital C here rather than teh lower case name used elsewhere?
                <xsl:attribute name="Id">
                    <xsl:value-of select="@id"/>
            </xsl:attribute>
            </Category>

the above could more easily be written

            <Category xmlns:YMIA="urn:schemas-music-yamaha-com:ymia"
            id="{@id}"/>

        </xsl:if>
        <!-- Selects category list of current category. XSLT logic no worry :)  -->
        <xsl:apply-templates select="category_list"/>
    </xsl:template>




<xsl:if test ="not($id=preceding-sibling::category/@id)"> : This does not seem to work. Any input.
as noted above there is no variable with name id defined.

David

Current Thread