Counting examples
Here should go questions about transforming XML with XSLT and FOP.
-
- Posts: 80
- Joined: Wed Jan 14, 2009 12:50 pm
Counting examples
Is there a straightforward way function to count the number of times a particular character or sequence of characters appears in the text of an element? For instance in
I would like to be able to count how many times the letter 'i' appears in element <a> (which is, of course, 3 times), whether there are the same number of '(' characters as ')' in <a>, and the like.
Code: Select all
<a>This (i.e. <b>an example</b>) is an example.</a>
-
- Site Admin
- Posts: 2095
- Joined: Thu Jan 09, 2003 2:58 pm
Re: Counting examples
Hi,
For checking characters you can use the translate function to remove a character from the text and then you can look at the length of the text before and after that, for example:
If you want to check a sequence of characters then it gets difficult with XSLT/XPath 1.0 but it should be easy with XSLT/XPath 2.0 where you have a number of regexp functions like matches, replace or tokenize.
Best Regards,
George
For checking characters you can use the translate function to remove a character from the text and then you can look at the length of the text before and after that, for example:
Code: Select all
<xsl:template match="a">
<xsl:value-of select="string-length(.) - string-length(translate(., 'i', ''))"/>
</xsl:template>
Best Regards,
George
George Cristian Bina
-
- Posts: 80
- Joined: Wed Jan 14, 2009 12:50 pm
Re: Counting examples
Thanks, that will do what I have in mind.
Can I modify this so that the contents of <b> are ignored when processing <a>?
In other words, if I have
I'd like the test here to come out false:
differently from when I test this:
Can I modify this so that the contents of <b> are ignored when processing <a>?
In other words, if I have
Code: Select all
<a>This (i.e. <b>an example)</b> is an example.</a>
Code: Select all
<xsl:template match="a">
<xsl:if test="string-length(translate(.,'(',''))!=string-length(translate(.,')',''))"> ... </xsl:if>
</xsl:template>
Code: Select all
<a>This (i.e. <b>an example</b>) is an example.</a>
-
- Site Admin
- Posts: 2095
- Joined: Thu Jan 09, 2003 2:58 pm
Re: Counting examples
You can select only the text nodes from the element "a" with something like
then use this variable instead of "." in the following expression.
Best Regards,
George
Code: Select all
<xsl:variable name="aText">
<xsl:value-of select="text()" separator="''"/>
</xsl:variable>
Best Regards,
George
George Cristian Bina
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ Artificial Intelligence (AI Positron Assistant add-on)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service