![](/img/trans.png)
[英]How do I create individual XML files from the parsed data output of a XML file?
[英]How do I delete an element from an XML file after it is parsed?
假設XML文件是:
<class name=math>
<student>luke1</student>
...
<student>luke8000000</student>
</class>
<class name=english>
<student>mary1</student>
...
<student>mary1000000</student>
</class>
在解析class=math
之后,我想從XML文件中刪除此元素,以便在解析class=english
時,Twig將不會遍歷class=math
的內容。
我想這樣做的原因是,到目前為止,即使我使用TwigRoots => {"class[\\@name='english']" => \\&counter}
我仍然需要等待很長時間才能讓Twig開始解析class=english
因為它需要遍歷class=math
每一行(如果不需要遍歷每一行,請更正我)。 在我運行的實際文件中,有幾個類,在找到它真正感興趣的類之前,我不想讓Twig遍歷class=math
每一行。
提前致謝。
構建樹枝時可以使用ignore_elts
選項:
ignore_elts
This option lets you ignore elements when building the twig. This is useful
in cases where you cannot use "twig_roots" to ignore elements, for example
if the element to ignore is a sibling of elements you are interested in.
Example:
my $twig= XML::Twig->new( ignore_elts => { elt => 1 });
$twig->parsefile( 'doc.xml');
This will build the complete twig for the document, except that all "elt"
elements (and their children) will be left out.
在這種情況下,您可以編寫XML :: Twig-> new(ignore_elts ignore_elts => { 'class[@name="math"]' => 1 }, ...
以跳過那些元素
請注意,這些元素將不包含在樹中,但仍將被解析。 這樣可以加快速度,但是速度卻不那么快(定量數據的速度如何?;-)在任何情況下都需要解析整個文件。
順便說一句,問題中的XML格式不正確,應在屬性周圍加引號。
我尚未將刪除功能與TWIG一起使用,但是請檢查此鏈接。這具有一些有關使用TWIG刪除節點的信息
相關部分在這里:
\n }\n 其他{\n $ para->刪除;\n }\n }\n如果該段落不包含與指定關鍵字匹配的內容,則該段落處理程序的最后一部分將從結果樹中刪除該樹枝。 這樣可以確保只有包含匹配項的那些段落才能進入最終輸出。
$para
是傳遞給處理程序的元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.