Thanks, Mike.
Now I have a better idea of when I might use current-group. How much
performance improvement might one expect from this change? I suppose it'd
be at least some improvement, since the processor must have already built
some kind of key for current-group.
I thought about making count(/table/table) into a variable for the sake of
performance, but I wanted to hold down the number of lines and try to make
the idea as understandable as possible. It'd definitely improve the
performance, though (and the real problem at hand has a lot more than
three tables and twelve columns, so I did that in my real solution).
I also thought about the one-such-name-to-a-table problem, but the
company for which I am currently consulting would have all kinds of other
problems if they had multiple columns in the same table with the same
name. Also, I've never seen that done at any other company. Still, it's a
good thing to note, since I'm sure it happens somewhere. (I suppose it's
one of those useful tricks for certain problems, but I know very little
about database design - my ability stops at basic SQL.)
Anywho, thanks again.
Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)
"Michael Kay" <mike(_at_)saxonica(_dot_)com>
03/22/2005 04:26 PM
Please respond to
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
To
<xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
cc
Subject
RE: [xsl] Common Element Solution (XSL 2.0)
My suggestion would be to replace this:
<xsl:for-each-group select="table/column" group-by="@name">
<xsl:variable name="name" select="@name"/>
<xsl:if
test="count(/tables/table/column[(_at_)name=$name])=count(/tables/table)">
<columnName><xsl:value-of select="@name"/></columnName>
</xsl:if>
</xsl:for-each-group>
by this:
<xsl:for-each-group select="table/column" group-by="@name">
<xsl:if test="count(current-group())=count(/tables/table)">
<columnName><xsl:value-of
select="current-grouping-key()"/></columnName>
</xsl:if>
</xsl:for-each-group>
You could also move count(/tables/table) outside the loop into a variable.
Note that the algorithm only works if a column name can appear in a table
at
most once.
Michael Kay
http://www.saxonica.com/
-----Original Message-----
From: JBryant(_at_)s-s-t(_dot_)com [mailto:JBryant(_at_)s-s-t(_dot_)com]
Sent: 22 March 2005 21:23
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Common Element Solution (XSL 2.0)
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"?>
<tables>
<table name="table1">
<column name="col1"/>
<column name="col2"/>
<column name="col3"/>
<column name="col4"/>
</table>
<table name="table2">
<column name="col1"/>
<column name="col2"/>
<column name="col5"/>
<column name="col6"/>
</table>
<table name="table3">
<column name="col1"/>
<column name="col2"/>
<column name="col7"/>
<column name="col8"/>
</table>
</tables>
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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<commonColumns>
<xsl:apply-templates/>
</commonColumns>
</xsl:template>
<xsl:template match="tables">
<xsl:for-each-group select="table/column" group-by="@name">
<xsl:variable name="name" select="@name"/>
<xsl:if
test="count(/tables/table/column[(_at_)name=$name])=count(/tables/table)">
<columnName><xsl:value-of select="@name"/></columnName>
</xsl:if>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
Produces the following output with Saxon 8:
<?xml version="1.0" encoding="UTF-8"?>
<commonColumns>
<columnName>col1</columnName>
<columnName>col2</columnName>
</commonColumns>
FWIW
Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail:
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--