Results 1 to 2 of 2

Thread: Sorting an XMLDocument or NodeList *RESOLVED*

  1. #1

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Sorting an XMLDocument or NodeList *RESOLVED*

    How can you sort the results of a SelectNodes result in an XMLDocument? Do I need to transform, with XSL, the document before loading it? Is that the only way? Or has anyone done this with the XPathNavigator object?
    Last edited by Edneeis; Jan 7th, 2004 at 12:11 PM.

  2. #2

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Well I didn't see a way to do this so I learned how to do XSL Transformations, if thats what they are called. Here is what I came up with:
    VB Code:
    1. Dim ms As New IO.MemoryStream
    2.         Dim sort As New Xsl.XslTransform
    3.         sort.Load(Server.MapPath("\traffic.xsl"))
    4.         sort.Transform(New XPathDocument(Server.MapPath("\traffic.xml")), Nothing, ms, New XmlUrlResolver)
    5.         ms.Position = 0
    6.         Dim doc As New XmlDocument
    7.         doc.Load(ms)
    8.         ms.Close()

    Here is the xsl file the attribute name is where I am sorting:
    VB Code:
    1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    2.     <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
    3.     <xsl:template match="/">
    4.         <xsl:apply-templates>
    5.             <xsl:sort select="@name" order="ascending" />
    6.         </xsl:apply-templates>
    7.     </xsl:template>
    8.     <xsl:template match="node()|@*">
    9.         <xsl:copy>
    10.             <xsl:apply-templates select="node()|@*">
    11.                 <xsl:sort select="@name" order="ascending" />
    12.             </xsl:apply-templates>
    13.         </xsl:copy>
    14.     </xsl:template>
    15. </xsl:stylesheet>

    Here is the xml:
    VB Code:
    1. <?xml version="1.0" standalone="yes" ?>
    2. <Traffic>
    3.     <TotalHits>1525</TotalHits>
    4.     <Platform name="VisualBasic6" hits="227" />
    5.     <Platform name="VisualBasicNET" hits="2709" />
    6.     <Page name="Edneeis - Control - MultiColumn ComboBox" hits="9" />
    7.     <Page name="Edneeis - Control - IconMenuExtender" hits="3" />
    8.     <Page name="Edneeis - Examples" hits="421" />
    9.     <Page name="Edneeis - Controls" hits="355" />
    10.     <Page name="Edneeis - Tutorials" hits="26" />
    11.     <Page name="Edneeis - Home" hits="1030" />
    12.     <Page name="Edneeis - Tutorial - How to pass Objects to Forms through the Constructor"
    13.         hits="2" />
    14.     <Page name="Edneeis - Tutorial - How to process a Method Asynchronously with BeginInvoke"
    15.         hits="5" />
    16.     <Page name="Edneeis - Tutorial - How to return Custom Objects as a Form Result" hits="3" />
    17.     <Page name="Edneeis - Tutorial - Showing Icons in Menus" hits="2" />
    18.     <Page name="Edneeis - Tutorial - Import/Export Images with a Database" hits="5" />
    19.     <Page name="Edneeis - Control - ImageComboBox" hits="2" />
    20.     <Page name="Edneeis - Control - FocusHighlighter" hits="6" />
    21.     <Page name="Edneeis - Control - Progress Panel" hits="2" />
    22.     <Page name="Edneeis - Example - Splash or Message Dialog" hits="1" />
    23.     <Page name="Edneeis - Example - Tip Of The Day" hits="1" />
    24. </Traffic>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width