Word Count in XML - untested


/ Published in: XML
Save to your folder(s)

This is untested, just something I found and wanted to keep


Copy this code and paste it in your HTML
  1. <?xml version="1.0" encoding="iso-8859-1"?><!-- DWXMLSource="<a href="http://feeds.directnews.org.uk/?e56bf4a0-bf37-4b2e-9dc3-45f400c4cbeb"">http://feeds.directnews.org.uk/?e56bf4a0-bf37-4b2e-9dc3-45f400c4cbeb"</a> -->
  2. <!DOCTYPE xsl:stylesheet [
  3. <!ENTITY nbsp "&amp;#160;">
  4. <!ENTITY copy "&amp;#169;">
  5. <!ENTITY reg "&amp;#174;">
  6. <!ENTITY trade "&amp;#8482;">
  7. <!ENTITY mdash "&amp;#8212;">
  8. <!ENTITY ldquo "&amp;#8220;">
  9. <!ENTITY rdquo "&amp;#8221;">
  10. <!ENTITY pound "&amp;#163;">
  11. <!ENTITY yen "&amp;#165;">
  12. <!ENTITY euro "&amp;#8364;">
  13. ]>
  14.  
  15. <xsl:stylesheet version="1.0" xmlns:xsl="<a href="http://www.w3.org/1999/XSL/Transform">">http://www.w3.org/1999/XSL/Transform"></a>
  16. <xsl:output method="html" encoding="iso-8859-1"/>
  17. <xsl:param name="ItemsPerPage" select="2" />
  18. <xsl:template match="/"><link href="../scripts/article10.css" rel="stylesheet" type="text/css" /><style type="text/css">
  19. <xsl:comment>
  20. body {
  21. margin-left: 0px;
  22. margin-top: 0px;
  23. margin-right: 0px;
  24. margin-bottom: 0px;
  25. }
  26. .style1 {font-size: 11px; color: #333333; font-family: Arial, Helvetica, sans-serif;}
  27. </xsl:comment>
  28. </style>
  29.  
  30.  
  31. <p>
  32.  
  33. <table width="100%" border="0" cellpadding="0" cellspacing="0" id="contents">
  34. <tr>
  35. <td width="50%" align="left" valign="top">
  36.  
  37. <xsl:for-each select="InfoStreamResults/Article[position() &amp;lt;= $ItemsPerPage]">
  38. <xsl:sort select="Heading" order="ascending"/>
  39.  
  40. <table width="280" class="tableStandard">
  41. <tr>
  42. <td colspan="2" bgcolor="#DBE1F2" class="newsHeadLine"><xsl:value-of select="Heading"/><br /> <xsl:value-of select="Date"/> </td>
  43. </tr>
  44. <tr class="bodycopy">
  45.  
  46. <td width="83%"><xsl:value-of select="substring-before(Contents, ',')" disable-output-escaping="yes"/> ....</td>
  47. </tr>
  48. <tr class="bodycopy">
  49. <td><a class="welcomebodycopy">
  50. <xsl:attribute name="href"> <xsl:text>fullStory.asp?varID=</xsl:text> <xsl:value-of select="@ID" /> <xsl:text>&amp;amp;varRelated=</xsl:text> <xsl:value-of select="Categories/Category/@ID" /></xsl:attribute>
  51. Read full story </a> <br />
  52. <xsl:value-of select="translate(Heading, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/></td>
  53. </tr>
  54. </table>
  55. <br />
  56. </xsl:for-each> </td>
  57. </tr>
  58. <br />
  59. </table>
  60.  
  61. </p>
  62.  
  63. </xsl:template>
  64. </xsl:stylesheet>
  65.  
  66. /////////////////////////////////////////////////////////////
  67.  
  68. This is the example i've got off the net -
  69.  
  70. ?xml version="1.0" encoding="UTF-8"?>
  71. <xsl:stylesheet
  72. xmlns:xsl="<a href="http://www.w3.org/1999/XSL/Transform"">http://www.w3.org/1999/XSL/Transform"</a>
  73. version="1.0">
  74.  
  75. <xsl:output method="html" indent="yes" />
  76.  
  77. <xsl:template match="/">
  78. <html>
  79. <head>
  80. <title>Example</title>
  81. </head>
  82. <body>
  83. <h1>Headlines</h1>
  84. <xsl:apply-templates select="headlines/headline" />
  85. </body>
  86. </html>
  87. </xsl:template>
  88.  
  89. <xsl:template match="headline">
  90. <h2>
  91. <xsl:call-template name="get-words">
  92. <xsl:with-param name="text" select="." />
  93. <xsl:with-param name="number-of-words" select="3" />
  94. </xsl:call-template>
  95. </h2>
  96. </xsl:template>
  97.  
  98. <xsl:template name="get-words">
  99. <xsl:param name="text" />
  100. <xsl:param name="number-of-words" />
  101. <xsl:param name="processed-words" select="''" />
  102. <xsl:choose>
  103. <xsl:when test="$number-of-words = 0">
  104. <xsl:value-of select="$processed-words" />
  105. </xsl:when>
  106. <xsl:otherwise>
  107. <xsl:call-template name="get-words">
  108. <xsl:with-param name="text"
  109. select="substring-after($text, ' ')" />
  110. <xsl:with-param name="number-of-words"
  111. select="$number-of-words - 1" />
  112. <xsl:with-param name="processed-words"
  113. select="concat($processed-words, ' ', substring-before($text, ' '))" />
  114. </xsl:call-template>
  115. </xsl:otherwise>
  116. </xsl:choose>
  117. </xsl:template>
  118.  
  119. </xsl:stylesheet>
  120.  
  121. Example input
  122.  
  123. <headlines>
  124. <headline>Kibology for all. All for Kibology.</headline>
  125. </headlines>
  126.  
  127. would be transformed as
  128.  
  129. <html>
  130. <head>
  131. <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
  132. <title>Example</title>
  133. </head>
  134. <body>
  135. <h1>Headlines</h1>
  136. <h2> Kibology for all.</h2>
  137. </body>
  138. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.