[xsl] Common Element Solution (XSL 2.0)

Subject: [xsl] Common Element Solution (XSL 2.0)
From: JBryant@xxxxxxxxx
Date: Tue, 22 Mar 2005 15:22:44 -0600
This is neither a question nor an answer to someone else's question. I had 
to solve a problem today (finding all the common columns in an XML 
representation of a database), and it occurred to me that the solution 
might be useful for others. So, I'm posting for the sake of future archive 
searchers. For several years (since early 2000), I would occasionally need 
an XSL solution and would search the archives for solutions to my 
problems. Now that I use XSL all a lot (was only occasional before), I'm 
trying to give back a bit as my understanding grows.

Of course, if anyone does have comments, I welcome input. I'm sure I'll 
never stop learning (at least I hope not).

Given this XML file:

<?xml version="1.0" encoding="UTF-8"?>
  <table name="table1">
    <column name="col1"/>
    <column name="col2"/>
    <column name="col3"/>
    <column name="col4"/>
  <table name="table2">
    <column name="col1"/>
    <column name="col2"/>
    <column name="col5"/>
    <column name="col6"/>
  <table name="table3">
    <column name="col1"/>
    <column name="col2"/>
    <column name="col7"/>
    <column name="col8"/>

Find the columns shared by all tables, by column name.

The solution (in XSL 2.0):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 

  <xsl:template match="/">

  <xsl:template match="tables">
    <xsl:for-each-group select="table/column" group-by="@name">
      <xsl:variable name="name" select="@name"/>
        <columnName><xsl:value-of select="@name"/></columnName>


Produces the following output with Saxon 8:

<?xml version="1.0" encoding="UTF-8"?>


Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)

Current Thread