Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have HTML files with all sharing the same structure. Something like this:

Body
    Table
         tr1
             ... (more nested tables)
         tr2
             ... (more nested tables)
         tr3
             td1
             ... (more nested tables)
             td2
          tr4 ...

I need to remove td1 table data from all the files. What tool should I use?

share|improve this question
    
Any tool that supports replacing text using regular expressions. –  Hoff Aug 15 '12 at 16:54
    
Since the main table may contain unknown number of tables I find it difficult to use regexps. –  Jack Aug 15 '12 at 20:05
1  
@Jack That comment doesn't make sense. You only show one table, and it has no tables within it, only some rows(and cells). You either need to update your example or correct your description to more closely describe what you want to happen and what you're working with. –  Su' Aug 16 '12 at 4:52
2  
Is there anything specific that identifies the <td> that you need to remove? For example, is it always the first <td> in the third <tr>, or does it have a class name or the same content in all files? Without having a way to identify the element you need to remove across all files, it's hard to suggest an automated process or regular expression to accomplish it. –  Nick Aug 16 '12 at 9:11
2  
Do you know Python or Perl or other programming or scripting languages? Python, for example, has a built-in HTML parser. –  paulmorriss Aug 16 '12 at 9:38
show 4 more comments

2 Answers 2

JavaScript and the DOM are very quick ways to update any items on a web page. If you understand scripting, it is pretty easy to pick up as it's loosely typed. If there is a unique identifier, or if you have a way of adding one, it's pretty useful. Or you can fetch the table, loop through rows or cells, whichever you prefer, checking each element for evidence that the item needs to be removed, and then you can pluck it right off the DOM tree. JavaScript all the way!

UPDATE

First, you should read this. Don't stop there! MDN has a lot of great information, including reference for many different languages including HTML, the DOM, and Javascript. For an "Idiot's Guide" starting point, you can check out this, but you you should keep in mind that their information is not perfect and they receive a fair amount of discredit from the dev community. However, it is nevertheless a good starting point.

Given your example, you want to get rid of the first cell. I am going to guess for the sake of this example that your table will have an id. So that would look something like this:

var table = document.getElementById('tableID'); //There are other ways to fetch HTML elements, but this is the most direct. It returns a table object.
var rows = table.rows; //Create a variable to store the table rows.
row1 = rows[0]; //Stores the first row in a variable.
row1.deleteCell(0); //Delete the first cell in the first row.

Admittedly, this is a very simplified version. If you are removing cells dynamically, you are probably going to have to collect rows and iterate through them searching for attributes: classes, IDs, etc, and then remove them. If you need help with that, I recommend posting more questions.

I hope this helps!

share|improve this answer
    
I know a little JavaScript, never worked with DOM, though. Can you give me some good place to start from with this JS/DOM stuff? –  Jack Sep 19 '12 at 10:19
    
I updated my original answer for you. –  nodirtyrockstar Sep 19 '12 at 15:08
add comment

Actually, there was a StackOverflow question about a similar problem; the most useful of which (and also the least complicated) includes using XSLT to modify your XHTML into another format.

http://help.hannonhill.com/discussions/how-do-i/269-strip-specific-html-tag-in-xslt

I also found an interesting gem, the HTML Enforcer. DISCLAIMER: I haven't used HTML Enforcer, so use with caution.

share|improve this answer
    
Wouldn't the XSLT only modify the output upon viewing? I read the question as wanting to actually modify the source files themselves. @Jack: you may want to clarify this, also. –  Su' Aug 16 '12 at 4:54
    
Yes, it would. If anything, the XSLT could be used as a quick patch until a more permanent solution is found. The problem (from what I understand from @Jack) is batch editing multiple HTML files with different DOM trees. In that case, it's a pretty tall order for any program to deliver on. –  Christopher Aug 16 '12 at 8:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.