0

how with XPath order xml file?

File:

<?xml version="1.0" encoding="iso-8859-1"?>
<cars>
    <car>
        <id>1</id>
        <name>Ford</name>
    </car>
    <car>
        <id>2</id>
        <name>Audi</name>
    </car>
    <car>
        <id>3</id>
        <name>VW</name>
    </car>
</cars>

Display(result):

2
Audi

1
Ford

3
VW
1
  • some sample code what should be ordered? What you want achieve by that? Do you want to store it right after? Do you just like to display it afterwards? Be a little bit more precise! Commented Dec 21, 2010 at 17:31

2 Answers 2

2

Sorting cannot be accomplished using XPath (1.0 or 2.0), because a sort() function is a higher-order function (requires a comparator function as one of its arguments), and higher-order functions are not supported in XPath until version 3.0, which is still a working draft only.

You need to use the programming language (XSLT, C#, ..., etc.) that is hosting XPath in order to produce the sorted result.

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

6 Comments

+1 In XPath 2.0, sequences have the order of construcction, so in theory you could sort, besides that finding a good expression for this it would be a challenge...
@Dimitre: for $pos in (0 to count($sequence)-1), $item in $sequence return if (count($sequence[. lt $item])=$pos) then $item else ()
@Alejandro: Good, though you should use le, not lt. Also, this only works on item types that support le. Of course, this is O(N^2).
@Dimitre: Maybe someone cleaver could come up with a better expression... ;) Some linear calculated sequence of positions. Ah! The operator must be lt because otherwise the $pos comparison will break.
@Alejandro: It must be obvious that using lt will not sort correctly a sequence in which some items are equal.
|
0

XPath is basically a query language on XML resource like the SELECT SQL's statment is for DBMS.

You can't change an XML file. You have to get its content by querying it and save the results in an other XML file (or the same).

You can use XSL to do it simpler (XSL use XPath queries).

2 Comments

Hi, i don't wish change file. I just wish show data from A to Z. With XPath query.
In your case, /cars/car/(id|name)/text() but it is no meaning to extract data that way. Do you have more precision about the query you want?

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.