473,569 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3331

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:styleshe et 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="generat e-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('ro w-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:otherwis e>
<xsl:attribut e
name="{local-name()}"
namespace="{nam espace-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
3642
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. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
4
6105
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 2000. Some of the columns (for example "category") have duplicate values throughout the records. We have a web page that queries the table to show...
8
7414
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 ON dbo.CIF_Departures.EndPoint = dbo.CIF_PlaceReference.PlaceID ORDER BY dbo.CIF_PlaceReference.Name This results in a column of placenames...
6
2472
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 AttributeError
1
2469
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 only way to find out the current state of a particular combination of attributes is to "select distinct on (id, ...) ... order by date desc". In...
4
5287
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 distinct values of Stationery. I've found some things on the web that suggest XPath statements to do this (in XSLT),
3
5642
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 Inc
2
1719
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 sam is true of the AttributeUsageAttribute class and on and on. I don't get it. Gary
2
4312
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 TRAN_DESC_CD="ACRT" TRAN_DESC="Actual Rate">
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7677
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.