简体   繁体   English

使用正则表达式在xml中添加名称空间前缀

[英]Regular expression to add namespace prefix in xml

I am stuck at one point.I have a xml which does not have a prefix. 我被困在一点。我有一个没有前缀的xml。 I am trying to put the prefix through Regular expression. 我正在尝试通过正则表达式放置前缀。 My xml looks as below:- 我的xml如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tier="http://xxxx">
<soapenv:Header/>
<soapenv:Body>
  <tier:UnisysMB_Dispatch>
     <PayLoad>
    <XMLTransaction>
        <Source_Identifier>WFM.MSM.SR</Source_Identifier>
        </XMLTransaction></PayLoad>
  </tier:UnisysMB_Dispatch>
</soapenv:Body>
</soapenv:Envelope>

but I want the xml with namespace prefix with every tag (s1) like below: 但是我想要带有每个标签(s1)的带有名称空间前缀的xml,如下所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"   xmlns:tier="http://xxxx" s1="someurl">
<soapenv:Header/>
<soapenv:Body>
  <tier:UnisysMB_Dispatch>
     <s1:PayLoad>
    <s1:XMLTransaction>
        <s1:Source_Identifier>WFM.MSM.SR</s1:Source_Identifier>
        <s1:/XMLTransaction></s1:PayLoad>
  </tier:UnisysMB_Dispatch>
</soapenv:Body>
</soapenv:Envelope>

I have tried this search regex ="(<\\/?)[a-z0-9]+"; 我已经尝试过此搜索正则表达式=“(<\\ /?)[a-z0-9] +”; replace =s1:"$1" but not working.. 替换= s1:“ $ 1”,但不起作用。

any help please?? 有什么帮助吗?

Replace (</?)(\\w+)> with $1s1:$2> . $1s1:$2>替换(</?)(\\w+)> $1s1:$2> Now this should only work fine in your IDE. 现在,这只能在您的IDE中正常工作。

For the first line you have to use another regex. 对于第一行,您必须使用另一个正则表达式。 (<soapenv:Envelope.*?)> by $1 s1="someUrl"> . (<soapenv:Envelope.*?)>通过$1 s1="someUrl">

I agree with other comments, that regex parsing is not the best way to extract information from XML. 我同意其他意见,即正则表达式解析不是从XML提取信息的最佳方法。 Actually this is not parsing, but replacing. 实际上,这不是解析,而是替换。 This should work for small examples. 这应该适用于一些小例子。

If you are in need for a robust solution that works for a variable number of WSDLs, you should switch to an XML parser/rewriter. 如果需要适用于可变数量WSDL的可靠解决方案,则应切换到XML解析器/重写器。 I think that some XML-Tools support your problem out of the box. 我认为某些XML工具可以立即解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM