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 648633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:51:16+00:00 2026-05-13T21:51:16+00:00

All the tutorials and examples I’ve found of XSLT processing seem to assume your

  • 0

All the tutorials and examples I’ve found of XSLT processing seem to assume your destination will be a significantly different format/structure to your source and that you know the structure of the source in advance. I’m struggling with finding out how to perform simple “in-place” modifications to a HTML document without knowing anything else about its existing structure.

Could somebody show me a clear example that, given an arbitrary unknown HTML source will:

1.) delete the classname 'foo' from all divs
2.) delete a node if its empty (ie <p></p>)
3.) delete a <p> node if its first child is <br>
4.) add newattr="newvalue" to all H1
5.) replace 'heading' in text nodes with 'title'
6.) wrap all <u> tags in <b> tags (ie, <u>foo</u> -> <b><u>foo</u></b>)
7.) output the transformed document without changing anything else

The above examples are the primary types of transform I wish to accomplish. Understanding how to do the above will go a long way towards helping me build more complex transforms.

To help clarify/test the examples here is a sample source and output, however I must reiterate that I want to work with arbitrary samples without rewriting the XSLT for each source:

<!doctype html>
<html>
<body>
  <h1>heading</h1>
  <p></p>
  <p><br>line</p>
  <div class="foo bar"><u>baz</u></div>
  <p>untouched</p>
</body>
</html>

output:

<!doctype html>
<html>
<body>
  <h1 newattr="newvalue">title</h1>
  <div class="bar"><b><u>baz</u></b></div>
  <p>untouched</p>
</body>
</html>
  • 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-13T21:51:16+00:00Added an answer on May 13, 2026 at 9:51 pm

    1.) delete the classname ‘foo’ from all divs

    <xsl:template match="div[contains(concat(' ', @class, ' '), ' foo ')]">
      <xsl:copy>
        <xsl:attribute name="class">
          <xsl:variable name="s" select="substring-before(concat(' ', @class, ' '), ' foo ')" />
          <xsl:variable name="e" select="substring-after(concat(' ', @class, ' '), ' foo ')" />
          <xsl:value-of select="normalize-space(concat($s, ' ', $e))" />
        </xsl:attribute>
        <xsl:apply-templates select="node() | @*[not(self::@class)]" />
      </xsl:copy>
    </xsl:template>
    

    2.) delete a node if its empty (ie <p></p>)

    <xsl:template match="*[normalize-space() = '']" />
    

    3.) delete a

    node if its first child is <br>

    <xsl:template match="p[*[1]/self::br]" />
    

    4.) add newattr="newvalue" to all <h1>

    <xsl:template match="h1[not(@newattr)]">
      <xsl:copy>
        <xsl:attribute name="newattr">
          <xsl:value-of select="'newvalue'" />
        </xsl:attribute>
        <xsl:apply-templates select="node() | @*" />
      </xsl:copy>
    </xsl:template>
    

    5.) replace ‘heading’ in text nodes with ‘title’

    <!-- This replaces the first occurrence of 'heading', case-sensitively.
         More generic search-and-replace templates are plenty, here on SO as well as 
         elsewhere on the 'net. -->
    <xsl:template match="text()[contains(concat(' ', ., ' '), ' heading ')]">
      <xsl:variable name="s" select="substring-before(concat(' ', ., ' '), ' heading ')" />
      <xsl:variable name="e" select="substring-after(concat(' ', ., ' '), ' title ')" />
      <xsl:value-of select="normalize-space(concat($s, ' ', $e))" />
    </xsl:template>
    

    6.) wrap all <u> tags in <b> tags (ie, <u>foo</u> -> <b><u>foo</u></b>)

    <xsl:template match="u[not(parent::*/self::b)]">
      <b>
        <xsl:copy>
          <xsl:apply-templates select="node() | @*" />
        </xsl:copy>
      </b>
    </xsl:template>
    

    7.) output the transformed document without changing anything else

    <!-- the identity template copies everything that is not handled by 
         any of the more specific templates above -->
    <xsl:template match="node() | @*">
      <xsl:copy>
        <xsl:apply-templates select="node() | @*" />
      </xsl:copy>
    </xsl:template>
    

    Template order and specificity determine which template “wins” when multiple templates could match the same node.

    More specific means: “Of multiple competing templates, the one that has the more complex matching rule wins”.

    Order means: “Of multiple competing templates with equal specificity, the one later in the XSLT document wins.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All the tutorials I have seen seem to use *.jsf , *.faces , or
The question says it all. I can't seem to find any recent rails tutorials
I think all of the tutorials and stuff are great on blogs, but sometimes
I'm just learning Objective-C/Cocoa programming for the Mac. All of the tutorials, books, blogs,
I'm looking at all the CSS Drop shadow tutorials, which are great. Unfortunately, I
All the articles I've found via google are either obsolete or contradict one another.
In almost all tutorials of BackgroundWorker the reportProgress event is handled like this (this
Greetings, I'm currently learning Cocoa and Objective-C. I've run through all the tutorials and
So we all know that #{someBean.value} will try and get the content of some
All the recent VisualSVN Server posts made me want to check it out. I

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.