免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3830 | 回复: 4

XPath路径中不能使用中文? [复制链接]

论坛徽章:
0
发表于 2017-07-25 21:50 |显示全部楼层
下面代码和xml来自网上,我加入了汉字后,就不能正确执行了。
请教如何解决!

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;

  4. use XML::XPath;
  5. use utf8;
  6. #binmode(STDOUT, ":utf8");
  7. my $xp=XML::XPath->new(filename=>"employees.xml");


  8. #测试节点
  9. print $xp->exists('/employees/employee[@age="10"]/name'),"\n";

  10. #获取节点值
  11. print $xp->findvalue('/employees/employee[@age="10"]/name'),"\n";

  12. #获取节点值
  13. print $xp->findvalue('/employees/employee[@age="30"]/工作'),"\n";

  14. #获取节点属性值
  15. print $xp->findvalue('/employees/employee[1]/@age'),"\n";

  16. #遍历节点
  17. my $nodeset=$xp->find('/employees/employee');
  18. foreach my $node ($nodeset->get_nodelist){
  19.         print $node->findvalue("country"),"\n";
  20. }
复制代码


employees.xml

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <employees>
  3.         <employee age="30">
  4.                 <name>linux</name>
  5.                 <country>US</country>
  6.                 <工作>教师</工作>
  7.         </employee>
  8.         <employee age="10">
  9.                 <name>mac</name>
  10.                 <country>US</country>
  11.         </employee>
  12.         <employee age="20">
  13.                 <name>windows</name>
  14.                 <country>US</country>
  15.         </employee>
  16. </employees>
复制代码

论坛徽章:
0
发表于 2017-07-26 10:26 |显示全部楼层
我在stackoverflow发了个贴,很快就解决了。要感谢老外的热情,也要承认他们的水平就是比我们高。
一个比较好的方案就是,直接用use XML::LibXML,而不用XML::XPath。
https://stackoverflow.com/questi ... glish-element-names
How to use Perl's XML::XPath with non-English element names?

  1. #!/usr/bin/perl

  2. use strict;
  3. use warnings;

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

  6. use feature qw( say );

  7. use XML::LibXML qw( );

  8. {
  9.    my $parser = XML::LibXML->new();
  10.    my $doc = $parser->parse_file('employees.xml');
  11.    say $doc->findvalue('/employees/employee[@age="10"]/name');
  12.    say $doc->findvalue('/employees/employee[@age="30"]/工作');
  13. }
复制代码

论坛徽章:
0
发表于 2017-07-26 10:34 |显示全部楼层
If you want to keep using the (buggy, slower, and far-less-widely used) XML::XPath, you can use the following:
如果你要坚持用哪个(有问题的、比较慢的、使用范围更小的)XML::XPath,你可以这样:
  1. #!/usr/bin/perl

  2. use strict;
  3. use warnings;

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

  6. use feature qw( say );

  7. use XML::XPath qw( );

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

  10.    # Colon removed from these definitions.
  11.    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}";
  12.    my $NameCharClassBody = "${NameStartCharClassBody}\\-.0-9\\xB7\\x{300}-\\x{36F}\\x{203F}-\\x{2040}";
  13.    my $Name = "(?:[$NameStartCharClassBody][$NameCharClassBody]*)";

  14.    $NCName = $Name;
  15.    $QName = "$NCName(?::$NCName)?";
  16.    $NCWild = "${NCName}:\\*";
  17. }

  18. {
  19.    my $doc = XML::XPath->new(filename => "employees.xml");
  20.    say $doc->findvalue('/employees/employee[@age="10"]/name');
  21.    say $doc->findvalue('/employees/employee[@age="30"]/工作');
  22. }
复制代码

论坛徽章:
0
发表于 2017-07-26 11:21 |显示全部楼层
原始的XML::XPath:arser里的tokenize方法正则匹配有问题,元素的第一个字符不允许中文,只能以英文字母为开头。

论坛徽章:
0
发表于 2017-08-09 15:09 |显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP