【问题标题】:Get XML element text, skipping certain elements, into single string获取 XML 元素文本,跳过某些元素,变成单个字符串
【发布时间】:2012-10-09 05:11:23
【问题描述】:

鉴于此源文件(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]-&gt;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

提前致谢!

【问题讨论】:

  • $body[0]-&gt;run(或$tmp=$body[0];$tmp-&gt;run)是一个数组吗?那你能implode吗?

标签: php xml xpath


【解决方案1】:

如果您能够从您的代码中运行 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

【讨论】:

  • 测试了上面的,果然,效果很好。不幸的是,我似乎无法在 Mac OS X 10.6.8 机器上使用xslt_create(),这一切都在运行……这很令人困惑,因为phpinfo() 报告:XSL => enabled,libxslt Version => 1.1 .24,针对 libxml 版本 => 2.7.3 编译的 libxslt,EXSLT => 启用,libexslt 版本 => 1.1.24 我想这是一个完全独立的问题。所以现在我仍在努力使用 xpath 获得结果,并试图让 xslt 在 OS X 的默认 PHP 5.3.8 安装下工作。
  • 我无法帮助您解决这个问题。也许您可以创建另一篇文章来帮助您在您的环境中运行 XSLT。
猜你喜欢
  • 2014-06-11
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-22
  • 1970-01-01
相关资源
最近更新 更多