www.webdeveloper.com
Results 1 to 5 of 5

Thread: How to count total number of quotes

  1. #1
    Join Date
    Mar 2009
    Posts
    14

    How to count total number of quotes

    For example if I have about 5 quotes like this:
    <quotes>
    <quote>
    <!--quote 1-->
    </quote>
    <quote>
    <!--quote 2-->
    </quote>
    <quote>
    <!--quote 3-->
    </quote>
    <quote>
    <!--quote 4-->
    </quote>
    <quote>
    <!--quote 5-->
    </quote>
    <quotes>

    How to count total number of "quote" tag inside the root "quotes" tag?
    I wanna the HTML output like this: THERE ARE TOTALLY 5 QUOTES

    Would you please help me? Thank you you guys so much

  2. #2
    Join Date
    Mar 2009
    Posts
    96
    You might use XSLT in combination with the XPath count() function:
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <?xml-stylesheet type="text/xsl" href="quotes.xsl" ?>
    <quotes>
      <quote/>
      <quote/>
      <quote/>
      <quote/>
      <quote/>
      <quote/>
    </quotes>
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:transform version="1.0"
               xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
               xmlns="http://www.w3.org/1999/xhtml">
      <!-- Match the root "quotes" node. -->
      <xsl:template match="/quotes">
        <xsl:element name="p">
          <!--
    	  count(quote) matches the total number of quote elements
    	  that are children of the root quotes node.
    
    	  Note that this doesn't include descendants, like:
    	      quotes > quote > somethingElse > quote
    	-->
          There are totally <xsl:value-of select="count(quote)"/> quotes.
        </xsl:element>
      </xsl:template>
    </xsl:transform>
    Last edited by dmboyd; 04-08-2009 at 10:26 PM.

  3. #3
    Join Date
    Mar 2009
    Posts
    14
    Code:
      <xsl:template match="/">
        <html>
          <body>
            <pre>
              TOtal number of quote <xsl:apply-templates select="//quote"/>
            </pre>
          </body>
        </html>
      </xsl:template>
      <xsl:template match="quote">
        <xsl:value-of select="count(quote)"/>
      </xsl:template>
    </xsl:stylesheet>
    This does not work. I write my codes like this and the result:
    TOtal number of quote: 00000

    Would you guys please help me about this problem?

  4. #4
    Join Date
    Feb 2009
    Posts
    33
    Code:
    <?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>
    cheers, John Bampton.

  5. #5
    Join Date
    Mar 2009
    Posts
    14
    I've done with this. Thank you so much for your help

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles