This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: counting characters in an XML document


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]