2012-05-29 39 views
-1

我在寫一些通過XPath查詢從Xml文件讀取的perl腳本。 從Perl腳本中,我需要返回具有最近日期的3部電影(類似於按日期排序的電影)。從XPath查詢中按日期訂購Xml項目

XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="films.xsl"?> 
<collection xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://...." xs:schemaLocation="http://.. films.xsd"> 

<film id="1"> 
    <title>Title1</title> 
    <date>2011-09-24</date> 
    <family>Action</family> 
</film> 
<film id="4"> 
    <title>Title2</title> 
    <date>1980-09-24</date> 
    <family>Thriller</family> 
</film> 

</collection> 

我該怎麼辦?我閱讀並嘗試更多解決方案,但任何事情

我嘗試

my $nodeset = $xp->findnodes(
"/collection/film[not(\@id < preceding-sibling::collection/film[\@id]) and not(\@id < following-sibling::collection/film[\@id])]"); 

我嘗試XPath 2.0中的max函數只返回最近, 我嘗試XSL

<xsl:for-each select="collection/film/date"> 
<xsl:sort select="." data-type="date" order="descending"/> 
<xsl:value-of select="title"/> 
</xsl:for-each> 

回答

0

查找使用XML::XSH2最新電影:

#!/usr/bin/perl 
use warnings; 
use strict; 

use XML::XSH2; 

xsh << 'end;'; 
    open films.xml ; 
    register-namespace xs http://.... ; 
    $films := hash xs:date /xs:collection/xs:film ; 
    $latest = { (sort { $films->{$a} cmp $films->{$b} } 
        keys %$films)[0] } ; 
    ls xsh:lookup('films', $latest) ; 
end;