Subject: RE: String-length From: Kay Michael <Michael.Kay@xxxxxxx> Date: Tue, 14 Dec 1999 10:21:37 -0000 |
> For information I'm using LotusXSL and XML4J. > Actually I did not need any namespace. > This worked for me: Well it shouldn't work. The variable $lengths is a result tree fragment containing a set of text nodes; the sum() function requires a node-set; the XPath standard states (a) an argument that is not of type node-set cannot be converted to a node-set, (b) it is an error if an argument cannot be converted to the required type; and XSLT conformance requires that the XSLT processor must signal all errors except where otherwise stated. > > <xsl:template match="P"> > <xsl:variable name="lengths"> > <xsl:apply-templates select="preceding-sibling::P|." > mode="length"/> > </xsl:variable> > <b>This para contains: <xsl:value-of select="string-length()"/> > Total from para number 1 to <xsl:value-of select="position()"/>: > <xsl:value-of select="sum($lengths)"/></b><br/> > <p><xsl:apply-templates /></p> > </xsl:template> > > <xsl:template match="P" mode="length"> > <xsl:value-of select="string-length()"/> > </xsl:template> > > Thanks for quick response. > > __ Thomas Karlsen | System Developer > __ Cell Network ASA | New Media Science | Sorkedalsveien 10A, > N-0304 Oslo, > Norway > Tel: +47 23196600 | Fax: +47 23196601 | Mob: +47 91514293 > > > > -----Original Message----- > > From: Phil Lanch [mailto:phil@xxxxxxxxxxxxxxx] > > Sent: 13. desember 1999 20:09 > > To: xsl-list@xxxxxxxxxxxxxxxx > > Subject: Re: String-length > > > > > > Alternatively, use a named template to collect (nodes > containing) the > > numbers you want to add up in a variable, and apply the > sum() function > > to them. The tricky bit is that the variable contains a result-tree > > fragment, not a node list, and the sum() function must be given a > > node-list, so you need to convert the result-tree fragment to a > > node-list using a node-set() extension function. This is > available in > > Saxon or XT (and it looks such a good idea that surely all good XSL > > processors will have it eventually). > > > > This kind of use of apply-templates or call-template inside > a variable > > is often an alternative to recursive named templates. > > > > So in Saxon that would be: > > > > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > > xmlns:sxf="/com.icl.saxon.functions.Extensions" version="1.0"> > > > > <xsl:template match="para"> > > <xsl:variable name="lengths"> > > <xsl:apply-templates select="preceding-sibling::para|." > > mode="length"/> > > </xsl:variable> > > This para contains: <xsl:value-of select="string-length()"/> > > Total from para number 1 to <xsl:value-of select="position()"/>: > > <xsl:value-of select="sum(sxf:node-set($lengths)/length)"/> > > </xsl:template> > > > > <xsl:template match="para" mode="length"> > > <length><xsl:value-of select="string-length()"/></length> > > </xsl:template> > > > > and in XT you need to declare the > xmlns:xt="http://www.jclark.com/xt" > > namespace and change sxf:node-set to xt:node-set. > > > > Kay Michael wrote: > > > > > > You need another of those recursive named templates that > > does, in effect > > > strlength($nodeset) = string-length($nodeset[1]) + > > > strlength($nodeset[position()>1]) > > > > > > Of course writing this in XSL is about 15 lines of code. > > There are many > > > examples of this pattern in the archives of this list. > > > > > > Mike Kay > > > > > > > -----Original Message----- > > > > From: Thomas Karlsen [mailto:ThomasK@xxxxxxxxxxxxxxx] > > > > Sent: 13 December 1999 11:22 > > > > To: 'XSL-List@xxxxxxxxxxxxxxxx' > > > > Subject: String-length > > > > > > > > > > > > Im trying to hold track over how many char. i'm sending > > to the output. > > > > > > > > xml: > > > > <section> > > > > <para>this is a test</para> > > > > <para>this is a test</para> > > > > <para>this is a test</para> > > > > <para>this is a test</para> > > > > </section> > > > > > > > > xsl: > > > > <template match="section"> > > > > <apply-templates /> > > > > </template> > > > > > > > > <template match="para"> > > > > This para contains: <xsl:value-of select="string-length()"/> > > > > Total from para number 1 to <xsl:value-of > select="position()"/>: > > > > <xsl:value-of select="string-length(preceding-sibling::para)"/> > > > > <apply-templates /> > > > > </template> > > > > > > > > My question is how can I add the string-values for the > > para's that is > > > > already traversed? > > > > The preceding-sibling function only gives me the value of the > > > > first para in > > > > the node-set. > > > > -- > > > > cheers > > > > phil > > > > "that monotonous state of the soul halfway between fulfillment > > and futility which comes with life in the country" --- Musil > > > > > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: String-length, Phil Lanch | Thread | Unparsed Entity and XSL, Bovone Stefano |
RE: dynamically reading tag-names i, Kay Michael | Date | Re: what are node set fragments and, David Carlisle |
Month |