3

このソース ファイル (file.xml) の場合:

<article>
<story name="column">
    <runs>
        <run p="902" c="103">
            THINK ABOUT IT
        </run>
    </runs>
</story>
<story name="body">
    <runs>
        <run p="895" c="103">
            ‘
        </run>
        <run p="895" c="920">
            T
        </run>
        <run p="895" c="103">
            here is an abiding
            <eol />
            beauty which may be
            <eol />
            appreciated by those
            <eol />
            who will see things as
            <eol />
            they are and who will
            <eol />
            ask for no reward
            <eol />
            except to see.’
            <eol />
        </run>
        <run p="896" c="103">
            Vera Brittain
            <eol />
            (1893-1970)
            <eol />
        </run>
        <run p="897" c="103">
            British author
        </run>
    </runs>
</story>

'body' 属性を持つストーリー要素からすべてのテキストを取得するために、単純な PHP スクリプトに引き込みました。

<?php

$xml = simplexml_load_file( "file.xml" );

$body = $xml->xpath( "//story[@name='body']/*[not(self::eol)]" );
if( $body ){
    print_r( $body[0] );
}

?>

私の出力は、私が期待したものとほぼ同じです:

SimpleXMLElement Object
(
    [run] => Array
        (
            [0] => ‘
            [1] => T
            [2] => here is an abiding beauty which may be appreciated by those who will see things as they are and who will ask for no reward except to see.’
    
            [3] => Vera Brittain 
    (1893-1970)
    
            [4] => British author
        )
)

なんらかの理由で、これらの値にアクセスしてそれらを連結する方法が見つかりません。$body[0]、などを解析しようとしまし$body[0]->runたが、期待どおりの結果が得られません。

要するに、次の値を持つ文字列を取得する必要があります。

‘There is an abiding 
beauty which may be 
appreciated by those 
who will see things as 
they are and who will 
ask for no reward 
except to see.’
     
Vera Brittain 
(1893-1970) 
British author

前もって感謝します!

4

1 に答える 1