4

There are a lot of answers for stripping HTML tags from a string, but I'd like to strip only a specific attribute: style. The HTML that I'm dealing with has some seriously nasty inline styles, and often looks something like this:

<p class="someclass" style="margin-left:2cm;text-indent:-36.0pt">Blah.</p>

In order to adjust the display for my application, I need to strip that style attribute. Is there a fast way to process the document to do this? It needs to work in iOS.

Thanks!

1
  • 1
    The fastest solution is to do this in JavaScript in the web browser. Commented Jul 3, 2011 at 1:12

3 Answers 3

1

Use an XSLT transformation. See http://developer.apple.com/library/mac/documentation/cocoa/Conceptual/NSXML_Concepts/Articles/WritingXML.html#//apple_ref/doc/uid/TP40001256-112639

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, the XSLT stuff doesn't seem to be available to iOS. I should have mentioned that before, sorry!
1

Ultimately, I went with a combination of ElementParser and regular expressions (using RegExKitLite), and stripping out the tags I didn't want and replacing them with ones I did, as required. Given that my HTML is coming from a trusted source, this should be fine.

It's far from ideal, but it's working. :-)

Comments

0

Well probably the simplest (but also quite expensive (CPU intensive)) is to use NSAttributedString+HMTL to turn it into an NSAttributedString. Then you can get the NSString from that.

Something like this.

  NSAttributedString *attrstring = [NSAttributedString attributedStringWithHTML:[htmlString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES] options:nil];

  //Access the string itself like this.
  [attrstring string];

[Warning: although this is the easiest way, (for you), it might not be the best way as it is quite expensive todo and will block your UI if done on the main thread (for obvious reasons)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.