tags:

views:

62

answers:

1

I have XML like,

<items> 
  <item> 
    <products> 
      <product>laptop</product> 
      <product>charger</product> 
      <product>Cam</product>
    </products> 
  </item> 
  <item> 
    <products> 
      <product>laptop</product> 
      <product>headphones</product>  
      <product>Photoframe</product>  
    </products>   
  </item> 
  <item> 
    <products> 
      <product>laptop</product> 
      <product>charger</product>
      <product>Battery</product>   
    </products>   
  </item> 
</items> 

and I am using xslt on it

//can't change xpath as getting it from somewhere else

<xsl:param name="xparameter" select="items/item[products/product='laptop' and products/product='charger']"></xsl:param>

    <xsl:template match="/">

        <xsl:for-each select="$xparameter">

          <xsl:for-each select="products/product[not(.=preceding::product)]">
              <xsl:sort select="."></xsl:sort>
              <xsl:value-of select="." ></xsl:value-of>,
          </xsl:for-each>

          </xsl:for-each>

    </xsl:template>

I want the output to be

laptop
charger
cam
Battery

But I m not getting the result as I m expecting...distinct values is working fine ..something is getting wrong when I am adding that and claus

A: 

You need to sort all nodes in a node-set -- they are not siblings at all.

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:param name="xparameter" select="items/item[products/product='laptop' and products/product='charger']"></xsl:param>

  <xsl:template match="/">
    <xsl:for-each select="$xparameter/products/product">
     <xsl:variable name="vPos" select="position()"/>
     <xsl:if test="not(. = ($xparameter/products/product)[position() > $vPos])">
      <xsl:value-of select="concat(., ',')"/>
     </xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document, produces the distinct list of values:

Cam,laptop,charger,Battery,

You'll need another pass to sort them in the wanted order and to eliminate the trailing comma.

Dimitre Novatchev
Thanks a lot...
AB