Hi Dipesh,
If I understand correctly, you want first to output the text data of a set
of nodes, consisting of one node each for each of the values of
COLUMN/@NAME in your document. That's what this line in your output is:
Agent Name,Install Path,Product Version,Build Number
Then after that, you want to output a line for every ROW element in your
input, which, for every @NAME value in your set of unique names, contains
the value of the COLUMN in that row with the same @NAME. If none exists,
you want an empty string. All the values, in every case, are separated by
commas.
Right? (This kind of statement of the problem is important because it
expresses the output we want in terms of the input we have: the first step
towards writing a declarative program to get it.)
Given the problem statement, it's interesting that you wanted to do "two
scans", since we have the problem of aggregating the set of @NAME values
that can occur in the document. There are several ways to do this,
including using two passes or a function to create a node set (which can in
effect give us two passes within a single run of a stylesheet); a method
closer to brute force also works, however:
<xsl:variable name="name-set" select="//@NAME[not(.=preceding::@NAME)"/>
This variable assignment basically collects all the @NAME attributes in the
document, and keeps only those whose value is not represented among any of
its preceding @NAME attributes -- in other words, a de-duplicated set.
Given this, the task is much easier. Our root template simply iterates over
this set and writes all the names, with a comma after each one but the
last. Then when it has done that, it applies templates to all the rows in
the document to process them in turn:
<xsl:template match="/">
<xsl:for-each select="$name-set">
<xsl:value-of select="."/>
<xsl:if test="not(position()=last())">,</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text><!-- line feed for legibility -->
<xsl:apply-templates select="//ROW"/>
<!-- this apply-templates could be written to be more efficient,
but I'm too lazy -->
</xsl:template>
Now all we need is a template to process our ROW elements. It runs over the
same set of unique values, only this time giving us the value of the
corresponding COLUMN in the row:
<xsl:template match="ROW">
<!-- we bind our context node to a variable so we can get
to it inside the for-each -->
<xsl:variable name="this-row" select="."/>
<!-- Now we put out whatever value we have for each node in our name set -->
<xsl:for-each select="$name-set">
<xsl:value-of select="$this-row/COLUMN[(_at_)NAME=current()]"/>
<xsl:if test="not(position()=last())">,</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text><!-- line feed for legibility -->
</xsl:template>
There are a couple of things you need to understand fully in order to see
why this works, mainly XPath things ... ask again if they aren't clear.
BTW, this isn't tested, but aside from possible typos it should work....
I hope that helps,
Wendell
At 01:59 PM 8/15/2003, you wrote:
As all of you gurus are saying that it is possible, then it must be. May be i
am not able to find the proper solution for it.
As michael said I want exactly that. Header descrbing the column name and
data
for that column names will follow. But there can be situation like same table
will have m columns at one place and n columns at another place in the input.
Below is the simulated input xml file.
====================================
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Tables>
<TABLE NAME="Client_Agent">
<ROW>
<COLUMN NAME="Agent Name">eXpress NS Client</COLUMN>
<COLUMN NAME="Product Version">5.5.0.517</COLUMN>
<COLUMN NAME="Build Number">517</COLUMN>
</ROW>
<ROW>
<COLUMN NAME="Agent Name">eXpress Inventory Solution</COLUMN>
<COLUMN NAME="Product Version">5.5.0.424</COLUMN>
<COLUMN NAME="Build Number">424</COLUMN>
</ROW>
</TABLE>
<TABLE NAME="Client_Agent">
<ROW>
<COLUMN NAME="Agent Name">eXpress NS Client</COLUMN>
<COLUMN NAME="Install Path">C:\Program Files\ABC\eXpress\NS Client\Software
Delivery\Software Packages\{01B54EB5-3679-4C73-9E10-E169D5EA8C59}</COLUMN>
<COLUMN NAME="Product Version">5.5.0.519</COLUMN>
<COLUMN NAME="Build Number">519</COLUMN>
</ROW>
<ROW>
<COLUMN NAME="Agent Name">eXpress Inventory Solution</COLUMN>
<COLUMN NAME="Install Path">C:\Program Files\ABC\eXpress\NS Client\Software
Delivery\Software Packages\{01B54EB5-4579-4C73-9E10-E169D5DA9E59}</COLUMN>
<COLUMN NAME="Product Version">5.5.0.428</COLUMN>
<COLUMN NAME="Build Number">428</COLUMN>
</ROW>
</TABLE>
</Tables>
</Root>
================================================================
And i m trying to get output something like this
Agent Name,Install Path,Product Version,Build Number
eXpress NS Client,,5.5.0.517,517
eXpress Inventory Solution,,5.5.0.424,424
eXpress NS Client,C:\Program Files\ABC\eXpress\NS Client\Software
Delivery\Software
Packages\{01B54EB5-3679-4C73-9E10-E169D5EA8C59},5.5.0.519,519
eXpress Inventory Solution,C:\Program Files\ABC\eXpress\NS Client\Software
Delivery\Software
Packages\{01B54EB5-4579-4C73-9E10-E169D5DA9E59},5.5.0.428,428
====================================================================
As of now i have shown that there is only one table and columns within it.
There are other tables also but to make things simple i have shown only one
table withn tables tag. I was not able to produce the desired output.
I hope with these as input and output there should be some way to get the
above mentioned output.
Thanks a lot to all you gurus for taking out your time to read my problem.
Eagerly waiting for reply.
Regards,
Dipesh
PS: Resending this email since from yesterday i have not recieved any digest.
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
======================================================================
Wendell Piez
mailto:wapiez(_at_)mulberrytech(_dot_)com
Mulberry Technologies, Inc. http://www.mulberrytech.com
17 West Jefferson Street Direct Phone: 301/315-9635
Suite 207 Phone: 301/315-9631
Rockville, MD 20850 Fax: 301/315-8285
----------------------------------------------------------------------
Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list