-1

My requirement is like below:

Input :-

<Root>
<Name>abc</Name>
<Name>def</Name>
<Name>abc</Name>
<Name>abc</Name>
<Name>def</Name>
<Name>def</Name>
</Root>

Output:- I need to make xml of Name element who's value don't be repititive

<Root>
<Name>abc</Name>
<Name>def</Name>  
</Root>  

We can use xsl or xquery anything... Please help me on the same...

user3384223
  • 35
  • 1
  • 6

2 Answers2

2

In XSLT 2.0, use

<xsl:for-each-group select="/*/Name" group-by=".">
  <xsl:copy-of select="current-group()[1]"/>
</xsl:for-each>

In XQuery 1.0 use

for $n in distinct-values(/*/Name)
return <Name>{$n}</Name>
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
0

In addition to @michael-kay's excellent answer, if you have XQuery 3.0 then you could use:

distinct-values(/*/Name) ! <Name>{.}</Name>

The ! is the Simple Map Operator, which IMHO can be nicer for such simple one-line transformation cases than a for.

adamretter
  • 3,885
  • 2
  • 23
  • 43