Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6592349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:32:36+00:00 2026-05-25T17:32:36+00:00

I need wrapping each word with a tag (e. span) in a HTML document,

  • 0

I need wrapping each word with a tag (e. span) in a HTML document, like:

<html>
<head>
    <title>It doesnt matter</title>
</head>
<body>
         <div> Text in a div </div>
         <div>
    Text in a div
    <p>
        Text inside a p
    </p>
     </div>
</body>
</html>

To result something like this:

<html>
<head>
    <title>It doesnt matter</title>
</head>
<body>
         <div> <span>Text </span> <span> in </span> <span> a </span> <span> div </span> </div>
         <div>

             <span>Text </span> <span> in </span> <span> a </span> <span> div </span>                     
             <p>
               <span>Text </span> <span> in </span> <span> a </span> <span> p </span> 
             </p>
     </div>
</body>
</html>

It’s important to keep the structure of the body…

Any help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:32:36+00:00Added an answer on May 25, 2026 at 5:32 pm

    All of the three different solutions below use the XSLT design pattern of overriding the identity rule to generally preserve the structure and contents of the XML document, and only modify specific nodes.

    I. XSLT 1.0 solution:

    This short and simple transformation (no <xsl:choose> used anywhere):

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="*[not(self::title)]/text()"
                   name="split">
      <xsl:param name="pText" select=
           "concat(normalize-space(.), ' ')"/>
    
      <xsl:if test="string-length(normalize-space($pText)) >0">
       <span>
       <xsl:value-of select=
            "substring-before($pText, ' ')"/>
       </span>
    
       <xsl:call-template name="split">
        <xsl:with-param name="pText"
             select="substring-after($pText, ' ')"/>
       </xsl:call-template>
      </xsl:if>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied to the provided XML document:

    <html>
        <head>
            <title>It doesnt matter</title>
        </head>
        <body>
            <div> Text in a div </div>
            <div>
             Text in a div
                <p>
                 Text inside a p
             </p>
            </div>
        </body>
    </html>
    

    produces the wanted, correct result:

    <html>
       <head>
          <title>It doesnt matter</title>
       </head>
       <body>
          <div>
             <span>Text</span>
             <span>in</span>
             <span>a</span>
             <span>div</span>
          </div>
          <div>
             <span>Text</span>
             <span>in</span>
             <span>a</span>
             <span>div</span>
             <p>
                <span>Text</span>
                <span>inside</span>
                <span>a</span>
                <span>p</span>
             </p>
          </div>
       </body>
    </html>
    

    II. XSLT 2.0 solution:

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="*[not(self::title)]/text()">
      <xsl:for-each select="tokenize(., '[\s]')[.]">
       <span><xsl:sequence select="."/></span>
      </xsl:for-each>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied to the same XML document (above), again the correct, wanted result is produced:

    <html>
       <head>
          <title>It doesnt matter</title>
       </head>
       <body>
          <div>
             <span>Text</span>
             <span>in</span>
             <span>a</span>
             <span>div</span>
          </div>
          <div>
             <span>Text</span>
             <span>in</span>
             <span>a</span>
             <span>div</span>
             <p>
                <span>Text</span>
                <span>inside</span>
                <span>a</span>
                <span>p</span>
             </p>
          </div>
       </body>
    </html>
    

    III Solution using FXSL:

    Using the str-split-to-words template/function of FXSL one can easily implement much more complicated tokenization — in any version of XSLT:

    Let’s have a more complicated XML document and tokenization rules:

    <html>
        <head>
            <title>It doesnt matter</title>
        </head>
        <body>
            <div> Text: in a div </div>
            <div>
             Text; in; a. div
                <p>
                 Text- inside [a] [p]
             </p>
            </div>
        </body>
    </html>
    

    Here there is more than one delimiter that indicates the start or end of a word. In this particular example the delimiters can be: " ", ";", ".", ":", "-", "[", "]".

    The following transformation uses FXSL for this more complicated tokenization:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ext="http://exslt.org/common"
     exclude-result-prefixes="ext">
    
       <xsl:import href="strSplit-to-Words.xsl"/>
    
       <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
       <xsl:strip-space elements="*"/>
    
        <xsl:template match="node()|@*">
            <xsl:copy>
              <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="*[not(self::title)]/text()">
          <xsl:variable name="vwordNodes">
            <xsl:call-template name="str-split-to-words">
              <xsl:with-param name="pStr" select="normalize-space(.)"/>
              <xsl:with-param name="pDelimiters" 
                              select="' ;.:-[]'"/>
            </xsl:call-template>
          </xsl:variable>
    
          <xsl:apply-templates select="ext:node-set($vwordNodes)/*"/>
        </xsl:template>
    
        <xsl:template match="word[string-length(normalize-space(.)) > 0]">
          <span>
            <xsl:value-of select="."/>
          </span>
        </xsl:template>
    </xsl:stylesheet>
    

    and produces the wanted, correct result:

    <html>
       <head>
          <title>It doesnt matter</title>
       </head>
       <body>
          <div>
             <span>Text</span>
             <span>in</span>
             <span>a</span>
             <span>div</span>
          </div>
          <div>
             <span>Text</span>
             <span>in</span>
             <span>a</span>
             <span>div</span>
             <p>
                <span>Text</span>
                <span>inside</span>
                <span>a</span>
                <span>p</span>
                <word/>
             </p>
          </div>
       </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a little ruby function that does word wrapping. I have
I need good word-wrapping handling for Java. Not too difficult, except for one wrinkle:
I need to build a HTML/CSS layout with the following layout |Static Div|Content Div0|Content
See title. I need a regular expression to replace newlines with wrapping <p> tags.
I'm trying to wrap each text character inside a <div id=test></div> with a span
I need to create an endless wrapping world with Box2D (where the X coordinate
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
Still wrapping my head around SQL and PHP, but hope someone can help with
I'm having troubles wrapping my head around how to organize the classes within my
I'm having a bit of an issue wrapping my head around something. I'm currently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.