How to get between first and last of a specific node in an XML file?

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.

The main idea is that I would like to find and replace that portion of data.

Answers


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").

Alternatively, if you aren't able to register this namespace, use:

//*[local-name()='message' 
  and 
    namespace-uri() = 'http://schemas.xmlsoap.org/wsdl/'
    ]/node()

The XPath

/*/wsdl:message[fn:position() > 1 and fn:position() < fn:last()]

should do it.

EDIT

For XPath 1.0 (and code for 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()]'));

?>

Need Your Help

Is Random class thread safe?

java multithreading random thread-safety

Is it valid to share one instance of the Random class between multiple threads? And to call nextInt(int) from multiple threads in particular?

Jquery get id from clicked div

javascript jquery html ajax

I have a html structure like this