Hi,

I'm currently writing a PHP script that allows users to access a resonably big XML database. One of the functions the scripts need to feature is sort the information contained in the XML file. What would be the most efficient way to achieve this?

Any help is appreciated

Edsko

    10 days later

    What do you mean by sort? XML is always in a "well formed document" format.

      Yeah, obviously it is well-formed. The problem is however to sort the information contained in the XML file (eg. alphabetically). Like an SQL query ORDER BY ...

      Edsko

        2 months later

        Hi
        For that you have to use XSL and xml

        In xsl there is function called sort
        which take three parameter 1)TagName
        2)Order 3)DataType
        This will help you for sorthing your xml file

        Milind

          3 months later

          But I want the input as a XML file and the output also should be a xml file with the sorted nodes

            I think XML is unsorted by definition, much like an SQL database. You shouldn't rely on the ordering of the XML file itself.

              8 months later

              xslt can output xml as well as html
              you can use the xslt sort example:

              Given the XML
              <document>
              <section num="3"/>
              <section num="4"/>
              <section num="2"/>
              <section num="1"/>
              </document>

              <xsl:output method="xml"/>
              <xsl:template match="/">
              <xsl:copy>
              <xsl:apply-templates select="section">
              <xsl:sort data-type="number" order="ascending" select="section[@num]"/>
              </xsl:apply-templates>
              </xsl:copy>
              </xsl:template>

              <xsl:template match="node()|@*">
              <xsl:copy>
              <xsl:apply-templates match=
              </xsl:copy>
              </xsl:template>

              should give return an xml file with the section elements ordered numberically in ascending order by the num attribute of each of those elements when passed through a xslt parser like salbot.

              Hope this helps

              news_friend wrote:

              But I want the input as a XML file and the output also should be a xml file with the sorted nodes

                Write a Reply...