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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据