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>

我已将其放入一个简单的 PHP 脚本中,以从故事元素中获取具有“body”属性的所有文本:

<?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

2

如果您能够从您的代码运行 XSLT 1.0 样式表,这里有一些 XSLT 代码可以为您提供结果:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <xsl:apply-templates select="//story[@name='body']"/>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>
    <xsl:template match="eol">
        <xsl:text>&#13;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

对于这个输入:

<?xml version="1.0" encoding="UTF-8"?>
<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>
</article>

结果是:

‘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
于 2012-10-09T08:41:02.953 回答