gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:36:30 25 4
gpt4 key购买 nike

给定此源文件 (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

提前致谢!

最佳答案

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

关于php - 获取 XML 元素文本,跳过某些元素,将其转换为单个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12793181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com