[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
Re: [xsl] Tricky one
Subject: Re: [xsl] Tricky one From: "Thomas B. Passin" <tpassin@xxxxxxxxxxxx> Date: Thu, 22 Aug 2002 10:09:03 -0400 |
The simplest way to detect and do something with FRED: <xsl:template match='abc'> <xsl:apply-templates select='def'/> </xsl:template> <xsl:template match='def[ghi="FRED"]'> <data>Found FRED</data> </xsl:template> <xsl:template match='def'> <data>No FRED</data> </xsl:template> To get the most out of xslt, remember that it finds parts of a tree and produces new parts of a tree from them. The tree parts are found by patterns. So you identity a pattern - usually by the select attribute in the xsl:apply-templates element or a match attribute in a template - and apply a different template for each different pattern you are interested in. More specific patterns take priority over more general ones, or you can set the priority yourself (you usually do not have to, though). Cheers, Tom P [Curtis Burisch] > > (apologies if you're getting this twice, I think the first one didn't get > through. If you've replied to this already please send me a copy? having > problems with the list.) > > I'm faced with a problem that for the life of me I can't figure out how to > solve. > > Scenario: > > XML: > > <root> > <abc> > <def> > <ghi>FRED</ghi> > <ghival>23</ghival> > </def> > <def> > <ghi>ANDY</ghi> > <ghival>96</ghival> > </def> > </abc> > </root> > > XSLT: > > (pseudo-code) > if root/abc/def/ghi = "FRED" then > if root/abc/def/ghival != "" then > output root/abc/def/ghival > else > output "0" > else > output "0" > > The inner IF is not a problem. Where the problem strikes is if the FRED > element does not exist. My current implementation (which doesn't work > properly, doesn't take account of this circumstance) goes like this: > > <xsl:template match="abc"> > <xsl:for-each select="def"> > <xsl:if test="ghi='FRED'"> > <xsl:call-template name="OutputNotNull"> > <xsl:with-param name="value" > select="def"/> > </xsl:call-template> > </xsl:if> > </xsl:for-each> > </xsl:template> > > (for clarity and brevity I've omitted the top-level templates and > OutputNotNull which simply outputs the value parameter, or 0 if it's empty. > Note also that I'm using strip-space elements="*" and output method="text" > indent="no") > > I can't use an xsl:choose in place of the xsl:if because I need to examine > all of the elements in order to determine that FRED definitely does not > exist before writing out a 0. > > FRED will exist once, and only once. Or not at all. > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Tricky one, Américo Albuquerque | Thread | Re: [xsl] Tricky one, Kurt George Gjerde |
Re: [xsl] generating formatted text, David Carlisle | Date | Re: [xsl] document() missing the do, Thomas B. Passin |
Month |
Keywords