PHP SIMPLEXML XPATH matches multiple tags with the same substring

I am making a simple script that takes XML as input, modifies some values ​​and saves it with a different name.

XML has several namespaces, often nested.

I am trying to find a smart solution for the following problem.

I have several tags that have a common substring, for example Episode

<ext:EpisodeLong>Test</ext:EpisodeLong>
<ext:EpisodeBrief>Test</ext:EpisodeBrief>
<ext:EpisodeName>Test</ext:EpisodeName>

I want to access each tag that matches "Episode" and change the value.

Corresponding Sliced ​​XML:

<Title endDateTime="2015-05-21T04:06:48Z" providerVersionNum="1" startDateTime="2015-05-02T04:06:48Z" uriId="c5.channel5.com/TITL0000000000001360">
    <core:Ext xmlns:core="http://www.cablelabs.com/namespaces/metadata/xsd/core/1">
        <ext:LocalizableTitleExt xmlns:ext="URN:NNDS:CMS:ADI3:01"    
        <xml:lang="eng">
            <ext:EpisodeLong>Test</ext:EpisodeLong>
            <ext:EpisodeBrief>Test</ext:EpisodeBrief>
            <ext:EpisodeName>Test</ext:EpisodeName>
        </ext:LocalizableTitleExt>

All xpaths I'm trying to do fail. If I do the line below, I get 99% XML, whereas I want everything just under the heading.

$results = $xml->xpath('//*[contains(E:LocalizableTitleExt, Episode)]');
foreach ($results as $s) 
{
    echo "DEBUG, xpath has found: ".$s."<br/>";
    $elementsToRemove[] = $s;       
}   

* suppose all namespaces are already registered, E - ext, C - Core

Ideas?

Thanks in advance

ps: impossible to use DOM, only SimpleXML and XPATH

+4
1

local-name() XML. , :

$results = $xml->xpath('//*[contains(local-name(), "Episode")]');

xpath , xpath ...:

//*[contains(E:LocalizableTitleExt, Episode)]

... , <E:LocalizableTitleExt> , <Episode>.

0

Source: https://habr.com/ru/post/1589231/


All Articles