string-length excluding child element content

Here should go questions about transforming XML with XSLT and FOP.
shudson310
Posts: 86
Location: USA

string-length excluding child element content

Mon Jan 23, 2017 3:56 am

I'm trying to get the string length of an element, but need to exclude the children elements from the count.

Here is the test source:

Code: Select all

<indexterm>12345678901234567890123456789012
            <index-see>bar
              <indexterm>baz</indexterm>
            </index-see>
         </indexterm>


I'm trying the following test:

Code: Select all

<sch:assert test="string-length(text()[1]) &lt;= 33" role="warning">


the problem is that the descendant element content seems to be included in the count! The above should be valid, but it is getting flagged. How do I exclude the children from the string-length calculation?
Scott Hudson
Content Strategist
Jeppesen
Site: jeppesen.com
tavy
Posts: 123

Re: string-length excluding child element content

Mon Jan 23, 2017 10:03 am

Hello,

The XPath expression that you used in the assertion excludes the children elements, but you need to exclude the whitespaces when you count the string length of the text node. For this you can use the "normalize-space()" function, something like:

Code: Select all

<sch:assert test="string-length(normalize-space(text()[1])) &lt;= 33" role="warning">


Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 1 guest