【问题标题】:XML complex Parsing [duplicate]XML复杂解析[重复]
【发布时间】:2012-10-14 15:23:52
【问题描述】:

可能重复:
distinct in Xpath?

我正在尝试使用 PHP 从我的 XML 文件中提取数据。我想从我的 XML 文件中基于WebCategory 获得唯一的ProductRange。但是下面编写的 PHP 代码会产生重​​复/重复的结果。我不知道我在哪里犯错!这是代码:

XML 代码:

<?xml version="1.0" standalone="yes"?>
<Rows> 
<Row Code="10000" Name="HTC Wildfire S-A510E " ProductRange="HTC" ProductSubRange="Wildfire" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10001" Name="HTC Wildfire" ProductRange="HTC" ProductSubRange="Wildfire" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10002" Name="Samsung Galaxy S3" ProductRange="Samsung" ProductSubRange="Galaxy" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10003" Name="Samsung Galaxy S2" ProductRange="Samsung" ProductSubRange="Galaxy" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10004" Name="Samsung Galaxy S1" ProductRange="Samsung" ProductSubRange="Galaxy" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10005" Name="Samsung Galaxy Tabloid" ProductRange="Samsung" ProductSubRange="Galaxy Tabloids" WebCategory="Gadgets" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10006" Name="Apple Ipad 3" ProductRange="Apple" ProductSubRange="Tabloids" WebCategory="Gadgets" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10007" Name="Apple Iphone 4S" ProductRange="Apple" ProductSubRange="Iphone" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10008" Name="Apple Iphone 3G" ProductRange="Apple" ProductSubRange="Iphone" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10009" Name="Miscrosoft XBOX 360 Elite" ProductRange="Microsoft" ProductSubRange="XBOX" WebCategory="Consoles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10010" Name="Sony Playstation 4" ProductRange="Sony" ProductSubRange="Playstation" WebCategory="Consoles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10011" Name="Sony PSP Go" ProductRange="Microsoft" ProductSubRange="PSP" WebCategory="Consoles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10012" Name="Sony Erricsson Satio" ProductRange="Sony Ericsson" ProductSubRange="Satio Series" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10013" Name="TomTom Go Live Gl2" ProductRange="TomTom" ProductSubRange="Go Live" WebCategory="Navigation" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
</Rows>

我想从我的 XML 文件中获得基于 WebCategory 的唯一 ProductRange。但是下面编写的 PHP 代码会产生重​​复/重复的结果。我不知道我在哪里犯错了!

PHP 代码:

  <?
 $xml =  simplexml_load_string(file_get_contents('XML/products.xml'));
 $prifix = '/categories/listings/' ;
 $cat=array();
 foreach ($xml as $row) {
 $attr = $row->attributes();
 if (!in_array((string)$attr->WebCategory, $cat)){
  printf('<li>%s</li>', anchor($prifix . $attr->Code, $attr->ProductRange));
  $cat[] = (string)$attr->WebCategory;
 }
 }
 ?>

请注意: 我想根据给定的WebCategory 提取ProductRange,例如我想根据他们的Webcategory 显示所有ProductRange,就像这样选择SQL 查询:

     "select ProductRange from XML where WebCategory='Mobiles'"

它可以给我基于 XML 的不同的“ProductRange”(非重复结果),如下所示:

宏达电
三星
苹果手机

等等...我尽了最大努力,但未能使用上述编码方法生成唯一的“ProductRange”。

请纠正我的错误,并请指导我在哪里需要进行更改以获得独特的ProductRange,如上所述。

【问题讨论】:

  • xpath 查询怎么样?
  • 那个 XML 无效。您似乎缺少 Rows 的开始标签
  • @Quentin 抱歉。我在写作时忘记在 xml 中添加 。我已经按照你提到的方式进行了编辑。有​​什么办法可以避免记录重复吗?

标签: php xml


【解决方案1】:

你可以试试

$list = groupBy($xml, "WebCategory");
foreach ( $list['Mobiles'] as $product ) {
    printf('<li>%s %s</li>', $product->Code, $product->Name);
}

输出

  • 10000 HTC Wildfire S-A510E
  • 10001 HTC 野火
  • 10002 三星 Galaxy S3
  • 10003 三星 Galaxy S2
  • 10004 三星 Galaxy S1
  • 10007苹果iphone 4S
  • 10008苹果iphone 3G
  • 10012 索尼爱立信 Satio
  • 使用的功能

    function groupBy($xml, $categoryName) {
        $xml = new \SimpleXMLElement($xml);
        $category = array();
        foreach ( $xml as $row ) {
            $attr = $row->attributes();
    
            if (! isset($attr->$categoryName)) {
                trigger_error("$categoryName does not exist in XML");
                break;
            }
    
            $category[(string) $attr->$categoryName][] = $attr;
        }
        return $category;
    }
    

    See live Demo

    【讨论】:

    • 精彩而合乎逻辑的答案..谢谢爸爸。我真的很感谢你的帮助。祝福
    • 随时欢迎您 .. 还有什么我可以帮助您解决 @Bilal Khalid 的问题
    • @Baba.i 试过 $xml = simplexml_load_string(file_get_contents('XML/products.xml'));在使用 groupBy 函数之前,因为我正在嵌入外部 XML 文件,但它给了我错误。如果我使用查询字符串中的值,我需要做什么?感谢您的帮助。祝福
    • 只需使用groupBy(file_get_contents('XML/products.xml'), "WebCategory") 固定到命名空间的代码
    • @Baba.Brilliant .Bless you Baba.you are a magician.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多