jjzjj

PHP:按属性过滤 XML?

coder 2024-07-03 原文

我正在尝试按参数进行某种排序。我有这样的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Name>Logos</Name>
  <Slides>
    <Item file="/upload/portfolio/22.jpg" lvl="1" designer="0001" theme="outline">Destiption one</Item>
    <Item file="/upload/portfolio/23.jpg" lvl="1" designer="0002" theme="drawn">Destiption 2</Item>
    <Item file="/upload/portfolio/24.jpg" lvl="2" designer="0003" theme="outline">Destiption 3</Item>
    <Item file="/upload/portfolio/26.jpg" lvl="2" designer="0004" theme="drawn">Destiption 4</Item>
    <Item file="/upload/portfolio/27.jpg" lvl="1" designer="0003" theme="outline">Destiption 5</Item>
    <Item file="/upload/portfolio/28.jpg" lvl="3" designer="0003" theme="outline">Destiption 6</Item>
  </Slides>
</Root>

PHP:

$result = $xml->Slides->xpath('Item');//get all items
$search1 = $result[0]->xpath('Item[@lvl="1"]');//failed(((

如何按属性搜索?这个想法是输出所有具有所需属性的 Item

最佳答案

问题/标题的答案(按属性过滤 XML):

您可以使用 The SimpleXMLElement class :

// xml example
$foo = '<?xml version="1.0" encoding="utf-8"?>
    <Root>
        <File type="1">File 1</File>
        <File type="2">File 2</File>
        <File type="1">File 3</File>
    </Root>';

$xml = new SimpleXMLElement($foo);
// get all 'File' elements with attribute 'type' = '1'
$search1 = $xml->xpath('File[@type="1"]');
//                      ^^^^   ^^
//                   Element   Attribute

然后,$search1 将有文件 File 1File 3



对于 OP 的特定情况: 无需先获取所有项目。您可以像这样直接使用它:

$result = $xml->Slides->xpath('Item[@lvl="1"]'); //get 'Item'-s with 'lvl' '1'

所以,print_r($result); 输出:

Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [file] => /upload/portfolio/22.jpg
                    [lvl] => 1
                    [designer] => 0001
                    [theme] => outline
                )

            [0] => Destiption one
        )

    [1] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [file] => /upload/portfolio/23.jpg
                    [lvl] => 1
                    [designer] => 0002
                    [theme] => drawn
                )

            [0] => Destiption 2
        )

    [2] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [file] => /upload/portfolio/27.jpg
                    [lvl] => 1
                    [designer] => 0003
                    [theme] => outline
                )

            [0] => Destiption 5
        )

)

请注意,它返回以 222327 结尾的 url(以 lvl 结尾的 url) = 1)


如果您查看您的代码,它有 $result[0]->xpath...$result 已经是项目列表,所以你会得到第一个项目 $result[0] (print_r($result[0]) ;):

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [file] => /upload/portfolio/22.jpg
            [lvl] => 1
            [designer] => 0001
            [theme] => outline
        )

    [0] => Destiption one
)

完整代码(复制/粘贴运行):

$foo = '<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Name>Logos</Name>
  <Slides>
    <Item file="/upload/portfolio/22.jpg" lvl="1" designer="0001" theme="outline">Destiption one</Item>
    <Item file="/upload/portfolio/23.jpg" lvl="1" designer="0002" theme="drawn">Destiption 2</Item>
    <Item file="/upload/portfolio/24.jpg" lvl="2" designer="0003" theme="outline">Destiption 3</Item>
    <Item file="/upload/portfolio/26.jpg" lvl="2" designer="0004" theme="drawn">Destiption 4</Item>
    <Item file="/upload/portfolio/27.jpg" lvl="1" designer="0003" theme="outline">Destiption 5</Item>
    <Item file="/upload/portfolio/28.jpg" lvl="3" designer="0003" theme="outline">Destiption 6</Item>
  </Slides>
</Root>';

$xml = new SimpleXMLElement($foo);
$search1 = $xml->Slides->xpath('Item[@lvl="1"]');
echo '<pre>';
print_r($search1);
echo '</pre>';

关于PHP:按属性过滤 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35846288/

有关PHP:按属性过滤 XML?的更多相关文章

  1. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  2. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  3. ruby-on-rails - 在混合/模块中覆盖模型的属性访问器 - 2

    我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah

  4. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  5. ruby - Nokogiri 剥离所有属性 - 2

    我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog

  6. ruby-on-rails - Rails 模型——非持久类成员或属性? - 2

    对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs

  7. ruby-on-rails - 事件管理员日期过滤器日期格式自定义 - 2

    是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s

  8. ruby-on-rails - 在 Controller 中干净地处理多个过滤器(参数) - 2

    我有一个名为Post的类,我需要能够适应以下场景:如果用户选择了一个类别,则只显示该类别的帖子如果用户选择了一种类型,则只显示该类型的帖子如果用户选择了一个类别和类型,则只显示该类别中该类型的帖子如果用户没有选择任何内容,则显示所有帖子我想知道我的Controller是否不可避免地会因大量条件语句而显得粗糙...这是我解决此问题的错误方法-有谁知道我如何才能做到这一点?classPostsController 最佳答案 您最好遵循“胖模型,瘦Controller”的惯例,这意味着您应该将这种逻辑放在模型本身中。Post类应该能够报告

  9. ruby - Chef Ruby 遍历 .erb 模板文件中的属性 - 2

    所以这可能有点令人困惑,但请耐心等待。简而言之,我想遍历具有特定键值的所有属性,然后如果值不为空,则将它们插入到模板中。这是我的代码:属性:#===DefaultfileConfigurations#default['elasticsearch']['default']['ES_USER']=''default['elasticsearch']['default']['ES_GROUP']=''default['elasticsearch']['default']['ES_HEAP_SIZE']=''default['elasticsearch']['default']['MAX_OP

  10. ruby - 获取数组中的值并最小化某个类属性的最优雅的方法是什么? - 2

    假设我有以下类(class):classPersondefinitialize(name,age)@name=name@age=ageenddefget_agereturn@ageendend我有一组Person对象。是否有一种简洁的、类似于Ruby的方法来获取最小(或最大)年龄的人?如何根据它对它们进行排序? 最佳答案 这样做会:people_array.min_by(&:get_age)people_array.max_by(&:get_age)people_array.sort_by(&:get_age)

随机推荐