gpt4 book ai didi

perl - 如果第二级或第三级部分元素中的条件匹配,则更改属性值

转载 作者:行者123 更新时间:2023-12-02 09:23:11 25 4
gpt4 key购买 nike

现在我有一个 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>

如果第二级或第三级节元素位于 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>&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>

我的代码:

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

对脏代码表示歉意...任何人都可以在这方面帮助我,我将不胜感激。

最佳答案

首先您需要修复您的 xpath 表达式。您的元素名为 <sec >,不是<section> 。然后您需要使用正确的表达式来定位 <def-list>元素。它们并不直接位于第二个 <sec> 之前,所以你需要使用两个斜杠 // .

sec/sec//def-list

现在对于处理程序,您可以获取元素和 go up its tree查找<sec> s。我们将其放入列表中并取出第一个,这是另一个元素。对此,我们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>

带有 pretty-print 的输出:

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

关于perl - 如果第二级或第三级部分元素中的条件匹配,则更改属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40179208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com