1

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>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!

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Matthew, as a quick followup, can you let me know if I can just put this type of thing in my PHP file, or is this a new file?
Here's a quick intro into using xslt with php5. tonymarston.net/php-mysql/xsl.html
Awesome!! If I had more reputation, I'd upboat it, but for now, I'll just be able to do this :-P. Thanks!!
Thanks for the info, but it looks like I can't use XSL at all... any other tips?
0
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
        }
}

1 Comment

Can't use this because I have over 6 comparisons to make

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.