如果 XML 文档没有特定属性, 如何在解析该文档时删除所有标签? 例如, 我希望所有标签( 当然除了根) 都有名称属性 。 我正使用 XML 来建立树类数据库, 没有名称的标签根本就没有意义 。
我当然可以对所有标签(深处)进行循环, 并检查属性是否存在, 但大文件需要一些时间 。
我想应该有办法用XMLParser... 来做这个... 也许用一些计划?
如果 XML 文档没有特定属性, 如何在解析该文档时删除所有标签? 例如, 我希望所有标签( 当然除了根) 都有名称属性 。 我正使用 XML 来建立树类数据库, 没有名称的标签根本就没有意义 。
我当然可以对所有标签(深处)进行循环, 并检查属性是否存在, 但大文件需要一些时间 。
我想应该有办法用XMLParser... 来做这个... 也许用一些计划?
在 XPath 和 lxml 中,这应该起作用:
from lxml import etree
xml = etree.XML("<root><a name= 1 ><b name= 1-1 >ABC</b></a><a>Does not exist</a><a name= 2 >DEF</a><a><b name= 3-1 >GHI</b></a></root>")
print Before:
print etree.tostring(xml)
xp = etree.XPath("/*/*[not(@name)]") # or "//*[not(@name)]" to include the root tag
all_nodes = xp(xml)
for x in all_nodes:
parent = x.getparent()
#if parent is None: continue # if the root tag is included, the parent is None
parent.remove(x)
print After:
print etree.tostring(xml)
在 XSLT 中非常容易。 两种模板规则, 一种身份规则, 复制一切 :
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
以及抛弃你们所不想要的元素的另一条规则:
<xsl:template match="*[not(@specific-attribute)]"/>
Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...
I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...
Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...
Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...
I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...
Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...
Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...
I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...