Re: [xsl] looking for best way to group elements

Subject: Re: [xsl] looking for best way to group elements
From: Bill French <bfh@xxxxxxxxxxxxx>
Date: Fri, 08 Sep 2006 12:55:39 -0700
Hi again,

Looks like I've answered my own question.

Here's the template I wrote:

<xsl:template match="rm:group[@name = 'Languages']/rm:field[@name = 'ppl_language']" priority="3">
<xsl:variable name="pos" select="position()"></xsl:variable>
<div class="group">
<span class="meta-name">Language</span>
<span class="meta-value"><xsl:copy-of select="./node()"/></span>
<span><xsl:value-of select="../rm:field[$pos + 1]/@displayName"/></span>
<span><xsl:copy-of select="../rm:field[$pos + 1]/node()"/></span>
<span><xsl:value-of select="../rm:field[$pos + 2]/@displayName"/></span>
<span><xsl:copy-of select="../rm:field[$pos + 2]/node()"/></span>
<span><xsl:value-of select="../rm:field[$pos + 3]/@displayName"/></span>
<span><xsl:copy-of select="../rm:field[$pos + 3]/node()"/></span>
</div>
</xsl:template>


If anyone can think of a more elegant solution, I'd still love to see it.

Regards,

--Bill

Bill French wrote:
Hi,

I'm struggling to transform XML that looks like this:

<group name="Languages" displayName="Languages" order="4">
<field name="ppl_language" displayName="Language"> Spanish </field>
<field name="ppl_language" displayName="Language"> Portuguese </field>
<field name="ppl_language_native" displayName="Native?"> Yes </field>
<field name="ppl_language_native" displayName="Native?"> No </field>
<field name="ppl_language_speak" displayName="Spoken"> Fluent </field>
<field name="ppl_language_speak" displayName="Spoken"> Functional </field>
<field name="ppl_language_write" displayName="Written"> Fluent </field>
<field name="ppl_language_write" displayName="Written"> Survival </field>
</group>


What I need is output that looks like this:

Language: Spanish
Native?: Yes
Spoken: Fluent
Written: Fluent

Language: Portuguese
Native?: No
Spoken: Functional
Written: Survival

That is, the source document contains <field/> elements that are returned grouped by their @name attribute values. The <field/> elements are always returned in the order stated above, ppl_language, ppl_language_native, ppl_language_speak, and ppl_language_write. There can be an arbitrary number of language entries in the list, but it is guaranteed that for every entry, all four <field/> elements will exist.

I am having trouble devising a strategy/writing a match pattern to group the elements correctly. Can anyone make a suggestion? Obviously, the problem is that there is only one <group/> element that is the parent for all <field/> elements, no matter how many <field/> elements there are. I'm using XSLT/XPath 2.0.

Best regards,

--Bill

Current Thread