473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Distinct Attribute Names with XSL

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
1 3317

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
4
by: Florian | last post by:
Hi, I have a table that contains log data, usually around a million records. The table has about 10 columns with various attributes of the logged data, nothing special. We're using SQL Server...
8
by: Richard | last post by:
This is probably easy but I can't work it out. I have this statement SELECT DISTINCT TOP 100 PERCENT dbo.CIF_PlaceReference.Name FROM dbo.CIF_Departures INNER JOIN dbo.CIF_PlaceReference...
6
by: Alex Hunsley | last post by:
I know that I can catch access to unknown attributes with code something like the following: class example: def __getattr__(self, name): if name == 'age': return __age else: raise...
1
by: Alex Satrapa | last post by:
I have a table from which I'm trying to extract certain information. For historical reasons, we archive every action on a particular thing ('thing' is identified, funnily enough, by 'id'). So the...
4
by: Iain | last post by:
I've an xml document that looks a bit like this <Vendors> <Vendor Stationery="Fred" /> <Vendor Stationery="bert" /> <Vendor Stationery="bert" /> </Vendors> I want to extract a list of the...
3
by: orekinbck | last post by:
Hi There Our test database has duplicate data: COMPANYID COMPANYNAME 1 Grupple Group 2 Grupple Group 5 Grupple Group 3 Yada Inc 4 Yada...
2
by: gary.goodwin | last post by:
HI I am trying to understand Attribute usage. For example the class SerializableAttribute is a class correct? So why when it is actually u sed the "Attribute" portion of the name is dropped. The...
2
by: rds80 | last post by:
In the xml document below, I would like to retrieve the distinct attributes for the element '<Bal>'. However, I haven't had any success. Here is what I have so far: <TRANS> <TRAN...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.