[英]How to parse JSF style XHTML
We have a Java project which uses JSF. 我们有一个使用JSF的Java项目。 In our view layer we didn't use id
s in all our XHTML files. 在我们的视图层中,我们并未在所有XHTML文件中使用id
。 What I want to do is to parse XHTML files and check for tags like 我想做的是解析XHTML文件并检查诸如
<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. 常量部分是<h:input...
>选择正确的标签后,其余部分可以是<h:inputSecret
<h:inputWHATELSE
。 I want to check if there is an id
attribute of that tag. 我想检查该标签是否有id
属性。 If not, I want to add an id
to it. 如果没有,我想添加一个id
。
Here is one of our XHTML files. 这是我们的XHTML文件之一。
I tried to do the work with Python. 我试图用Python做这项工作。 I tried ElementTree, piksemel and BeautifulSoup. 我尝试了ElementTree,piksemel和BeautifulSoup。 Any help about this issue will be appreciated. 任何有关此问题的帮助将不胜感激。
Using Beautifulsoup, you could do this as follows: 使用Beautifulsoup,您可以执行以下操作:
from BeautifulSoup import BeautifulSoup
import re
soup = BeautifulSoup(<your_xml_here>)
nodes = soup.findAll(name=re.compile('^h:input'))
for node in nodes:
if 'id' not in dict(node.attrs):
node['id'] = <whatever you need>
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 id
is defined or not and assign a new value when needed. 之后,您可以检查节点属性以确保是否定义了id
并在需要时分配新值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.