2016-10-21 8 views
2

이제 XML 파일이 있고 섹션 레벨 속성 (특히 두 번째 또는 세 번째 레벨)을 수정해야합니다. 예를 들어 :두 번째 또는 세 번째 레벨 섹션 요소에서 조건이 일치하는 경우 속성 값 변경

입력 :

<?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>&ndash;<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>&#x2013;<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> 

는 두 번째 또는 세 번째 레벨 섹션 요소 내가 특정 섹션 수준 att1="deflist"에 대한 속성을 삽입해야 다음 DEF-목록을 선행합니다.

예상 출력 :

<?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>&ndash;<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>&#x2013;<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> 

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 
{ } 

사과가 ... 누군가가이 일에 저를 도울 수하고 감상 할 수있다.

+1

XML과 xpath 표현식이 일치하지 않습니다. 당신은 각각'sec'와'section'을 가지고 있습니다. 더러운 코드에 대해 사과하는 대신 [편집] 할 수 있고 적어도 복사하고 붙여 넣을 수있는'DATA' 섹션을 가진 완전한 예제를 제공 할 수 있습니다. 아직 해결하지 않으셨습니까? – simbabque

답변

5

먼저 xpath 표현식을 수정해야합니다. 귀하의 요소는 <sec>이 아니고 <section>이 아닙니다. 그런 다음 오른쪽 표현식을 사용하여 <def-list> 요소를 타겟팅해야합니다. 그것들은 두번째로 <sec> 앞에 직접 나오지 않으므로 두 개의 슬래시 //을 사용해야합니다.

sec/sec//def-list 

지금 핸들러를 들어, 요소를 취할 수 있으며, go up its tree<sec>의를 찾을 수 있습니다. 우리는 그것을 목록에 넣고 첫 번째 요소를 취합니다. 이것은 또 다른 요소입니다. 그것에 우리는 set the attribute. 그게 전부 야. 귀여운 프린트와

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> 

출력 : 두 번째 수준 <sec>에 하나 이상의 <def-list>가있는 경우

<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> 

또한 작동합니다.

+0

멋지고 굉장합니다. 그것의 작품은 내가 시도한 수준 모두 괜찮아. – ssr1012