CSS parser + XHTML generator, advice needed

元气小坏坏 提交于 2019-12-05 08:27:17

While I do not know any specific tool to do this, here is the basic approach I would take:

Load as xml document
Extract the css classes and styles from document
For each pair of css class and style
  Construct xpath query from css class
  For each matching node
    Set the style attribute for that class
Remove style node from document Convert document to string

Depends on how complicated your CSS is going to be. If it's a simple matter of elements ("p {}", "a {}"), IDs/Classes (#test {}), then probably easiest to use regular expressions. You'd have to have one to find all the style definitions and then parse them, then use more regular expressions to find instances of tags that match.

For, for example, if you found you had a style for A tags, you could use a regular expression like:

<a\b[^>]*>(.*?)</a>

To get them, then you'd have to do a replace to add the style. Of course you'd want the regex to accept the tag as a parameter (the A tag in this case).

If you got into child selection or anything more than just root elements and ID/classes this could get messy fast.

Consider just defining the styles inline to begin with?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!