Sweety
Sweety

Reputation: 423

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.

Upvotes: 0

Views: 143

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243579

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()

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 60037

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()]'));

?>

Upvotes: 0

Related Questions