How to use Perl's XML::XPath with non-English element names? -
how can work xml::xpath when elements' names not in english?
i use strawberry perl.
i employees.xml
, train_xml.pl
web, work good.
but when when add chinese characters, following error:
wide character in die @ d:/strawberry/perl/site/lib/xml/xpath/parser.pm line 189.
query: /employees/employee[@age="30"]/工作... ..............................^^^ invalid query somewhere around here (i think)
how can solve this?
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; $xp=xml::xpath->new(filename=>"employees.xml"); print $xp->findvalue('/employees/employee[@age="10"]/name'),"\n"; $path1 = '/employees/employee[@age="30"]/工作'; print $xp->findvalue($path1),"\n";
you use xml::libxml:
#!/usr/bin/perl use strict; use warnings; use utf8; use open ':std', ':encoding(utf-8)'; use feature qw( ); use xml::libxml qw( ); { $parser = xml::libxml->new(); $doc = $parser->parse_file($argv[0]); $doc->findvalue('/employees/employee[@age="10"]/name'); $doc->findvalue('/employees/employee[@age="30"]/工作'); }
output:
$ ./a a.xml mac 教师
if want keep using (buggy, slower, , far-less-widely used) xml::xpath, can use following:
#!/usr/bin/perl use strict; use warnings; use utf8; use open ':std', ':encoding(utf-8)'; use feature qw( ); use xml::xpath qw( ); { # monkeypatch xml::xpath. package xml::xpath::parser; # colon removed these definitions. $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}"; $namecharclassbody = "${namestartcharclassbody}\\-.0-9\\xb7\\x{300}-\\x{36f}\\x{203f}-\\x{2040}"; $name = "(?:[$namestartcharclassbody][$namecharclassbody]*)"; $ncname = $name; $qname = "$ncname(?::$ncname)?"; $ncwild = "${ncname}:\\*"; } { $doc = xml::xpath->new(filename => $argv[0]); $doc->findvalue('/employees/employee[@age="10"]/name'); $doc->findvalue('/employees/employee[@age="30"]/工作'); }
output:
$ ./a a.xml mac 教师
Comments
Post a Comment