一個節點的XML文件看起來像如下:的Perl的libxml:使用findnodes與屬性,達到在xml文件
<?xml version="1.0"?>
<application name="pos">
<artifact id="123" type="war" cycle="Release7-Sprint1">
<jira/>
<jenkins/>
<deployment/>
<scm>
<transaction id="1234" user="">
<file name=""/>
<file name=""/>
</transaction>
</scm>
</artifact>
</application>
我的代碼塊下面看上去,當我使用屬性的硬編碼值工作正常(名稱),而不是使用變量。我引用線(我的$查詢=
'//應用[@名稱= 「POS」]';)
my $manifestDoc = $manifestFileParser->parse_file($manifestFile);
my $changeLogDoc = $changeLogParser->parse_file($changeLogXml);
my $changeLogRoot = $changeLogDoc->getDocumentElement;
#my $applicationName = pos;
my $query = '//application[@name="pos"]';
my $applicationNode = $manifestDoc->findnodes($query);
my $artifactNode = $manifestDoc->createElement('artifact');
$artifactNode->setAttribute("id",$artifactID);
$artifactNode->setAttribute("type",$artifactType);
$artifactNode->setAttribute("cycle",$releaseCycle);
$applicationNode->[0]->appendChild($artifactNode);
但是如果我修改$查詢變量使用一個變量($的applicationName)而不是屬性的硬編碼值,它給了我一個編譯錯誤下面說:
在updateManifest.pl線無法調用「的appendChild」未定義的值
修改後的代碼:
my $applicationName = "pos" ;
my $query = '//application[@name="$applicationName"]';
不知道什麼是錯的。任何與報價有關的事情? 任何幫助,非常感謝。
非常感謝。我已經使用了選項1,它工作。 – user1470220
XPath也支持單引號。所以我通常會使用''// application [\ @ name ='$ applicationName']「'。 – nwellnhof