0

我需要根据其子 MajorDescription 的值对以下 XML(foreach ProgramList)进行排序

<ArrayOfProgramList xmlns="http://schemas.datacontract.org/2004/07/Taca.Resources" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ProgramList>
    <AreaOfInterests xmlns:a="http://schemas.datacontract.org/2004/07/Taca">
        <a:AreaOfInterest>
            <a:Interest>ABORIGINAL STUDIES</a:Interest>
        </a:AreaOfInterest>
    </AreaOfInterests>
    <Coop>true</Coop>
    <MajorDescription>ABORIGINAL COMMUNITY AND SOCIAL DEVELOPMENT</MajorDescription>
    <Program>ACSD</Program>
    <ProgramLocations>
        <ProgramLocation>
            <Campus>Barrie</Campus>
        </ProgramLocation>
    </ProgramLocations>
    <Term>201210</Term>
</ProgramList>
<ProgramList>
    <AreaOfInterests xmlns:a="http://schemas.datacontract.org/2004/07/Taca">
        <a:AreaOfInterest>
            <a:Interest>GRADUATE CERTIFICATE STUDIES</a:Interest>
        </a:AreaOfInterest>
        <a:AreaOfInterest>
            <a:Interest>HEALTH AND WELLNESS STUDIES</a:Interest>
        </a:AreaOfInterest>
    </AreaOfInterests>
    <Coop>false</Coop>
    <MajorDescription>ADVANCED CARE PARAMEDIC</MajorDescription>
    <Program>PARM</Program>
    <ProgramLocations>
        <ProgramLocation>
            <Campus>Barrie</Campus>
        </ProgramLocation>
    </ProgramLocations>
    <Term>201210</Term>
</ProgramList>
</ArrayOfProgramList>

我正在尝试使用 SimpleDOM 来做到这一点,因为我已经阅读了在其他 SO 问题上对 XML 进行排序的最简单方法。

我试过使用:

foreach($listxml->sortedXPath('//ArrayOfProgramList/ProgramList','//ArrayOfProgramList/ProgramList/MajorDescription') as $program){ ... }

以及各种其他类似的“排序”值,例如“@MajorDescription”、“/MajorDescription”和“.” 正如这里所建议的,如何使用 SimpleDOM sortedXPath 对节点值进行排序?但是当我用 var_dump() 检查它时,一切都返回一个空数组

我认为问题是我需要对子节点的值进行排序——这可能吗?foreach 需要在 ProgramList 上,因为我需要在每次迭代时输出 ProgramList 中所有子节点的值。

有什么建议么?我不必使用 SimpleDOM,我对任何有效的方法持开放态度 - 目前我正在迭代 AZ 数组,并且对于每个字母,迭代 ProgramList,将 MajorDescription 的第一个字母与当前字母进行比较和如果匹配则输出 - 这显然理想,只对第一个字母进行排序......

4

2 回答 2

1

您可以尝试将所有 ProgramList 元素放入一个数组中,并根据自定义函数对其进行排序。代码应如下所示:

function cmp($a, $b)
{
  return strcmp($a->MajorDescription[0],$b->MajorDescription[0])
}

$arr = $listxml->xpath("//ArrayOfProgramList/ProgramList");
usort($arr,"cmp");
于 2013-01-10T18:28:44.210 回答
0

您的原始代码有两个问题。首先是您的 XML 使用默认名称空间,并且根据设计,XPath 不支持默认名称空间,因此您必须寻找名称空间节点(例如//foo:bar,not //bar)来找到它们。如果您无法为此命名空间注册前缀(例如,如果您无法修改源 XML),您可以使用通配符//*结合与节点的命名空间和/或本地名称匹配的谓词来匹配命名空间节点。

$nsPredicate = '[namespace-uri() = "http://schemas.datacontract.org/2004/07/Taca.Resources"]';

$query = '//*[local-name() = "ArrayOfProgramList"]' . $nsPredicate
       . '/*[local-name() = "ProgramList"]' . $nsPredicate;

$orderBy = '*[local-name() = "MajorDescription"]' . $nsPredicate;

foreach ($listxml->sortedXPath($query, $orderBy) as $program)
{
    echo $program->asXML(),"\n";
}

另一个问题是您的排序标准。它应该从目标节点的上下文中写入。

于 2013-01-11T18:01:18.940 回答