Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

How to Use filter to parse and modify html to replace strings

Avatar

Level 2

Hi,

 

I am new to filters and would like to know how to modify the html on a page using them. My use case is such that I'm trying to dynamically look for certain strings in the html when rendering a page and replace them with another string (this is a very simplified version of what I'm trying to achieve but is a good summary).

 

I think filters would be a good candidate to achieve this however it is difficult to find resources on this. Any help is appreciated and I'd be happy to provide more details if necessary!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Sling filter

Sling supports filtering the request processing by applying filter chains to the requests before actually dispatching to the servlet or script for processing. Filters to be used in such filter processing are plain OSGi services of type javax.servlet.Filter which of course means that the services implement this interface.

So ib this case, if you want to intercept the request you can use filter but for your use case it is not the best option

 

Sling Rewrite/Transformer

The Apache Sling Rewriter is a module for rewriting the output generated by a usual Sling rendering process. Some possible use cases include rewriting or checking all links in an HTML page, manipulating the HTML page, or using the generated output as the base for further transformation. An example of further transformation is to use XSLT to transform rendered XML to some output format like HTML or XSL:FO for generating PDF.

https://sling.apache.org/documentation/bundles/output-rewriting-pipelines-org-apache-sling-rewriter....

 

 

 

HTML Parser can be used with either above of services.

 



Arun Patidar

View solution in original post

6 Replies

Avatar

Community Advisor

Hi,

 

With your use case, I think you can achieve with html parser instead of filter

 

Check the below link

https://aem4beginner.blogspot.com/creating-html-parser-service-for-adobe

Avatar

Level 2

This link is giving 404 not found. Do you have an alternate resource?

Avatar

Community Advisor

In this case, you can do it using sling rewriter e.g.

https://wcm.io/handler/url/rewriter.html

 



Arun Patidar

Avatar

Level 2

@arunpatidar @Ravi_Pampana what would you say the difference would be between using a Filter vs HTML Parser vs Service vs Rewriter vs Transformer? It's pretty confusing the difference and I would like to understand the difference before making a decision!

Avatar

Correct answer by
Community Advisor

Sling filter

Sling supports filtering the request processing by applying filter chains to the requests before actually dispatching to the servlet or script for processing. Filters to be used in such filter processing are plain OSGi services of type javax.servlet.Filter which of course means that the services implement this interface.

So ib this case, if you want to intercept the request you can use filter but for your use case it is not the best option

 

Sling Rewrite/Transformer

The Apache Sling Rewriter is a module for rewriting the output generated by a usual Sling rendering process. Some possible use cases include rewriting or checking all links in an HTML page, manipulating the HTML page, or using the generated output as the base for further transformation. An example of further transformation is to use XSLT to transform rendered XML to some output format like HTML or XSL:FO for generating PDF.

https://sling.apache.org/documentation/bundles/output-rewriting-pipelines-org-apache-sling-rewriter....

 

 

 

HTML Parser can be used with either above of services.

 



Arun Patidar