首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果条件在第二级或第三级区段元素中匹配,则更改属性值。

如果条件在第二级或第三级区段元素中匹配,则更改属性值。
EN

Stack Overflow用户
提问于 2016-10-21 14:26:38
回答 1查看 46关注 0票数 2

现在,我有了一个XML文件,并且要修改节级别属性(特别是第二级或第三级)。例如:

输入:

代码语言:javascript
复制
<?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>

如果第二级或第三级区段元素位于def-list之前,那么我就插入了特定区段级别att1="deflist"的属性。

预期产出:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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
{  }

为肮脏的代码道歉..。任何人都可以在这件事上帮助我,我将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-21 14:50:12

首先,您需要修复xpath表达式。您的元素名为<sec>,而不是<section>。然后,您需要使用正确的表达式来针对<def-list>元素。它们不直接出现在第二个<sec>之前,因此您需要使用两个斜杠//

代码语言:javascript
复制
sec/sec//def-list

现在,对于处理程序,您可以使用元素和go up its tree来查找<sec>,我们将其放入一个列表中,并接受第一个列表,这是另一个元素。在这一点上,我们set the attribute。就这样。

代码语言:javascript
复制
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>

输出有漂亮的打印:

代码语言:javascript
复制
<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>

如果第二级<def-list>中有多个<sec>,它也会工作。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40179208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档