开发者

Sorting an XML on multiple attributes WITHOUT XSLT

Updated info:

Previously, I asked how to sort XML based on several attributes of each element. The answer there was valid (which is why I am creating a new question), but I found that I am under restrictions where my version of PHP does not have the XSLT library installed, and my IT will not install it.

I found a way to make my XML an associative array, but that seems really tedious to sort through.

Also, I have several levels of sorting (around 6); since there are so many, I cannot implement the other answer below.

So, my final question is: is it possible to sort an XML without using XSLT? Every time I Google it, all the sites show how to use XSLT...

Thanks again, everyone!


OLD INFO::::::I've done a whole bunch of research between DOM and XSL and can't seem to find a way to sort on multiple elements. I've successfully generated my XML using DOM, but now I would like to sort based on multiple values.

I would prefer to have another child under the <item> perhaps titled <Priority>, or I could put it in the <item priority=__>

For simplicity, here is a dumbed down version of what I am trying to do:

XML beforehand:

<root>
    <item>
        <attr1>2</attr1>
        <attr2>50</attr2>
    </item>
    <item>
        <attr1>1</attr1>
        <attr2>100</attr2>
    </item>
    <item>
        <attr1>2</attr1>
        <attr2>10</attr2>
    </item>
    <item>
        <attr1>2</attr1>
        <attr2>50</attr2>
    </item>
    <item>
        <attr1&g开发者_如何学Pythont;1</attr1>
        <attr2>75</attr2>
    </item>
    <item>
        <attr1>2</attr1>
        <attr2>1</attr2>
    </item>
</root>

I'd like to find a way to make sort first on attr1, and if they're the same, then sort on attr2. As I said before, I don't care how I sort, nor do I care if we use the attribute within an element like <item priority = ___>, a new element for each <item>, or an entirely new xml file. I am just so lost as to what I should do next; I've been using PHP up to this point, but could switch to something else. Thanks in advance!


It can be done with xslt via the sort element and concat() if you would prefer that route.

    <xsl:template match="/root">
        <xsl:apply-templates>
            <xsl:sort select="concat(attr1,attr2)"/>
        </xsl:apply-templates>
    </xsl:template>

EDIT for the No XSLT clause

Another way to do it would be to load all the elements into a collection of item objects, with attr1 and attr2 properties, etc., and then sort the collection by your rules, you could then write it back out to xml in it's newly sorted order.


function getval($i) {
    global $feeditem;
    return $feeditem->getElementsByTagName($i)->item(0)->nodeValue;
}

$info = "";

$xmldoc = new DOMDocument();
$xmldoc->load('path_to_xml_file_here.xml');
foreach ($xmldoc->getElementsByTagName('item') as $feeditem){
        if(getval('attr1') == getval('attr2')) {
            #blablabla
        } else {
            #blablabla
        }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜