选择只有一个节点,并在PHP上移动 [英] select only one node and move on php

查看:119
本文介绍了选择只有一个节点,并在PHP上移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自Echonest API Spotify URI的歌曲列表,但它增加了更多的歌曲,然后我需要。我只需要其中的一个而不是全部,但是我想继续这样做20次。所以我只想得到节点内的第一个轨道,然后进入下一个。



是我从$ 获取信息的xml文件。



这是我使用的PHP:

< iframe src =https://embed.spotify.com/?uri=spotify:trackset:Playlist based on Rihanna:<?php 
$ completeurl =http ://developer.echonest.com/api/v4/playlist/static API_KEY = FILDTEOIK2HBORODV&安培;艺术家=蕾哈娜&安培;格式= XML&安培;结果= 20&安培;类型=艺术家无线电&安培;斗=磁道&安培;斗= ID:Spotify的-WW ;
$ xml = simplexml_load_file($ completeurl);
$ i = 1;
foreach($ xml-> xpath('// songs')as $ playlist){
$ spotify_playlist = $ playlist-> foreign_id;
$ spotify_playlist2 = str_replace(spotify-WW:track:,'' ,$ spotify_playlist);
回声$ spotify_playlist2,;
if($ i ++ == 10)break;

?>width =300height =380frameborder =0allowtransparency =truestyle =float:right>< / iframe>


解决方案

您可以通过计算结果来检索歌曲的数量:

  $ numberOfSongsElements = count($ xml-> xpath('// songs')); 

$ b $ p
$ b $ p

















  $ playlistNumber = 1; 
if($ playlistNumber> $ numberOfSongsElements){
throw new Exception('Not enough< songs> elements ');
}
$ songsElement = $ xml-> xpath(// songs [$ playlistNumber]);

通过在Xpath谓词中使用位置号:

$ p $ // songs [ 1] - 缩略形式://歌曲[position()= 1]
//歌曲[2]
//歌曲[3]
...

您可以直接选择节点y你对此感兴趣,不管它是先( 1 )还是其他数字,甚至是最后一个( last()) 。请参阅 2.4谓词 4.1节点集函数

希望这有帮助。正如已经评论的那样,你的问题不是那么清楚。我希望计数和编号访问将允许您至少以编程方式选择要查找的元素。


I have a list of songs from Echonest API with Spotify URI but it adds many more songs then I need. I only need one of them not all of them but I want to keep on doing this 20 times. So I only want to get the first track inside the node and move on to the next.

Here is the xml file I get the info from

This is the PHP I use:

<iframe src="https://embed.spotify.com/?uri=spotify:trackset:Playlist based on Rihanna:<?php 
$completeurl = "http://developer.echonest.com/api/v4/playlist/static?api_key=FILDTEOIK2HBORODV&artist=Rihanna&format=xml&results=20&type=artist-radio&bucket=tracks&bucket=id:spotify-WW"; 
$xml = simplexml_load_file($completeurl); 
$i = 1;
foreach ($xml->xpath('//songs') as $playlist) {
    $spotify_playlist = $playlist->foreign_id;
    $spotify_playlist2 = str_replace("spotify-WW:track:",'',$spotify_playlist);
    echo  "$spotify_playlist2,"; 
    if ($i++ == 10) break;
}
?>" width="300" height="380" frameborder="0" allowtransparency="true" style="float: right"></iframe>

解决方案

You can retrieve the number of songs by counting the result:

$numberOfSongsElements = count($xml->xpath('//songs'));

This should allow to find out if the song you want to retrieve is in there. For example:

$playlistNumber = 1;
if ($playlistNumber > $numberOfSongsElements) {
    throw new Exception('Not enough <songs> elements');
}
$songsElement = $xml->xpath("//songs[$playlistNumber]");

By using the position number inside the Xpath predicate:

//songs[1]  --  abbreviated form of: //songs[position()=1]
//songs[2]
//songs[3]
...

You can directly select the node you're interested in, be it first (1) or some other number or even last (last()). See 2.4 Predicates and 4.1 Node Set Functions.

Hope this is helpful. As already commented, your question was not that clear. I hope the counting and numbered access will allow you to select the element you're looking for at least programmatically.

这篇关于选择只有一个节点,并在PHP上移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆