Dymond
Dymond

Reputation: 2277

count word in a XML with XSLT

I want to count a special word in my XML file. I know I should use contain but cant figure ut how to. The word I want it to count and print out is automat. The automatic word is in the nod.

This is some of my XML file

</autoads>
    <ad>
        <type>2</type>
        <name>Mercedes-Benz</name>
        <model>C220 Elegance</model>
        <regyear>1995</regyear>
        <price>209000</price>
        <adtext>1995 Mercedes-Benz C220 Elegance, 4 dörrar, 88.000 km. skinn, klima/automatic, cruise, el.spegel/fönster, alu.fälgar, c.lås, airbag, antispinn,  ABS, ute temp, radio, s/v-hjul, servo, creme skinn. automat. Pris 209.000 kr,-.  </adtext>
        <addate>20020118</addate>
        <volume>0</volume>
        <category>4 dörrar</category>
    </ad>
<ad>
        <type>2</type>
        <name>Audi</name>
        <model>S8</model>
        <regyear>2000</regyear>
        <price>850000</price>
        <adtext>2000 Audi S8, 4 dörrar, 40.000 km. 4x4, Tiptronic-aut., klimataut., ABS, el.fönster/speglar/säten, soltak, c.lås, servo, airbag, startspärr, antispinn, cruise., alu., träinred., sort skinn, mitttarmstöd., sportssäten, stereo, alarm, s/v-hjul, dragkrok, 17"+18"alu. Pris 850.000 kr,-.  </adtext>
        <addate>20020118</addate>
        <volume>0</volume>
        <category>4 dörrar</category>
    </ad>
</autoads>

So the output in this should be 1 automatic car :) Thanks.

Upvotes: 1

Views: 160

Answers (1)

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56182

Use:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
      <xsl:value-of select="count(//*[contains(text(), 'automat')])"/>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 2

Related Questions