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

Subject: Re: [xsl] Unique list of elements based in attribute
From: Tech Guru <tech_gr@xxxxxxxxx>
Date: Wed, 7 Mar 2007 13:18:35 -0800 (PST)
Ohh  so much stressed up ..could not able to copy the correct stylesheet :(

I
just wanted to extract all <category> elements with unique id with changed
name as <Category Id='....'> from the XML provided . This is the stylesheet I
actually wanted to provide in the mail but 

<?xml version="1.0"
encoding="UTF-8"?>

    <xsl:output method="xml" version="1.0"
encoding="UTF-8" indent="yes"/>
    <xsl:key name="ParentKey" match="category"
use="@id"/>
    <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 -->
        <xsl:variable
name="id" select="@id"/>
        <xsl:if test
="not($id=following::category/@id)">
            
            <Category
xmlns:YMIA="urn:schemas-music-yamaha-com:ymia">
                <xsl:attribute
name="Id">
                    <xsl:value-of select="@id"/>
</xsl:attribute>
            </Category>
        </xsl:if>
        <!--
Selects category list of current category. XSLT logic no worry :)  -->
<xsl:apply-templates select="category_list"/>
    </xsl:template>
</xsl:stylesheet>


<xsl:if test ="not($id=following::category/@id)">, this
xsl:if does not seem to work correctly.

-R

----- Original Message ----
From:
David Carlisle <davidc@xxxxxxxxx>
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Sent:
Wednesday, March 7, 2007 3:56:30 PM
Subject: Re: [xsl] Unique list of elements
based in attribute


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
_____________________________________________________________________________
_______
We won't tell. Get more on shows you hate to love 
(and love to hate):
Yahoo! TV's Guilty Pleasures list.
http://tv.yahoo.com/collections/265

Current Thread