Re: [xsl] counting characters in an XML document

Subject: Re: [xsl] counting characters in an XML document
From: Mike Brown <mike@xxxxxxxx>
Date: Wed, 24 Oct 2001 13:59:54 -0600 (MDT)
Tom Melkonian wrote:
> Does anyone know how I can use XSL to count the number of characters in an
> XML document. I want to process the XML so that, if there are less than a
> certain amount of characters, a certain message gets printed. Another option
> could be counting number of tags (elements)

XSLT only operates on parsed documents, so you can't, without extension
functions, count characters in an unparsed document.

Parsing means decoding the bytes (if necessary) to Unicode characters and
then decoding the characters ("tags" and everything in between) to the
logical structures they imply: elements, attributes, character data,
processing instructions and sometimes comments, and making these available
to an application such as an XSLT processor. 

The XSLT processor will interpret the structures as a logical tree of
nodes according to the XPath data model (see the XPath spec). From within
XSLT you have access to these nodes.

So you can easily count element nodes:
<xsl:value-of select="count(//*)"/>

Or all nodes:
<xsl:value-of select="count(//node())"/>

But guessing at the original representation of the document as a string of
characters, when you all have to go on is the node tree, is impossible to 
do with any accuracy.

   - Mike
____________________________________________________________________________
  mike j. brown, fourthought.com  |  xml/xslt: http://skew.org/xml/
  denver/boulder, colorado, usa   |  personal: http://hyperreal.org/~mike/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread