在perl中使用XML :: LibXML在XML中使用元字符进行字符串搜索

时间:2013-07-14 16:52:05

标签: xml perl xpath xml-libxml

我正在读取一个XML文件,并希望将“print”中的XPath替换为“@href =”* releaseStatus = RELEASED“,这样我就可以使它成为第一次出现的实例。我怎么能实现那个?

use XML::LibXML;

my $parser = XML::LibXML->new();
my $doc = $parser->parse_file("type_book.xml");

for my $chapter_node ($doc->findnodes('/book/contents'))
{
print $chapter_node->findvalue('locator[@href ="/book/isbn/979-0-4444-1000-17/book-part/chapter/bk444444ch1?releaseStatus=RELEASED"]/@title');
}

XML文件:

<book>
<contents>
<locator href="/book/isbn/979-0-4444-1000-17/book-part/chapter/bk444444ch1?  releaseStatus=RELEASED" role="http://www.abc.org/roles/book-part-locator" title="Photonic crystal light-emitting sources">
</locator>
<locator href="/book/isbn/979-0-4444-1000-17/book-part/chapter/bk444444ch1?releaseStatus=RELEASED&amp;format=pdf" role="http://www.abc.org/roles/book-part-pdf-locator" title="Photonic crystal light-emitting sources">
</locator>
<locator href="/book/isbn/979-0-4444-1000-17/book-part/chapter/bk444444ch1?releaseStatus=RELEASED&amp;format=epub" role="http://www.abc.org/roles/book-part-epub-locator" title="Photonic crystal light-emitting sources">
</locator>
</contents>
</book>

1 个答案:

答案 0 :(得分:2)

够好吗?

locator[contains(@href, "?releaseStatus=RELEASED")]/@title

使用XPath 2函数(不确定XML :: LibXML是否支持)

locator[fn:matches(@href, "^.*(\?|&)releaseStatus=RELEASED(&|$)")]/@title

您可能需要将fnhttp://www.w3.org/2005/xpath-functions关联。