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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:35:04+00:00 2026-06-06T00:35:04+00:00

OK so I have an xslt issue that I cannot seem to solve. This

  • 0

OK so I have an xslt issue that I cannot seem to solve. This is my second attempt at asking the question please do not mark as duplicate.

My issue is that I would like to include some HTML after the fourth <p> tag in my document. The issue is that there can be <p> tags with the attribute class="exclude"

If the <p> tag has that class, I would like to not render that paragraph. At the same time, I would still like to render my included html after the 4th paragraph, regardless of the number of <p class="excludes" there are

EDIT::
Here is the xsl I had previously. This would slot my included html into the 4th paragraph.

<xsl:template match="content/p[position() = 4]">
     content include<xsl:apply-templates /></p>
</xsl:template> 

Here are some example inputs / outputs

First is the base example where there are no <p class="excludes"> My input is:

<body>
<p></p>
<p></p>
<p></p>
<p></p>
include new HTML here
<p></p>
...
</body>

In that example, the html renders nicely after the 4th paragraph after passing through my xslt.

However, I need to take into account <p class="exclude">

Here is a more complicated example with <p class="exclude" being used >

<body>
<p class="exclude"></p> (should not be rendering on page)
<p></p>
<p></p>
<p></p>
<p></p>
include new HTML here
<p></p>
...
</body>

Notice that in this example, the included HTML is appearing after the 5th* paragraph because the first paragraph should not be rendering on the page, but at the same time I want the rendered HTML to be included after the 4th rendered paragraph. Lets look at one more example that is more complicated.

<body>
<p class="exclude"></p> (this should not be rendering on page)
<p></p>
<p class="exclude"></p> (this should not be rendering on page)
<p class="exclude"></p> ...
<p></p>
<p class="exclude"></p> ...
<p></p>
<p class="exclude"></p> ...
<p class="exclude"></p> ...
<p class="exclude"></p> ...
<p></p>
new HTML include here
<p></p>
...
<body>
<p class="exclude"></p>

Now, in my final example you can see that multiple <p class="excludes" have been added and yes i still want the new html content to render after the 4th rendered paragraph, or in this example the 11th total over all paragraph.

If anyone has any xslt that could help me achieve this that would be great. Please keep in mind I am using xslt 1.0 Thanks in advance

  • 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-06-06T00:35:07+00:00Added an answer on June 6, 2026 at 12:35 am

    Here’s another XSLT 1.0 stylesheet option.

    In this one I tried to take into account the “normal processing” of the p elements that was mentioned in the comments.

    In the example, I’m adding a processing instruction just to show that a p was processed. This can be replaced or removed depending on what you’re doing in your XSLT.

    XML Input

    <body>
        <p class="exclude">delete1</p>
        <p>keep1</p>
        <p class="exclude">delete2</p>
        <p class="exclude">delete3</p>
        <p>keep2</p>
        <p class="exclude">delete4</p>
        <p>keep3</p>
        <p class="exclude">delete5</p>
        <p class="exclude">delete6</p>
        <p class="exclude">delete7</p>
        <p>keep4</p>
        <p>keep5</p>
        <p class="exclude">delete8</p>
    </body>
    

    XSLT 1.0 (Tested with Xalan and Saxon 6.5.5)

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
    
        <!--Strip p if class attribute value is exclude.-->
        <xsl:template match="p[@class='exclude']"/>
    
        <!--Special handling of the 4th non-exclude p. Call the "normal" template to 
            handle any other p processing and then output the additional HTML.-->
        <xsl:template match="p[not(@class='exclude')][count(preceding::p[not(@class='exclude')])+1=4]">
            <xsl:call-template name="normal"/>      
            <b>ADDITIONAL HTML HERE</b>
        </xsl:template>
    
        <!--This is the "normal" processing of "p". For the example, just adding a PI.-->
        <xsl:template match="p" name="normal">
            <xsl:copy>
                <xsl:processing-instruction name="processed">normally</xsl:processing-instruction>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>     
        </xsl:template>
    
    </xsl:stylesheet>
    

    Output

    <body>
       <p><?processed normally?>keep1</p>
       <p><?processed normally?>keep2</p>
       <p><?processed normally?>keep3</p>
       <p><?processed normally?>keep4</p>
       <b>ADDITIONAL HTML HERE</b>
       <p><?processed normally?>keep5</p>
    </body>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .xslt that translates xml from one form to another (I'm not
I have an XSLT transform issue: style=width:{Data/PercentSpaceUsed}%; And the value of Data/PercentSpaceUsed is integer
I have this XSLT 2.0: <xsl:stylesheet version=2.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:output indent=yes/> <xsl:strip-space elements=*/> <xsl:template match=node()|@*>
I have an XSLT stylesheet that transforms an XML file to JSON format and
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty
[Please edit the title if you find its not good enough] I have code
I have XSLT 1.0 code like this: <xsl:key name=enemyItems match=metadata[attributes/metadata_key/@value = 'enemylist'] use=attributes/metadata_refkey/@value/> <xsl:template
I have some XSLT that gets rendered in the Sitecore 6 CMS, but I
A while ago I asked this question: using xslt stylesheet to convert xhtml blank
I have an issue with transforming an xml data by using xslt template. 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.