如何使用Perl的XML :: XPath与非英语元素名称?

时间:2017-07-25 15:12:37

标签: xml perl xpath utf-8

当某些元素与某些元素相关时,我如何使用XML::XPath?名字不是英文的?

我使用Strawberry Perl。

我从网上获得了employees.xmltrain_xml.pl,但效果很好。

但是当我添加一些中文字符时,我收到以下错误:

  

Wide character in die at D:/Strawberry/perl/site/lib/XML/XPath/Parser.pm line 189.

Query:
/employees/employee[@age="30"]/工作...
..............................^^^
Invalid query somewhere around here (I think)

我该如何解决这个问题?

employees.xml

<?xml version="1.0" encoding="utf-8" ?>
<employees>
    <employee age="30">
        <name>linux</name>
    <country>US</country>
    <工作>教师</工作>
    </employee>
    <employee age="10">
    <name>mac</name>
    <country>US</country>
    </employee>
    <employee age="20">
    <name>windows</name>
    <country>US</country>
    </employee>
</employees>

train_xml.pl

use Encode;
use XML::XPath->new;
use utf8;
my $xp=XML::XPath->new(filename=>"employees.xml");
print $xp->findvalue('/employees/employee[@age="10"]/name'),"\n";
my $path1 = '/employees/employee[@age="30"]/工作';
print $xp->findvalue($path1),"\n";

2 个答案:

答案 0 :(得分:4)

您可以使用XML::LibXML

#!/usr/bin/perl

use strict;
use warnings;

use utf8;
use open ':std', ':encoding(UTF-8)';

use feature qw( say );

use XML::LibXML qw( );

{
   my $parser = XML::LibXML->new();
   my $doc = $parser->parse_file($ARGV[0]);
   say $doc->findvalue('/employees/employee[@age="10"]/name');
   say $doc->findvalue('/employees/employee[@age="30"]/工作');
}

输出:

$ ./a a.xml
mac
教师

如果您想继续使用(错误,速度较慢且广泛使用的)XML::XPath,您可以使用以下内容:

#!/usr/bin/perl

use strict;
use warnings;

use utf8;
use open ':std', ':encoding(UTF-8)';

use feature qw( say );

use XML::XPath qw( );

{ # Monkeypatch XML::XPath.
   package XML::XPath::Parser;

   # Colon removed from these definitions.
   my $NameStartCharClassBody = "a-zA-Z_\\xC0-\\xD6\\xD8-\\xF6\\xF8-\\x{2FF}\\x{370}-\\x{37D}\\x{37F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{2070}-\\x{218F}\\x{2C00}-\\x{2FEF}\\x{3001}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}";
   my $NameCharClassBody = "${NameStartCharClassBody}\\-.0-9\\xB7\\x{300}-\\x{36F}\\x{203F}-\\x{2040}";
   my $Name = "(?:[$NameStartCharClassBody][$NameCharClassBody]*)";

   $NCName = $Name;
   $QName = "$NCName(?::$NCName)?";
   $NCWild = "${NCName}:\\*";
}

{
   my $doc = XML::XPath->new(filename => $ARGV[0]);
   say $doc->findvalue('/employees/employee[@age="10"]/name');
   say $doc->findvalue('/employees/employee[@age="30"]/工作');
}

输出:

$ ./a a.xml
mac
教师

答案 1 :(得分:3)

您应始终毫无例外发布您运行的实际代码,而不是像以下那样的乱码:

use XML::XPath->new;

现在,关于这个问题,我相当肯定这是由this line in XML/XPath/Parser.pm

引起的
$NCName = '([A-Za-z_][\w\\.\\-]*)';

由于我不熟悉的原因,要求将元素的第一个字符限制为英文字母集_。这是一个简单的测试用例:

#!/usr/bin/env perl

use v5.14;
use strict;
use warnings;

use utf8;
use open qw(:std :encoding(UTF-8));

use XML::XPath;

my $xp = XML::XPath->new(ioref => \*DATA );

my $good_path = '/employees/employee[@age="30"]/yağcı';
my $bad_path = '/employees/employee[@age="30"]/şımarık';

say $xp->findvalue($good_path);

say $xp->findvalue($bad_path);

__DATA__
<?xml version="1.0" encoding="utf-8" ?>
<employees>
    <employee age="30">
        <şımarık>değil</şımarık>
        <yağcı>değil</yağcı>
    </employee>
</employees>

输出:

C:\...\> perl x.pl
değil
Query:
/employees/employee[@age="30"]/şımarık...
..............................^^^
Invalid query somewhere around here (I think)

如果我将该模式更改为:

$NCName = '(\w[\w\\.\\-]*)';

我得到了输出:

C:\...\> perl x.pl
değil
değil

并且,使用您的原始数据,我得到:

değil
教师

进行适当的更改后。

这不是正确使用的模式,但我这样做是为了确保通过尽可能进行最小的更改来确定原因是否正确。正确的规范是in the standard

Name          ::= NameStartChar (NameChar)*

NameStartChar ::= ":" | [A-Z] | "_" | [a-z] |
                  [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] |
                  [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] |
                  [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] |
                  [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]

NameChar      ::= NameStartChar | "-" | "." | [0-9] | #xB7 |
                  [#x0300-#x036F] | [#x203F-#x2040]

Issue opened

module has been patched。您可以下载version 1.41或更高版本的测试。