[xsl] Muenchian grouping with either attribute

Subject: [xsl] Muenchian grouping with either attribute
From: "Jack Tanner" <ihok@xxxxxxxxxxx>
Date: Thu, 14 Sep 2006 09:58:50 -0400
My apologies in advance. I'm sure this is a FAQ, but I'm having a hard time
piecing the answer together.

The input is like this:

<root>
 <item>
  <property attr1="foo" attr2="bar" />
  <property attr1="bar" attr2="foo" />
  <property attr1="bar" attr2="baz" />
 </item>
</root>

The property elements are not guaranteed to all have attr1 and attr2; some
might have attr3, some might be missing attr1, etc.

The ultimate goal is an output tally like this:

<properties>
  <property name="foo" freq_attr1="1" freq_attr2="1" />
  <property name="bar" freq_attr1="2" freq_attr2="1" />
  <property name="baz" freq_attr1="0" freq_attr2="1" />
</properties>

That is, if property@attr1="bar" twice in the input, that is reflected
accordingly.

I'm close; I can do the following.

<xsl:template match="property">
  <xsl:variable name="name">
    <xsl:value-of select="@attr1|@attr2" />
  </xsl:variable>
  <property>
    <xsl:attribute name="name">
      <xsl:value-of select="$name" />
    </xsl:attribute>
    <xsl:attribute name="freq_attr1">
      <xsl:value-of select="count(/item/property[@attr1 = $name])" />
    </xsl:attribute>
    <xsl:attribute name="freq_attr2">
      <xsl:value-of select="count(/item/property[@attr2 = $name])" />
    </xsl:attribute>
  </property>
</xsl:template>

The tallies in the output are correct, but because the variable "name" is not
restricted to unique values, I get multiple occurrences of each property.

I know that the standard answer in cases like this is "do the Muenchian". But
all the examples I've found deal with the Muenchian over elements, whereas
here, I need a Muenchian on the combination of attr1 and attr2. How do I do
that?

Thanks in advance. For what it's worth, I'm using libxslt2, and I need to
stick to XSLT 1.0.

_________________________________________________________________
Search from any Web page with powerful protection. Get the FREE Windows Live
Toolbar Today!
http://get.live.com/toolbar/overview

Current Thread