获取XML元素文本,跳过某些元素,转换为单个字符串

时间:2012-10-09 05:11:24

标签: php xml xpath

鉴于此源文件(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”的story元素中的所有文本:

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

提前致谢!

1 个答案:

答案 0 :(得分: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