Re: [xsl] Length of a literal string containing embedded tags

Subject: Re: [xsl] Length of a literal string containing embedded tags
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Wed, 01 Aug 2007 15:21:14 +0200
Abel Braaksma wrote:

1. if you do not care about whitespace, CDATA and empty tags like in MK's example above, you can use the information from the infoset to calculate the length of the node you are having by using name() and attribute() methods (and perhaps namespace methods) and some string-length addition.



That would be (for the fun of it), something alongside the following (assuming the context node must be counted):


sum((
descendant-or-self::text()/(string-length()),
descendant-or-self::element()/(string-length(name()), string-length(name()), 5),
descendant-or-self::element()/attribute::*/(string-length(name()), string-length(), 4),
descendant-or-self::processing-instruction()/(string-length(name()), string-length(), 4),
descendant-or-self::comment()/(string-length(), 7)
))


And yes, the double parentheses around sum() are on purpose ;)

Note that the last one in each selection is the minimal number of extra characters that are used in literal XML, i.e., for an attribute it requires two quotes, one whitespace (at least) and one equal sign, totaling 4.

Note also that this XPath expression sees all elements as <elem>data</elem>, all texts as normal texts (no cdata, each entity as one), all whitespace as relevant and cannot count markup-whitespace etc.

Cheers,
-- Abel Braaksma

Current Thread