[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
RE: [xsl] [XSL] Counting Child Nodes?
Subject: RE: [xsl] [XSL] Counting Child Nodes? From: "Scott Trenda" <Scott.Trenda@xxxxxxxx> Date: Fri, 19 Oct 2007 10:01:51 -0500 |
A few things here, Alice: - It seems you have your if's and when's confused here. This sequence: <xsl:choose> <xsl:when test="someTest">someResult</xsl:when> <xsl:otherwise/> </xsl:choose> is the exact same as the following: <xsl:if test="someTest">someResult</xsl:if> Right beneath that, though, you have a series of three <xsl:if/> statements that create an attribute "style", of which two could possible be true at a time. This sounds like a prime case for using <xsl:when/>. - ./@rendition is the exact same as @rendition. Since you're not switching the context node in your XPath tests involving $rendition, it looks like the variable $rendition isn't needed either. (This might change in your final stylesheet, but keep it simple until then!) - "child::text()" is the same as "text()", which is the same as "." in functions that evaluate the value of their arguments. And string-length() doesn't even need an argument, if you want it to default to the value of the context node. ... Looking at it, though, since there are child _elements_ in the context node, you'd be better off sticking with text() for consistency. - It seems you forgot a parenthesis in your second condition (also, numbers are a primitive datatype in XPath, so don't quote them): string-length('25 ' < child::text() < '50') Two inequality tests in a row doesn't work in XPath like it does in math; < outputs a boolean, so you're testing if the boolean result is less than 50. Always true. With that, here's my rendition of the fixed stylesheet: <xsl:template match="book"> <div> <p> <xsl:if test="@rendition != ''"> <div class="{@rendition}"> <xsl:attribute name="style"> <xsl:variable name="bg-size"> <xsl:choose> <xsl:when test="count(p) > 1">_large</xsl:when> <xsl:when test="string-length(text()) < 25"/> <xsl:when test="string-length(text()) < 50)">_med</xsl:when> </xsl:choose> </xsl:variable> <xsl:value-of select="concat('background-image:url('', @rendition, $bg-size, '.png')'"/> </xsl:attribute> <p> <xsl:call-template name="rend"/> <xsl:apply-templates/> </p> </div> </xsl:if> </p> </div> </xsl:template> Give it a spin, see how it works. ~ Scott -----Original Message----- From: Alice Ju-Hsuan Wei [mailto:ajwei@xxxxxxxxxxx] Sent: Friday, October 19, 2007 9:12 AM To: xsl-list Subject: [xsl] [XSL] Counting Child Nodes? Hi, I am making attempts to make the output of the transformed XML to be able to display different images based on the nodes and string length. Below is the XSLT: XSLT: <xsl:template match="book"> <xsl:variable name="rendition"> <xsl:value-of select="./@rendition"/> </xsl:variable> <div> <p> <xsl:choose> <xsl:when test="$rendition!=''"> <div> <xsl:attribute name="class"> <xsl:value-of select="$rendition"/> </xsl:attribute> <xsl:if test="string-length(child::text())<'25'"> <xsl:attribute name="style"> background-image:url('<xsl:value-of select="./@rendition"/>.png') </xsl:attribute> </xsl:if> <xsl:if test="string-length('25 ' < child::text() < '50')"> <xsl:attribute name="style"> background-image:url('<xsl:value-of select="./@rendition"/>_med.png') </xsl:attribute> </xsl:if> <xsl:if test="count(child::p) > 1"> <xsl:attribute name="style"> background-image:url('<xsl:value-of select="./@rendition"/>_large.png') </xsl:attribute> </xsl:if> <p> <xsl:call-template name="rend"/> <xsl:apply-templates/> </p> </div> </xsl:when> <xsl:otherwise> <!--Don't display anything--> </xsl:otherwise> </xsl:choose> </p> </div> </xsl:template> XML: <book topic="Technology" rendition="xslt" author="Tennison, Jeni" title="Beginning XSLT"> <p>New York: Apress, 2002.</p> <p>The book serves as an introduction to XSLT based on the examples of a television program set. A resourceful book for those new to the technology.</p> </book> I want to use the code displayed above (XML) to display the file name with the name of xslt_large, and now in the HTML source it does not display that, even though the code above has two <p> tags. Can anyone please help me out? Anything is appreciated. Alice
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] [XSL] Counting Child Nodes?, Alice Ju-Hsuan Wei | Thread | RE: [xsl] [XSL] Counting Child Node, Michael Kay |
Re: [xsl] Enclosing sequence items , cknell | Date | RE: [xsl] [XSL] Counting Child Node, Michael Kay |
Month |