We have a Java project which uses JSF. In our view layer we didn’t use ids in all our XHTML files. What I want to do is to parse XHTML files and check for tags like
<h:inputText id="username" value="#{identity.username}"/>
The constant part is <h:input...>
The rest can be anything like <h:inputSecret <h:inputWHATELSE after selecting the correct tag. I want to check if there is an id attribute of that tag. If not, I want to add an id to it.
Here is one of our XHTML files.
I tried to do the work with Python. I tried ElementTree, piksemel and BeautifulSoup. Any help about this issue will be appreciated.
Using Beautifulsoup, you could do this as follows:
As it can be seen, to get all the nodes that match the name pattern you’re looking for all you need is a regular expression. After that, you can check the node attributes to make sure if
idis defined or not and assign a new value when needed.