dsnnvpobaljihv3490 2013-05-21 11:23
浏览 41
已采纳

使用XPath指定自定义前缀时如何解决命名冲突?

I'm running PHP 5.4.14, and I'm trying to use XPath to search inside an XML document. I can't know which namespace prefix will be used for each namespace, so I define my own prefixes using registerXPathNamespace().

The problem is that if I register a new prefix for XPath (e.g. C) and the document was already using it, the XPath query won't use my prefix but the original one.

Let me show you a sample code to show this behaviour:

<?php
$body = <<<EOF                                                              
<multistatus xmlns="DAV:" xmlns:VC="urn:ietf:params:xml:ns:carddav" xmlns:C="urn:ietf:params:xml:ns:caldav" xmlns:C1="http://calendarserver.org/ns/">
 <response>                                                                 
  <href>/caldav.php/jorge/contacts/</href>                                  
  <propstat>                                                                
   <prop>                                                                   
    <current-user-principal>                                                
     <href>/caldav.php/jorge/</href>                                        
    </current-user-principal>                                               
    <resourcetype>                                                          
     <collection/>                                                          
     <VC:addressbook/>                                                      
    </resourcetype>                                                         
    <displayname/>                                                          
    <VC:addressbook-home-set>                                               
     <href>/caldav.php/jorge/</href>                                        
    </VC:addressbook-home-set>                                              
   </prop>                                                                  
   <status>HTTP/1.1 200 OK</status>                                         
  </propstat>                                                               
 </response>                                                                
</multistatus>                                                              
EOF;                                                                        

$xml = new SimpleXMLElement($body);                                         

$xml->registerXPathNamespace('C', 'urn:ietf:params:xml:ns:carddav');        
$xpresult = $xml->xpath('//C:addressbook-home-set');                        

var_dump($xpresult);                                                        

If you run it you'll see the query returns no results.

Surprisingly, if I change the registered prefix from C to any other prefix that isn't already defined, such as X, then the query works as expected:

$xml->registerXPathNamespace('X', 'urn:ietf:params:xml:ns:carddav');        
$xpresult = $xml->xpath('//X:addressbook-home-set');    

Am I doing anything wrong? Given that I can't know what prefixes will be used in advance, is it possible to set a custom prefix and make sure it won't conflict with other prefixes in the document besides using an ugly MYPROGRAMUNUSEDPREFIXC prefix?

I know I can get the prefixes and namespaces used in the document, but I'd like to have fixed XPath query strings.

FWIW DOMXPath has this problem too (checked it).

  • 写回答

1条回答 默认 最新

  • dongshan4316 2013-05-21 11:36
    关注

    Unless you really want to go into the theoretical possibility that any prefix you choose might already be in use, just use an improbable to guess one. Like for example the prefix: SJDUWISKJDU

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入