1

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

Bohemian
  • 412,405
  • 93
  • 575
  • 722
user475464
  • 1,741
  • 10
  • 26
  • 38

3 Answers3

2

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.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
0

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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Richard A
  • 2,783
  • 2
  • 22
  • 34
0

with xpath 2.0 we can try like this:

distinct-values(/products/product[productid='1001']/remarks/title)
Jayy
  • 2,368
  • 4
  • 24
  • 35