sort xml

Last post 08-08-2007 3:16 PM by Diamsorn. 7 replies.

Sort Posts:

  • sort xml

    08-08-2007, 11:11 AM
    • Member
      55 point Member
    • tpiazza
    • Member since 01-30-2007, 6:24 PM
    • Posts 252

    If i have the following code -- how would i sort by the element "name"

    Dim xmlDocJobItemList As New Xml.XmlDocument()

    xmlDocJobItemList.LoadXml(responseMsgSetJobItemList.ToXMLString)

    sort name alpabetically in here then read

     

    Dim nlJobItemList As Xml.XmlNodeList = xmlDocJobItemList.DocumentElement.SelectNodes("//ItemServiceRet")

    Dim elJobItemList As Xml.XmlNode

  • Re: sort xml

    08-08-2007, 12:06 PM
    • Contributor
      2,109 point Contributor
    • Diamsorn
    • Member since 08-04-2004, 9:37 PM
    • Texas
    • Posts 384

    XSLT has a sort function built in that allows you to sort on an element.  But that means that you will need to transform your XML with XSLT and parse it that way

    <xsl:sort select="*[name() = 'name']" order="ascending"/>

     

    Dave Yancey
    My Blog
  • Re: sort xml

    08-08-2007, 1:22 PM
    • Member
      55 point Member
    • tpiazza
    • Member since 01-30-2007, 6:24 PM
    • Posts 252

    do you have a code example of how to do this

  • Re: sort xml

    08-08-2007, 1:34 PM
    • Contributor
      2,109 point Contributor
    • Diamsorn
    • Member since 08-04-2004, 9:37 PM
    • Texas
    • Posts 384

    The following site gives you a good example on how to write an XSLT using the sort function. 

    http://www.developer.com/xml/article.php/1560361

    If you have any questions on what the author is doing in the XSLT example he gives I'll be happy to answer.

     To apply the transformation in .net 2.0 you would use XSLCompiledTransform and we can help you out with that if you need it.

    Dave Yancey
    My Blog
  • Re: sort xml

    08-08-2007, 2:26 PM
    • Member
      55 point Member
    • tpiazza
    • Member since 01-30-2007, 6:24 PM
    • Posts 252

    i think i got my xsl the way it needs to be but im having trouble getting it to transform in my code

    Dim xmlDocJobItemList As New Xml.XmlDocument()

    xmlDocJobItemList.LoadXml(myxmlstring)

    What exactly am i supposed to here? this is what ive been trying thus far

    Dim myXPathDocument As XPathDocument = New XPathDocument(xmlDocJobItemList.Name)

    Dim myXslTransform As XslCompiledTransform = New XslCompiledTransform

    myXslTransform.Load("xsltztest.xslt")

    Dim settings As New XmlReaderSettings()

    settings.IgnoreWhitespace = True

    Dim reader As XmlReader = XmlReader.Create(myXPathDocument.CreateNavigator.Name, settings)

    textbox1.text = the output  -- how do i get ot there

     

  • Re: sort xml

    08-08-2007, 2:42 PM
    • Contributor
      2,109 point Contributor
    • Diamsorn
    • Member since 08-04-2004, 9:37 PM
    • Texas
    • Posts 384

    What you have to do is load your XslCompiledTransform object with the stylesheet, then set your xmldocument in the transform along with a object to output the xml to.  In this case here we are loading an xmlwriter with a StringWriter object.  This will allow us to output the transfomred XML to a string.

    Dim myXslTransform As XslCompiledTransform = new XslCompiledTransform
    Dim tempStringWriter As New StringWriter
    Dim tempXMLWriter As XMLWriter = New XMLTextWriter(tempStringWriter)
    
    myXslTransform.Load("xsltztest.xslt")
    myXSLTransform(myxmlstring, tempstring)
    
    textbox1.text = tempStringWriter.ToString()
    
     
    Dave Yancey
    My Blog
  • Re: sort xml

    08-08-2007, 2:57 PM
    • Member
      55 point Member
    • tpiazza
    • Member since 01-30-2007, 6:24 PM
    • Posts 252

    I found a link to something similar right after I posted but thanks

    how do i turn it back into xml?

    I am still having some trouble getting the xml to output something which im guessing is the xsl is not properly put together

    xml/xsl segment below -- im trying to get the name, listid, and ParentRef/FullName and sort by name -- it returns nothing -- can you see the error? 

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">


    <xsl:param name="sortBy" select="'Name'"/>
    <xsl:param name="strXPath" select="//ItemServiceRet"/>
    <xsl:template match="/">
      <xsl:apply-templates select="$strXPath">
        <xsl:sort select="*[name()=$sortBy]" order="ascending"/>
      </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="Person">
     <xsl:value-of select="Name"/>
        <xsl:value-of select="ParentRef/Fullname"/><
     
    </xsl:template>
    </xsl:stylesheet>

     

    <ItemServiceRet>

    <ListID>2A40000-1171382505</ListID>

    <TimeCreated>2007-02-13T11:01:45-05:00</TimeCreated>

    <TimeModified>2007-07-03T10:19:50-05:00</TimeModified>

    <EditSequence>1183472390</EditSequence>

    <Name>01.820 Other</Name>

    <FullName>200:01.000 GENERAL CONDITIONS:01.820 BUSINESS LICENSE:01.820 Other</FullName>

    <IsActive>true</IsActive>

    <ParentRef>

    <ListID>2A30000-1171382402</ListID>

    <FullName>200:01.000 GENERAL CONDITIONS:01.820 BUSINESS LICENSE</FullName>

    </ParentRef>

    <Sublevel>3</Sublevel>

    <SalesOrPurchase>

    <Desc>01.820 Other</Desc>

    <Price>0.00</Price>

    <AccountRef>

    <ListID>B10001-1124894555</ListID>

    <FullName>Construction in Progress:Direct Cost:Capitalized</FullName>

    </AccountRef>

    </SalesOrPurchase>

    </ItemServiceRet>

  • Re: sort xml

    08-08-2007, 3:16 PM
    Answer
    • Contributor
      2,109 point Contributor
    • Diamsorn
    • Member since 08-04-2004, 9:37 PM
    • Texas
    • Posts 384

    okay few things I see here.

     First off in your XSLT you have a lonely "<" right after your xsl:select statement for /parentref/fullname.

     Once you remove that you will get an output.  However it wont be selecting what you want.

    Your sorting in one template and then selecting values in a 2nd template but not applying that template.

    I've created another sample XSL for you to work from.   Take alook at this and see if this helps out.  It will print out the 1st <name> element and any of the <parentRef/Fullname> elements.


    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:param name="sortBy" select="'Name'"/>
    <xsl:param name="strXPath" select="//ItemServiceRet"/>
    <xsl:template match="/">
      <xsl:for-each select="$strXPath">
        <xsl:sort select="*[name()=$sortBy]" order="ascending"/>
    <xsl:value-of select="Name"/>
        <xsl:value-of select="ParentRef/FullName"/>
        </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

     
    Dave Yancey
    My Blog
Page 1 of 1 (8 items)