xsl-list
[Top] [All Lists]

Re: Can't get output from xml -> text transformation

2004-08-13 11:38:24
It seems the second appl-template tag in the second template rule was the trick. Thanks so much. BTW, you were right about this being the beginning of what I needed - but I couldn't move on untill I got this piece done. Now I need to get the formatting the way I want and then figure out how to get the game name as well. The game id info is all collected in the one file in the attribute, so that's easy. For the game name I'll have to go to a {gameid}.xml file and find it in there. Iterating through all those may be a bit more that a simple xsl can handle, but i'm happy to be moving on this again.


Thanks,
Todd Alexander
heeznow(_at_)msn(_dot_)com





From: "M. David Peterson" <m(_dot_)david(_at_)mdptws(_dot_)com>
Reply-To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Can't get output from xml -> text transformation
Date: Wed, 11 Aug 2004 12:25:06 -0600

Hi Todd,

Understanding what nodes are being processed and matched to a template will help in your understanding of whats happening. <xsl:apply-templates /> is the same thing as <xsl:apply-templates select="*"/> which will select all the children of the current context (root) and seek out a template to match them to (in your case there are no matching templates to the children of context - one node by the name of "allGamesDownloadable" - and as such the "value" of each child node is simply sent to the output "as is" until there are no more children left to process. The "blank" lines you are seeing are most likely the newline characters that exist as child "text" nodes of "allGamesDownloadable" in your source XML.

It seems that what you are expecting to happen is for every node (every child of a child of a child etc...) to be processed according to document order and when there is not matching template it will simply move to the next child or grandchild until it finds a match. While this is in no way an uncommon mistake - in fact its a VERY common mistake - it is fundamentally flawed from an XSLT template matching perspective and as such will not provide you the results you are looking for. If you try to think of it from the perspective that to gain access to the children of the current context node you must begin the recursive nature of apply-templates when the desired children are in context you will find yourself catching your errors quite easily... for example, the following will select the children of the root node, find the best matching template, process it accordingly, and then begin the recursive process on the children of the current context node ("allGamesDownloadable") by once again calling apply-templates at a time when the children are in context. Because we are using "*" for our match value the same template will match the children currently being processed which means any children of these children will be recursively processed and matched to the same template and this process will continue until every node in the source document has gone through "the ringer" so to speak...

so this....

<?xml version="1.0"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="text" />

<xsl:template match="/">
<xsl:text>
GAMEID,GAMENAME
</xsl:text>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="*">
<xsl:value-of select="@gameId"/>,
<xsl:apply-templates/>
</xsl:template>

Will output the value of @gameId for every node that is processed by this template that has an attribute gameId.

While this particular case will not give you the CSV output you are in desire of this should give you just enough information to help move you in the right direction...

Best of luck to you!

<M:D/>





<Prev in Thread] Current Thread [Next in Thread>