Search code examples
xpathcountpunctuation

Count specific punctuation in a tag using Xpath


I'm trying to count the amount of colon followed by space within tag "report":

<report>But that doesn't work either: what do you guys think: To be solved</report>

My xpath code

`report[count([text()=": "]) &gt; 1]`

but it not works


Solution

  • To complete the XPath 2.0 solution provided by @Slkrasnodar, here's the XPath 1.0 solution :

    string-length(//report)-string-length(translate(//report,":",""))
    

    We check the length of the original string. Then we generate a string without colon and check its length. Finally, we substract this length from the original length string to get the result.

    Output : 2

    EDIT : Please fix your XML file. It contains unclosed tags. I've worked with :

    <authors>
    <author>
    <gnm>Lee</gnm>
    <snm>Auch</snm>
    </author>
    <report>Search Results: Count the number of words in a xml node using xsl: Thank you</report>
    </authors>
    

    To get the authors names who have a report which contains more than one colon, use the following expression :

    //authors[string-length(//report)-string-length(translate(//report,":",""))>1]//gnm/text()
    

    Output : Lee