W3C home > Mailing lists > Public > www-xml-linking-comments@w3.org > July to September 2003

[XSLTalk] How to get every attributes for a node ?

From: Dipesh Khakhkhar <dkhakhkh@mailbox.syr.edu>
Date: Wed, 3 Sep 2003 19:10:18 -0400
To: XSLTalk@yahoogroups.com
Message-ID: <3F581859@OrangeMail>

Hi,

I am trying to solve one problem. I am presenting the mock example of my input 
and output. I have emaile earlier with somewhat lengthy example.

XML
===

<?xml version="1.0" encoding="UTF-8"?>
<Root Att1="val1" Att2="val2">
<Node1 Node1Att="Nval1" Node2Att="Nval2">
<CLASS Table="Firsttable">
<Row>
<Column ColName="ValOfColName1">Colvalue1</Column>
<Column ColName="ValOfColName2">Colvalue2</Column>
<Column ColName="ValOfColName3">Colvalue3</Column>
</Row>
<Row>
<Column ColName="ValOfColName2">Colvalue2</Column>
<Column ColName="ValOfColName4">Colvalue4</Column>
</Row>
</CLASS>
</Node1>
<Node1 Node1Att="val1" Node2Att="val2">
<CLASS Table="Firsttable">
<Row>
<Column ColName="ValOfColName1">Colvalue1</Column>
<Column ColName="ValOfColName2">Colvalue2</Column>
<Column ColName="ValOfColName3">Colvalue3</Column>
<Column ColName="ValOfColName5">Colvalue5</Column>
</Row>
<Row>
<Column ColName="ValOfColName4">Colvalue4</Column>
<Column ColName="ValOfColName6">Colvalue6</Column>
</Row>
</CLASS>
</Node1>
</Root>

---------------------------------------------------------------------

I need to have ouput like this.First I ouput column name as column header and 
then I have to get the data below the column header. I have used "`" as a 
seperator.

Desired Output
--------------

RootID`Att1`Att2`RootID`Node1ID`Node1Att`Node2Att`Node1ID`FirstTableID`ValOfCo
lName1`ValOfColName2`ValOfColName3`ValOfColName4`ValOfColName5`ValOfColName6
GeneratedIDForRoot`val`val2`GeneratedIdForRoot`GeneratedIDForNode1`Nval1`Nval2
`GeneratedIDForNode1`GeneratedFirstTableID`Colvalue1`Colvalue2`Colvalue3```
``````````Colvalue2``Colvalue4``
```GeneratedIdForRoot`GeneratedIDForNode1`Nval1`Nval2`GeneratedIDForNode1`Gene
ratedFirstTableID`Colvalue1`Colvalue2`Colvalue3``Colvalue5`
````````````Colvalue4``Colvalue6

---------------------------------------------------------------------

Current XSL
===========

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0">
<xsl:output method="text"/>
<!-- Getting Root node with maximum attributes -->
<xsl:variable name="mostattForRootNode">
<xsl:for-each select="//Root">
<xsl:sort select="count(attribute::*)" order="descending" data-type="number"/>
<xsl:if test="position()=1">
<xsl:value-of select="generate-id()"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="maxAttRoot-node" select="//node()[generate-id() = 
$mostattForRootNode]"/>
<!-- Getting Node1 node with maximum attributes -->
<xsl:variable name="mostattForNode1">
<xsl:for-each select="//Node1">
<xsl:sort select="count(attribute::*)" order="descending" data-type="number"/>
<xsl:if test="position()=1">
<xsl:value-of select="generate-id()"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="maxAttNode1-node" select="//node()[generate-id() = 
$mostattForNode1]"/>

<!-- Getting FirstTable node with maximum childnodes i.e. having maximum 
columns-->
<xsl:variable name="mostattFirsttable">
<xsl:for-each 
select="//Root/Node1/CLASS[normalize-space(@Table)='Firsttable']/Row">
<xsl:sort select="count(Column)" data-type="number" order="descending"/>
<xsl:if test="position()=1">
<xsl:value-of select="generate-id(.)"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="maxattFirsttable-node" select="//node()[generate-id() = 
$mostattFirsttable]"/>

<!-- Outputting Column Header first and Then Data -->
<xsl:template match="/">
<!-- Printing header for Root -->
<xsl:text>RootID`</xsl:text>
<xsl:for-each select="$maxAttRoot-node/@*">
<xsl:value-of select="name(.)"/>
<xsl:if test="position()!=last()">`</xsl:if>
</xsl:for-each>
<!-- Printing header for Node1 -->
<xsl:text>`RootID`Node1ID`</xsl:text>
<xsl:for-each select="$maxAttNode1-node/@*">
<xsl:value-of select="name(.)"/>
<xsl:if test="position()!=last()">`</xsl:if>
</xsl:for-each>

<!-- Printing header for Firsttable i.e. Class node with attribute="First 
table" -->
<xsl:for-each select="$maxattFirsttable-node">
<xsl:text>`Node1ID`FirstTableID`</xsl:text>
<xsl:for-each select="Column/@ColName">
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">`</xsl:if>
</xsl:for-each>
</xsl:for-each> <xsl:text>&#10;</xsl:text>
<!-- Now putting data data below the Column Headers -->
<xsl:apply-templates select="/Root"/>
</xsl:template>

<xsl:template match="Root">
<xsl:value-of select="generate-id(.)"/><xsl:text>`</xsl:text>
<xsl:variable name="r" select="."/>
<xsl:for-each select="$maxAttRoot-node">
<xsl:for-each select="@*">
<xsl:choose>
<xsl:when test="$r/@*[name()=name(current())]">
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">`</xsl:if>
</xsl:when>
<xsl:otherwise>`</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<!--<xsl:text>&#10;</xsl:text> -->
</xsl:for-each>
<xsl:apply-templates select="Node1"/>
</xsl:template>
<!-- Template for resouce Type -->
<xsl:template match="Node1">`<xsl:value-of 
select="generate-id(..)"/>`<xsl:value-of 
select="generate-id(.)"/><xsl:text>`</xsl:text>
<xsl:variable name="r" select="."/>
<xsl:for-each select="$maxAttNode1-node">
<xsl:for-each select="@*">
<xsl:choose>
<xsl:when test="$r/@*[name()=name(current())]">
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">`</xsl:if>
</xsl:when>
<xsl:otherwise>`</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<!--<xsl:text>&#10;</xsl:text> -->
</xsl:for-each>
<xsl:apply-templates select="CLASS"/>
<!-- I will count the attributes of parent node and dynamically generate the 
seperators -->
<xsl:if test="not (position()=last())">
<xsl:text>
</xsl:text>
<xsl:call-template name="do-quotes">
<xsl:with-param name="count" select="count($maxAttRoot-node/@*) "/>
</xsl:call-template>
</xsl:if>
</xsl:template>

<!--Temaplate for Class -->
<xsl:template match="CLASS">
<xsl:choose>
<!-- When Table is Firsttable -->
<xsl:when test="normalize-space(@Table)='Firsttable'">`<xsl:for-each 
select="Row"><xsl:variable name="r" select="."/><xsl:value-of 
select="generate-id(../..)"/>`<xsl:value-of 
select="generate-id(.)"/><xsl:text>`</xsl:text>
<xsl:for-each select="$maxattFirsttable-node/Column">
<xsl:value-of 
select="normalize-space($r/Column[@ColName=current()/@ColName])"/>
<xsl:if test="position()!=last()">`</xsl:if>
</xsl:for-each>
<xsl:if test="not (position()=last())">
<xsl:text>
</xsl:text>
<xsl:call-template name="do-quotes">
<xsl:with-param name="count" select="count($maxAttRoot-node/@*) + 
count($maxAttNode1-node/@*) + 3"/>
</xsl:call-template>
</xsl:if>
</xsl:for-each>
</xsl:when>
</xsl:choose>
</xsl:template>
<!-- RECURSIVE TEMPLATE TO OUTPUT DELIMTER i.e " `" -->
<xsl:template name="do-quotes">
<xsl:param name="count" select="0"/>
<xsl:if test="$count">
<xsl:text>`</xsl:text>
<xsl:call-template name="do-quotes">
<xsl:with-param name="count" select="$count - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

--------------------------------------------------------------------


In the above xsl I am findng first the Class node with name="Firsttable" and 
finding a row node with maximum "column" nodes. If i follow this approach I 
miss few columns which are not trapped when i found a node (Row) having 
maximum column nodes.
But I need the header which can give me all the column names.

How do i get all the nodes and then correspondingly get the data ?

Thanks in the anticipation that this time i will get some suggestion how to 
tackle this problem. My earlier email qouted the same problem but it was very 
lengthy. This is comparatively simple and not that lengthy.

Eagerly waiting for reply.

Regards,
Dipesh


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for Your HP, Epson, Canon or Lexmark
Printer at Myinks.com. Free s/h on orders $50 or more to the US & Canada. http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/l.m7sD/LIdGAA/qnsNAA/2U_rlB/TM
---------------------------------------------------------------------~->

This discussion list is hosted at TopXML (topxml.com).  If you like what we're doing, please show your support by posting your small code snippets there!  Also, post your tech announcements and news on our new site, Markup World (markupworld.com)

To unsubscribe from this group, send an email to:
XSLTalk-unsubscribe@egroups.com
 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 
Received on Wednesday, 3 September 2003 19:13:18 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 20:32:24 UTC