By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,044 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,044 IT Pros & Developers. It's quick & easy.

Distinct Attribute Names with XSL

P: n/a
I have seen examples of selecting distinct elements by name, but my
issue is that I need to get distinct attributes by name. My XML looks
like:

<root>
<file>
<rows>
<row att1="1" />
<row att2="2" />
<row att1="1" att2="2" />
</rows>
</file>
<file>
<rows>
<row att3="1" />
<row />
<row att4="2" />
</rows>
</file>
</root>

For each file element, I want to return a distinct list of attribute
names. I'm trying to create an HTML table based on this data in an
XSL. For example:
+-------+--------+
| attr1 | attr 2 |
+-------+--------+
| 1 | N/A |
+-------+--------+
| N/A | 2 |
+-------+--------+
| 1 | 2 |
+-------+--------+

+-------+--------+
| attr3 | attr 4 |
+-------+--------+
| 1 | N/A |
+-------+--------+
| N/A | N/A |
+-------+--------+
| N/A | 2 |
+-------+--------+

I'm sure the Muenchian Technique is the thing to use (utilizing a
custom @use clause on the <xsl:keyto "group by" the file element),
but I can't seem to get it to work correctly for the attributes.

Thanks in advance for your help.
-Damien

Apr 19 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a

Damien wrote:
I have seen examples of selecting distinct elements by
name, but my issue is that I need to get distinct
attributes by name.

<root>
<file>
<rows>
<row att1="1" />
<row att2="2" />
<row att1="1" att2="2" />
</rows>
</file>
<file>
<rows>
<row att3="1" />
<row />
<row att4="2" />
</rows>
</file>
</root>

For each file element, I want to return a distinct list of
attribute names. I'm trying to create an HTML table based
on this data in an XSL.
Here's a partial solution for you. It's a bit klugdy
(figuring out precisely what's kludgy about it is left as a
potentially enlightening exercise for the reader):

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="row-group-attrs" match="@*"
use="generate-id(ancestor-or-self::rows[1])"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template name="row-group-id">
<xsl:value-of
select="generate-id(ancestor-or-self::rows[1])"/>
</xsl:template>
<xsl:template match="row">
<xsl:variable name="this-row-group-id">
<xsl:call-template name="row-group-id"/>
</xsl:variable>
<xsl:copy>
<xsl:apply-templates
select="key('row-group-attrs',$this-row-group-id)"
mode="group-attr">
<xsl:with-param name="context" select="."/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="@*" mode="group-attr">
<xsl:param name="context"/>
<xsl:variable
name="source-attr"
select=
"
$context/@*
[
local-name()=local-name(current()) and
namespace-uri()=namespace-uri(current())
]
"/>
<xsl:choose>
<xsl:when test="$source-attr">
<xsl:apply-templates select="$source-attr"/>
</xsl:when>
<xsl:otherwise>
<xsl:attribute
name="{local-name()}"
namespace="{namespace-uri()}"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

--
Pavel Lepin
Apr 20 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.