现在我有一个XML文件,并且要修改Section级别属性(特别是第二级或第三级).例如:
输入:
<?xml version="1.0"?>
<article>
<front></front>
<body>
<sec id="sec1">
<title>1. Introduction</title><p>The cerebrospinal venous system has been the focus of many studies in the last few years, because of the hypothesized involvement of insufficient extracranial venous drainage in central nervous system disorders such as multiple sclerosis, normal-pressure hydrocephalus, and transient monocular blindness [<xref ref-type="bibr" rid="B1">1</xref>–<xref ref-type="bibr" rid="B4">4</xref>]. An insufficiency in venous blood drainage can be due to the presence of single or multiple stenosis on the main routes of cerebrospinal venous system [<xref ref-type="bibr" rid="B5">5</xref>].</p>
<sec id="sec1.1">
<title>Section level 2</title>
<p><def-list><def-item><term>term I:</term><def><p>defintion I</p></def></def-item><def-item><term>term 2:</term><def><p>defintion 2</p></def></def-item></def-list>In the past years, great efforts have been made to develop excellent algorithms and tools for the processing and analyzing of traditional BS-Seq data [<xref ref-type="bibr" rid="B7">7</xref>–<xref ref-type="bibr" rid="B10">10</xref>] but none for hairpin-BS-Seq data. In this study, we designed and implemented HBS-tools and compared them against other state-of-the-art mapping tools. Our result indicated that HBS-tools have a reduced mapping time and improved mapping efficiency.</p>
</sec>
</sec></body>
</article>
Run Code Online (Sandbox Code Playgroud)
如果第二级或第三级部分元素在def-list之前,那么我已插入特定部分级别的属性att1="deflist".
预期产出:
<?xml version="1.0"?>
<article>
<front></front>
<body>
<sec id="sec1">
<title>1. Introduction</title><p>The cerebrospinal venous system has been the focus of many studies in the last few years, because of the hypothesized involvement of insufficient extracranial venous drainage in central nervous system disorders such as multiple sclerosis, normal-pressure hydrocephalus, and transient monocular blindness [<xref ref-type="bibr" rid="B1">1</xref>–<xref ref-type="bibr" rid="B4">4</xref>]. An insufficiency in venous blood drainage can be due to the presence of single or multiple stenosis on the main routes of cerebrospinal venous system [<xref ref-type="bibr" rid="B5">5</xref>].</p>
<sec id="sec1.1" att1="deflist">
<title>Section level 2</title>
<p><def-list><def-item><term>term I:</term><def><p>defintion I</p></def></def-item><def-item><term>term 2:</term><def><p>defintion 2</p></def></def-item></def-list>In the past years, great efforts have been made to develop excellent algorithms and tools for the processing and analyzing of traditional BS-Seq data [<xref ref-type="bibr" rid="B7">7</xref>–<xref ref-type="bibr" rid="B10">10</xref>] but none for hairpin-BS-Seq data. In this study, we designed and implemented HBS-tools and compared them against other state-of-the-art mapping tools. Our result indicated that HBS-tools have a reduced mapping time and improved mapping efficiency.</p>
</sec>
</sec></body>
</article>
Run Code Online (Sandbox Code Playgroud)
mycode的:
use strict;
use warnings;
use XML::Twig;
my $t= XML::Twig->new( twig_handlers =>
{ 'sec/section/def-list' => \&Check_deflist }
)
->parsefile('input.xml');
sub Check_deflist
{ }
Run Code Online (Sandbox Code Playgroud)
为脏代码道歉...任何人都可以帮助我这一个,我们将不胜感激.
首先,您需要修复xpath表达式.你的元素被称为<sec>,而不是<section>.然后,您需要使用正确的表达式来定位<def-list>元素.它们不直接在第二个之前<sec>,因此您需要使用两个斜杠//.
sec/sec//def-list
Run Code Online (Sandbox Code Playgroud)
现在对于处理程序,您可以获取元素并在其树上查找<sec>s.我们把它放到一个列表中并取第一个,这是另一个元素.在那,我们设置属性.而已.
use strict;
use warnings;
use XML::Twig;
my $t = XML::Twig->new(
twig_handlers => { 'sec/sec//def-list' => \&Check_deflist },
pretty_print => 'indented'
)->parse( \*DATA )->print;
sub Check_deflist {
( $_->ancestors('sec') )[0]->set_att( att1 => 'deflist' );
}
__DATA__
<sec id="sec1">
<title>Section level 1</title>
<p>.......</p>
<sec id="sec1.1">
<title>Section level 2</title>
<p><def-list><p>...</p>...</def-list></p>
</sec>
</sec>
Run Code Online (Sandbox Code Playgroud)
输出漂亮的印刷:
<sec id="sec1">
<title>Section level 1</title>
<p>.......</p>
<sec att1="deflist" id="sec1.1">
<title>Section level 2</title>
<p>
<def-list><p>...</p>...</def-list>
</p>
</sec>
</sec>
Run Code Online (Sandbox Code Playgroud)
如果<def-list>在第二级中存在多个,则它也将起作用<sec>.