user475464
user475464

Reputation: 1763

How find distinct data using xpath?

Below is my xml

<products>
    <product>
        <productid>1001</productid>
        <remarks>
            <title>This is remarks text of 1001</title>
        </remarks>
    </product>
    <product>
        <productid>1002</productid>
        <remarks>
            <title>This is remarks text of 1002</title>
        </remarks>
    </product>
    <product>
        <productid>1001</productid>
        <remarks>
            <title>This is remarks text of 1001</title>
        </remarks>
    </product>
    <product>
        <productid>1001</productid>
        <remarks>
            <title>This is another remarks text of 1001</title>
        </remarks>
    </product>
</products>

I need out put like below

  1. Take distinct remarks/title from there own productid
  2. need to take out the distinct remark of productid 1001

    Productid : 1001 Remark:This is remarks text of 1001

    Productid : 1001 Remark:This is another remarks text of 1001

Upvotes: 1

Views: 176

Answers (3)

Richard A
Richard A

Reputation: 2833

I'd group the product id and remarks together uniquely using the Muenchean method. There's quite a lot of information on the net about how to do this.

See if this question xslt-distinct-elements-and-grouping gives you any clues.

Upvotes: 0

Jayy
Jayy

Reputation: 2436

with xpath 2.0 we can try like this:

distinct-values(/products/product[productid='1001']/remarks/title)

Upvotes: 0

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243599

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kProdById" match="product" use="productid"/>

 <xsl:template match=
  "product
    [generate-id()
    =
     generate-id(key('kProdById', productid)[1])
    ]
  ">
  <xsl:apply-templates 
     select="key('kProdById', productid)/remarks"/>
 </xsl:template>

 <xsl:template match="remarks">
  <xsl:value-of select=
   "concat('Productid : ', ../productid,
           ' Remark:', title, '&#xA;')"/>
 </xsl:template>

 <xsl:template match="product"/>
</xsl:stylesheet>

when applied on the provided XML document:

<products>
    <product>
        <productid>1001</productid>
        <remarks>
            <title>This is remarks text of 1001</title>
        </remarks>
    </product>
    <product>
        <productid>1002</productid>
        <remarks>
            <title>This is remarks text of 1002</title>
        </remarks>
    </product>
    <product>
        <productid>1001</productid>
        <remarks>
            <title>This is remarks text of 1001</title>
        </remarks>
    </product>
    <product>
        <productid>1001</productid>
        <remarks>
            <title>This is another remarks text of 1001</title>
        </remarks>
    </product>
</products>

produces the wanted, correct result:

Productid : 1001 Remark:This is remarks text of 1001
Productid : 1001 Remark:This is remarks text of 1001
Productid : 1001 Remark:This is another remarks text of 1001
Productid : 1002 Remark:This is remarks text of 1002

Explanation: Using the Muenchian method for grouping.

Upvotes: 2

Related Questions