1

I have this type of XML file :

<Product>
  <item1>...</item1>
  <item2>...</item2>
  <item3>...</item3>
</Product>
<Product>
  <item1>...</item1>
  <item2>...</item2>
  <item4>...</item4>
  <item5>...</item5>
</Product>
<Product>
  <item1>...</item1>
  <item6>...</item6>
</Product>

I would like to get the list of all unique children element of the parent type "Product", like this :

<item1>...</item1>
<item2>...</item2>
<item3>...</item3>
<item4>...</item4>
<item5>...</item5>
<item6>...</item6>

I don't care of what is under these "item" elements, I just want to list all types of "item" elements that can exist under all "Product" elements of my file, using XPATH 1.0 if possible. I want to display each "item" element one time only, even if they exist under several "Product" elements.

How can I do that ? Thank you for your help !

JCD70
  • 33
  • 4

2 Answers2

0

This XSLT-1.0 approach matches all children of Product elements and filters them by checking if a node with the same name/local-name has already occured in the preceding-axis.

<xsl:template match="//Product/*">
  <xsl:variable name="this" select="local-name()" />
  <xsl:if test="count(preceding::*[local-name()=$this]) = 0">
    Unique node: <xsl:value-of select="name()"/>
  </xsl:if>
</xsl:template>
zx485
  • 28,498
  • 28
  • 50
  • 59
0

Using xpath version 1.0, this returns all elements without duplicates.

//Product/*[not(../preceding-sibling::Product/*/local-name()=local-name())]

Using xpath version 2.0 , returns all values without duplicates.

distinct-values(//Product/*/local-name())
derloopkat
  • 6,232
  • 16
  • 38
  • 45
  • Do you know any free XML Tool which can run Xpath 2.0 queries ? I'm currently using XML Explorer ; it's quite good but only supports Xpath 1.0 :( – JCD70 Jul 19 '17 at 15:44
  • I have updated my answer following the suggestion made by Roman Vottner. According to https://www.w3.org/TR/xpath/ *preceding-sibling* should be supported by version 1. In the other hand once I've used Saxon open source implementation with .Net. See https://stackoverflow.com/questions/1525299/xpath-and-xslt-2-0-for-net – derloopkat Jul 19 '17 at 16:06
  • Xpath v1.0 code does not seem to work, but Xpath v2.0 does. I have managed to use it with Saxon HE command line application. Thank you ! – JCD70 Jul 20 '17 at 08:40