Consider I have a file which contains the following data:
考虑我有一个包含以下数据的文件:
...
<wsdl:message>
...
</wsdl:message>
<wsdl:message>
...
</wsdl:message>
<wsdl:message>
...
</wsdl:message>
...
What would be an appropriate regex to get all data between the first <wsdl:message>
and the last </wsdl:message>
?
在第一个
Or alternatively, (as has been suggested), an appropriate XPath solution.
或者,(如已建议的那样),一个适当的XPath解决方案。
The main idea is that I would like to find and replace that portion of data.
主要想法是我想找到并替换那部分数据。
2 个解决方案
#1
1
Just use:
只需使用:
//x:message/node()
where the prefix x:
is bound (in the programming language that is hosting XPath) to the wsdl namespace -- this is often called "registering a namespace").
其中前缀x:绑定(以托管XPath的编程语言)到wsdl命名空间 - 这通常称为“注册命名空间”)。
Alternatively, if you aren't able to register this namespace, use:
或者,如果您无法注册此命名空间,请使用:
//*[local-name()='message'
and
namespace-uri() = 'http://schemas.xmlsoap.org/wsdl/'
]/node()
#2
0
The XPath
XPath
/*/wsdl:message[fn:position() > 1 and fn:position() < fn:last()]
should do it.
应该这样做。
EDIT
编辑
For XPath 1.0 (and code for PHP)
对于XPath 1.0(和PHP的代码)
<?php
$string = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:wsdl="/">
<wsdl:message>
One
</wsdl:message>
<wsdl:message>
Two
</wsdl:message>
<wsdl:message>
Three
</wsdl:message>
<wsdl:message>
Four
</wsdl:message>
<wsdl:message>
Five
</wsdl:message>
<wsdl:message>
Six
</wsdl:message>
</root>
XML;
print $string;
$xml = new SimpleXMLElement($string);
print_r($xml->xpath('/*/wsdl:message[position() > 1'.
' and position() < last()]'));
?>
#1
1
Just use:
只需使用:
//x:message/node()
where the prefix x:
is bound (in the programming language that is hosting XPath) to the wsdl namespace -- this is often called "registering a namespace").
其中前缀x:绑定(以托管XPath的编程语言)到wsdl命名空间 - 这通常称为“注册命名空间”)。
Alternatively, if you aren't able to register this namespace, use:
或者,如果您无法注册此命名空间,请使用:
//*[local-name()='message'
and
namespace-uri() = 'http://schemas.xmlsoap.org/wsdl/'
]/node()
#2
0
The XPath
XPath
/*/wsdl:message[fn:position() > 1 and fn:position() < fn:last()]
should do it.
应该这样做。
EDIT
编辑
For XPath 1.0 (and code for PHP)
对于XPath 1.0(和PHP的代码)
<?php
$string = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:wsdl="/">
<wsdl:message>
One
</wsdl:message>
<wsdl:message>
Two
</wsdl:message>
<wsdl:message>
Three
</wsdl:message>
<wsdl:message>
Four
</wsdl:message>
<wsdl:message>
Five
</wsdl:message>
<wsdl:message>
Six
</wsdl:message>
</root>
XML;
print $string;
$xml = new SimpleXMLElement($string);
print_r($xml->xpath('/*/wsdl:message[position() > 1'.
' and position() < last()]'));
?>