简体   繁体   English

基于XML属性值的PHP If / Then

[英]PHP If/Then based on XML Property Values

I have an XML file that I'm reading as a SimpleXML Element. 我有一个XML文件,该文件正在读取为SimpleXML元素。 In that file I have the following elements: 在该文件中,我具有以下元素:

<files>
  <file>
    <type>GIF</type>
    <url>http://dl.server.com/1.GIF</url>
  </file>
  <file>
    <type>JPG</type>
    <url>http://dl.server.com/2.JPG</url>
  </file>
  <file>
    <type>TIF</type>
    <url>http://dl.server.com/1.TIF</url>
  </file>
  <file>
    <type>EPS</type>
    <url>http://dl.server.com/1.EPS</url>
  </file>
  <file>
    <type>LEPS</type>
    <url>http://dl.server.com/2.EPS</url>
  </file>
</files>

I was using a foreach() to loop through and based on the element's value, was performing an action. 我正在使用一个foreach()循环并根据元素的值进行操作。 Now I need to look at all the files->file->type values and if "LEPS" exists, use that URL, otherwise if "EPS" exists, use that URL, and if neither exists, do nothing. 现在,我需要查看所有files-> file-> type值,如果“ LEPS”存在,请使用该URL,否则,如果“ EPS”存在,请使用该URL,如果都不存在,则不执行任何操作。

My struggle is with the XML node/element/property terms and not being able to find a way to query if files->file->type = "LEPS" is true or not. 我的难题是使用XML节点/元素/属性术语,而无法找到一种查询file-> file-> type =“ LEPS”是否正确的方法。 While I know how to check for attributes (isset(element['attributename']), I'm not sure how to check for an element with a specific property value. 虽然我知道如何检查属性(isset(element ['attributename']),但我不确定如何检查具有特定属性值的元素。

Sorry for the elementary question! 对不起基本问题!

You can use a predicate to check the <type> and those of the sibling file/types. 您可以使用谓词来检查<type>以及同级文件/类型的那些。

//files/file
[
    type='LEPS' or
    type='EPS' and not(
        following-sibling::file/type='LEPS' or
        preceding-sibling::file/type='LEPS'
    )
]/url

Example: 例:

(This example uses the DOM extension rather than SimpleXML for reasons of taste. The xpath expression itself is agnostic and can be applied equally in either.) (出于口味的考虑,此示例使用DOM扩展而不是SimpleXML。xpath表达式本身是不可知的,并且可以在两者中均等地应用。)

/* $xml = your xml string */

$dom = new DOMDocument();
$dom->loadXML($xml);
$xpath = new DOMXPath($dom);

$query = <<<'XPATH'
//files/file
[
    type='LEPS' or
    type='EPS' and not(
        following-sibling::file/type='LEPS' or
        preceding-sibling::file/type='LEPS'
    )
]/url
XPATH;

foreach ($xpath->query($query) as $node) {
    echo $dom->saveXML($node), "\n";
}

Output: 输出:

<url>http://dl.server.com/2.EPS</url>

In basic pseudo, you could, in your foreach, check your type, if it matches, store the url for later use and break out of the loop. 在基本的伪代码中,您可以在foreach中检查您的类型(如果匹配),存储该URL供以后使用并退出循环。

Something similar to 类似于

url = null
foreach node in element:
    if node->type == 'LEPS':
        url = node->url
        break
    else if node->type == 'EPS':
        url = node->url

// if url is null, no result found

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM