donglinxia1541
2013-03-05 11:59
采纳率: 0%
浏览 91

如何从PHP中删除XML中的所有命名空间(标记和属性)

I've recently had serious grief from XML namespaces and dealing with them effectively in PHP. Here's a sample of the worst kind of culprit:

<dc:type xsi:type="TypeName" xsi:identifier="NN">Others</dc:type>

What I successfully managed to do using preg_replace was to "un-namespace" the tags (without breaking URLs) using:

$xml = preg_replace(
  '/<(\/?)([^:" ].*):([^>\/ ].*)(\/?)>/msiU',
  '<$1$2_$3$4>',
  $x->readOuterXML()
);

# <dc_type xsi:type="TypeName" xsi:identifier="NN">Others</dc_type>

What I couldn't do - through lack of regular expression wizardry - was convert all namespaced attributes into the same format. I managed to convert the first occurence, but don't know how to set a repeatable condition. I deleted the code because it didn't work (and I can't remember what I did), but the result was like this:

<dc_type xsi_type="TypeName" xsi:identifier="NN">Others</dc_type>

Whereas what would be beautiful is this:

<dc_type xsi_type="TypeName" xsi_identifier="NN">Others</dc_type>

Are there any regex masters out there who can help?

图片转代码服务由CSDN问答提供 功能建议

我最近对XML命名空间感到非常悲痛,并在PHP中有效地处理它们。 以下是最糟糕的罪魁祸首的样本:

 &lt; dc:type xsi:type =“TypeName”xsi:identifier =“NN”&gt; Others&lt; / dc: 类型&gt; 
   
 
 

我使用preg_replace成功设法做的是使用以下方法“取消命名”标记(不破坏URL): \ n

  $ xml = preg_replace(
'/&lt;(\ /?)([^:“]。*):( [^&gt; \ /]。*)(\ /?)&gt  ; / msiU',
'&lt; $ 1 $ 2_ $ 3 $ 4&gt;',
 $ x-&gt; readOuterXML()
); 
 
#&lt; dc_type xsi:type =“TypeName”xsi:  identifier =“NN”&gt;其他&lt; / dc_type&gt; 
   
 
 

我不能做的事 - 通过缺乏正则表达式魔法 - 将所有命名空间属性转换为 相同的格式。我设法转换第一次出现,但不知道如何设置可重复的条件。我删除了代码,因为它不起作用(我不记得我做了什么),但结果就像 这个:

 &lt; dc_type xsi_type =“TypeName”xsi:identifier =“NN”&gt;其他&lt; / dc_type&gt; 
   
  
 

然而什么是美好的 iful是这样的:

 &lt; dc_type xsi_type =“TypeName”xsi_identifier =“NN”&gt;其他&lt; / dc_type&gt; 
   
  
 

有没有可以提供帮助的正则表达式大师?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doubi1797 2013-03-05 12:28
    已采纳

    To rewrite a complete XML document like renaming element or attribute names as well as changing namespace related data like xmlns attributes, you can use the expat based xml parser extension:

    This works by parsing the file and change the output on the fly. The parser invokes callback functions (so called handler) that gets the data pre-parsed, for example the elements name in form of a string and the attributes in form of an array.

    You then can change these values on the fly and output the (potentially changed) data.

    Done this way you don't need to care about regular expressions any longer (which is non-trivial for proper XML parsing).

    You can find some boilerplate code to get this started in a previous answer of mine.

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题