tags:

views:

226

answers:

3

Hi, My question is: using XSLT, how to count the total number of QUOTE tag (please see the sample code below) The result need to be exported in HTLM, it will display like this: There are total 6 quotes

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="text.xsl" ?>

    <quotes>
      <quote>Quote 1 </quote>
      <quote>Quote 2</quote>
      <quote>Quote 3</quote>
      <quote>Quote 4</quote>
      <quote>Quote 5</quote>
      <quote>Quote 6</quote>
    </quotes>

I already try this XSLT code but it does not work:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xs"
      version="2.0">
    <xsl:template match="/">
     <xsl:value-of select="count(//quote)"></xsl:value-of>
    </xsl:template>

</xsl:stylesheet>

Would you please help me through this problem? Thank you

A: 

it seems to work fine in Firefox 3, and IE6. more information about your setup?

Andy Hohorst
+3  A: 

Your XPath expression, though not very efficient, produces the right result.

When the transformation is run with Saxon 9.1.0.5J, the result is:

<?xml version="1.0" encoding="UTF-8"?>6

The problem seems to be that this is an XSLT 2.0 transformation (which it doesn't need to be!), and you seem to be trying to run it within a browser. Unfortunately, today's browsers do not support (yet) XSLT 2.0.

The solution is to simply change the version to 1.0.

You also don't need the XML Schema namespace for this transformation.

Finally, if the structure of the provided XML document is not going to change, a more efficient XPath expression (because using the // abbreviation causes the whole (sub)tree starting at the top element node to be scanned), will be the following:

count(/*/quote)

Putting all these together we get the following transformation:

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

    <xsl:template match="/">
     <xsl:value-of select="count(/*/quote)"/>
    </xsl:template>
</xsl:stylesheet>

and it produces the wanted result.

Dimitre Novatchev
OK, It works, thanks a lot
Nguyen Quoc Hung
A: 

Ok, it works! Thanks a lot

Nguyen Quoc Hung