|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: XML to HTML: How to make decisions based on what h
Hi Constantin,
At 05:58 PM 10/29/2003, you wrote: I am creating the stylesheet for displaying info from fairly large XML files (1 to 2 MB). The output is HTML and I'm using the http://www.w3.org/1999/XSL/Transform namespace. I have to alternate background colors of every row in a table. The FAQ list has a straightforward answer that I've used: Ah, but alas, it isn't immaterial -- or I shouldn't say that: *why* you're only selecting some elements is immaterial, but *that* you are selecting only some, and which ones you are selecting, is very material. You've found the position() test doesn't work since having selected a superset of the elements you want, the positions of the ones you want within that set (ordered by the selection) are all wrong. The solution is either not to use position() for your test (there are often other ways to do it), or to make the selection in such a way that you're only selecting the nodes you want, not selecting a bunch and then throwing some away. Let me show you what I mean: source XML: <set> <element status="okay">Whee</element> <element status="okay">Hoo</element> <element status="not okay">Boo!</element> <element status="okay">Yip</element> <element status="okay">Zing</element> </set> XSLT: <xsl:template match="set"> <xsl:apply-templates/> </xsl:template> <xsl:template match="element">
<xsl:if test="@status = 'okay'">
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
<xsl:value-of select="position()"/>
</xsl:if>
</xsl:template>... run that and you'll get positions 1, 2, 4, 5. (Unless you fail to strip whitespace from the <set> element so the whitespace-only text nodes aren't selected, in which case you'll probably get 2, 4, 8, 10 -- which is also wrong.) -- but -- <xsl:template match="set"> <xsl:apply-templates select="element[@status='okay']"/> <xsl:text>
</xsl:text> </xsl:template> <xsl:template match="element"> <xsl:value-of select="."/> <xsl:text> </xsl:text> <xsl:value-of select="position()"/> <xsl:text>
</xsl:text> </xsl:template> The positions come out right, since only the nodes you actually wanted were selected. I know your case will be more complex than this, but it illustrates the general idea. My idea is that if I can store somehow the value of the previous background, I can look at it and select the next one. I looked at xsl:variable, but you can't change (or reassign) that value. I'd like to know if this can be done at all and if so, how? If it can't be done, the source XML files will have to be changed. It can't be done this way -- XSLT does not depend on, and does not use, the state of its own internal processing to control what it does. Rather, it rewards a "synchronous" view of all-the-document-at-once. Assume any processing of any nodes can occur in any order, and you'll be closer to the truth. (And bizarre as it may seem, this is actually a good thing.) On the other hand, you may be too quick to conclude that if it can't be done by switching a flag back and forth in your processor, it can't be done at all -- witness the example above. So it may not be necessary to change your XML source. Even in the worst cases, it's always possible to work in two passes: one pass filters the data, the second one adds the colors. Also, I have to display in the HTML document the name - only once - of an element who has at least one descendant satisfying certain conditions. The way I solved that was to use xsl:for-each on the descendants, and select the name of the element only on the first descendant satisfying the condition: Again, it can't be done this way -- but it's usually possible to develop much more robust ways of checking and de-duplicating than the above. One of the most elegant ways of de-duplicating, usually found in grouping algorithms (which present the same set of problems), is to use keys. You might want to look into resources on groupring in XSLT to learn more about this. You will find Google to be a big help, since this is a well-documented problem area. And see, as always, www.jenitennison.com on XSLT grouping. Cheers, Wendell
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
|

Cart








