1

Using XSLT 1.0, how do I obtain the maximum number of distinct <info> elements that are children of the same <container> element in a structure like the following (if at all possible)?

<root>
  <container>
    <subelem sometimes_empty='yes'/>
    <subelem>
        <info>A</info>
    </subelem>
    <subelem>
        <info>B</info>
        <irrelevant>meh</irrelevant>
    </subelem>
    ...
  </container>
  <container>
    <subelem>
        <info>C</info>
    </subelem>
    <subelem>
        <info>C</info>
    </subelem>
    ...
  </container>
  ...
</root>

Using the distinct grouping solution offered here, I am able to get the number of distinct entries overall, but I'm actually interested on this number per <container> node, which will be lower. I've being trying to apply the functions count() and max() to this end, but my limited experience with XSLT/XPath hasn't allowed me to do so successfully. Any help would be much appreciated.

Community
  • 1
  • 1
Dologan
  • 4,554
  • 2
  • 31
  • 33

2 Answers2

2

Using XSLT 1.0, how do I obtain the maximum number of distinct elements per node in a structure like the following (if at all possible)?

Here is how to find the wanted maximum:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kContInfo" match="info" use=
  "concat(generate-id(ancestor::container[1]),'+',.)"/>

 <xsl:template match="/*">
  <xsl:for-each select="container">
    <xsl:sort data-type="number" order="descending" select=
    "count(*/info[generate-id()
                 =generate-id(key('kContInfo',
                                  concat(generate-id(ancestor::container[1]),
                                         '+',.)
                                  )
                             )])"/>
     <xsl:if test="position() = 1">
      <xsl:value-of select=
    "count(*/info[generate-id()
                 =generate-id(key('kContInfo',
                                  concat(generate-id(ancestor::container[1]),
                                         '+',.)
                                  )
                             )])"/>
     </xsl:if>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<root>
  <container>
    <subelem sometimes_empty='yes'/>
    <subelem>
        <info>A</info>
    </subelem>
    <subelem>
        <info>B</info>
        <irrelevant>meh</irrelevant>
    </subelem>
    ...
  </container>
  <container>
    <subelem>
        <info>C</info>
    </subelem>
    <subelem>
        <info>C</info>
    </subelem>
    ...
  </container>
  ...
</root>

the wanted, correct result is produced:

2
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • I didn't realise max() was XSLT 2.0 only. I guess that explains some of the errors I was getting... Thanks for the sorting trick for that. – Dologan Apr 30 '13 at 09:45
1

The link you reference is the right direction. In this case, you simply need a composite key like this:

<xsl:key
  name="kInfoByContainer"
  match="info"
  use="concat(generate-id(../../container), '+', .)" />

To confirm, when this XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes" method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:key
    name="kInfoByContainer"
    match="info"
    use="concat(generate-id(../../container), '+', .)"/>

  <xsl:template match="container">
    Container <xsl:value-of select="position()"/> has unique info elements:
    <xsl:value-of
      select="count(*/info[generate-id() = 
                           generate-id(key(
                             'kInfoByContainer',
                              concat(generate-id(../../container), '+', .)
                           )[1])])"/>

  </xsl:template>
</xsl:stylesheet>

...is applied against the provided XML:

<root>
  <container>
    <subelem sometimes_empty="yes"/>
    <subelem>
      <info>A</info>
    </subelem>
    <subelem>
      <info>B</info>
      <irrelevant>meh</irrelevant>
    </subelem>
  </container>
  <container>
    <subelem>
      <info>C</info>
    </subelem>
    <subelem>
      <info>C</info>
    </subelem>
  </container>
</root>

...the wanted result is produced:

 Container 1 has unique info elements:
 2
 Container 2 has unique info elements:
 1
ABach
  • 3,743
  • 5
  • 25
  • 33