我的xpath / php输出需要显示标签

时间:2009-04-13 00:09:37

标签: php xpath

我正在尝试获取一个xpath xpression,它将删除我的外部标记,但它正在删除它,加上内部标记并打印元素数据。如何让它来打印我的提示标签?

我的php看起来像这样:

<?php
#get file
$xml = simplexml_load_file('content.xml') or die ("Unable to load XML string!");

#loop through results
foreach ( $xml->xpath('//prompt') as $voiceQ) {
  echo "<h2>Thank you for visiting our site2</h2>";
  print $voiceQ;
} //for
?>

我的content.xml看起来像这样
(我正在写这个文件而且它正在丢失一些格式。我不认为这是问题):

<?xml version="1.0" encoding="UTF-8"?>
<result>
    <prompt>
        Welcome to Group Finder, Mary Lamb . We see that
        you  like the group:   JackJohnson
    </prompt>
</result>

我的输出如下:
欢迎来到Group Finder,Mary Lamb。我们看到你喜欢这个组:JackJohnson

但我希望它看起来像这样:
(只需删除结果标记):

<prompt>Welcome to Group Finder, Mary Lamb . We see that you  like the group: JackJohnson</prompt>

2 个答案:

答案 0 :(得分:1)

在SimpleXMLElement上尝试asXML方法:

<?php
$string = '<?xml version="1.0" encoding="UTF-8"?><result>
<prompt>Welcome to Group Finder, Mary Lamb . We see that you  like the group:   JackJohnson</prompt></result>';

#get file
$xml = new SimpleXMLElement($string);

#loop through results
foreach ( $xml->xpath('//prompt') as $voiceQ) {
  echo "<h2>Thank you for visiting our site2</h2>";
  print $voiceQ->asXML();
} //for
?>

答案 1 :(得分:0)

如果结果在提示符内,只需循环一点:)

  <php?
    #get file
    $xml = simplexml_load_file('content.xml') or die ("Unable to load XML string!");

    #loop through results
    foreach ( $xml->xpath('//prompt') as $voiceQ) {
      foreach($voiceQ AS $inVoice)
        {
           echo "<h2>Thank you for visiting our site2</h2>";
           print $inVoice;
         }
    } //for
    ?>