[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Need assistance with conditional logic and counter
Jennifer,
I hope you see this, as it's nearly a week old, but I thought a neater solution was possible to your problem, since you actually need neither counters nor recursion. The XSLT xsl:number instruction will suffice for the numbering. In fact the only thing that really makes this tricky (other than how to set up xsl:number, which isn't obvious until you learn it) is the fact that you want to drop elements that appear without content. Another way of handling that problem (not given below) would be to solve this in two passes. That would also give opportunities to make exception handling a bit more graceful (for example, what should happen if no Return Date value is given). But this (single-pass) solution will work also under XSLT 1.0, and solves the problem you presented us with. <xsl:template match="Order"> <div class="order"> <xsl:apply-templates/> </div> </xsl:template> <xsl:template name="entry"> <xsl:param name="label"/> <xsl:if test="normalize-space()"> <!-- don't generate output for elements that have no contents or only whitespace contents --> <p> <xsl:call-template name="number"/> <xsl:value-of select="$label"/> <xsl:text>: </xsl:text> <xsl:apply-templates/> </p> </xsl:if> </xsl:template> <xsl:template name="number"> <xsl:number level="multiple" format="1.1. " count="OrderNumber[normalize-space()] | CustomerID[normalize-space()] | Return[normalize-space()] | ReturnReason[normalize-space()] | Total[normalize-space()]"/> </xsl:template> <xsl:template match="OrderNumber"> <xsl:call-template name="entry"> <xsl:with-param name="label">Order number</xsl:with-param> </xsl:call-template> </xsl:template> <xsl:template match="CustomerID"> <xsl:call-template name="entry"> <xsl:with-param name="label">Customer ID</xsl:with-param> </xsl:call-template> </xsl:template> <xsl:template match="Return"> <xsl:if test="normalize-space()"> <p> <xsl:call-template name="number"/> <xsl:text>Return Date: </xsl:text> <xsl:apply-templates select="ReturnDate"/> </p> <xsl:apply-templates select="ReturnReason"/> </xsl:if> </xsl:template> <xsl:template match="ReturnReason"> <xsl:call-template name="entry"> <xsl:with-param name="label">Reason</xsl:with-param> </xsl:call-template> </xsl:template> <xsl:template match="Total"> <xsl:call-template name="entry"> <xsl:with-param name="label">Total</xsl:with-param> </xsl:call-template> </xsl:template> Cheers, Wendell On 3/14/2012 8:58 AM, Jennifer Elkhouri wrote: I am very new to XSLT, so thank you in advance for helping me out. My issue is related to wanting to print output conditionally using counters. If an element returns no data, I do not want to print the header for that section (easy enough). However, these headers need to be numbered ... which is where I am having a problem. -- ====================================================================== 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 ======================================================================
|
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
|