簡體   English   中英

獲取XML元素文本,跳過某些元素,轉換為單個字符串

[英]Get XML element text, skipping certain elements, into single string

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

提前致謝!

如果您能夠從代碼中運行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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM