[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] RE: Number of scans required ??
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[@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. ====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|