Transform - Sorting xml document using vb .net

vb , vb .net , .net , xml , xslt , xsl , vb .net 2009 , framework 3.5 , transformation , transform Washington, United States
  • 8 years ago

    I need help creating a function for transforming an xml document using xslt. I am able to copy the document using xsl:copy-of, however it's not copying the root of the source xml document. As shown below in italic text, the root of the xml should be :

         <?xml version="1.0" encoding="UTF-8" ?> 
    - <!-- Sample XML file generated by XMLSpy v2008 sp1 (http://www.altova.com)
      -->
    

    * - * - -

    The xslt transformation function I have follows:

          Dim xmldocumentobject As XmlDocument
                xmldocumentobject = New XmlDocument
                Dim strfilepath As String = xmlFilePath
    
                'Load the unsorted/current Xml file
                xmldocumentobject.Load(strfilepath)
    
            'Insert function to overwrite previous unsorted xmldocument with newly sorted newxmldocument
              Dim str_sortedxmldoc As String                           
              str_sortedxmldoc = XsltSortDoc(xmldocumentobject.ToString)
    
               ' Re-Load the transformed/sorted xml document into the xml document object
              xmldocumentobject.LoadXml(str_sortedxmldoc)
              'Then save xml document in configured file path
              xmldocumentobject.Save(xmlFilePath)
    
            Public Function XsltSortDoc(ByVal sXMLDocument As String) As String
            Dim xd As XmlDocument
            Dim xdNav As XPathNavigator
            Dim tr As Xsl.XslCompiledTransform
            Dim sw As StringWriter
    
    
            xd = New XmlDocument()
            xd.LoadXml(sXMLDocument)
            xdNav = xd.CreateNavigator()
            tr = New Xsl.XslCompiledTransform()
            tr.Load(config.xsltFilePath)
            sw = New StringWriter()
            tr.Transform(xdNav, Nothing, sw)
    
            Return sw.ToString()
    
        End Function
    

    The xslt file defining the transformation follows:

      <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
      <xsl:output method="xml" indent="yes"/>
    
      <!--Copy List and all nodes, namespaces, child nodes, and text from source xml doc-->
      <!-- by using @*|*|text()|comment()|processing-instruction() or @* | node()-->
      <xsl:template match="/">
        <xsl:copy-of select="@* | node()"/>
      </xsl:template>
    
    <!--Match the Slug nodes 
      Copy the current node's top-level values(the tag and it's attributes,
             but not it's descendents)-->
      <xsl:template match="List">
        <xsl:copy>
           <!--Apply transformation to all S nodes.-->
          <xsl:apply-templates select="S">
            <!--For all Slugname attributes, sort them 
                  ascendingly according to their @SlugName attribute.-->
            <xsl:sort select="@SName" order="ascending"/>
            <!--Copy the entire node (include its descendantas)-->
           </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    </xsl:stylesheet>
    

    The above code produces the following outputed xml file in the debugger. Notice that the has been commented out by the transformation. This causes the transformation function to fail during runtime compilation. :

      <?xml version="1.0" encoding="utf-8"?>
    <!--Sample XML file generated by XMLSpy v2008 sp1 (http://www.altova.com)-->
    

    * *

    <B BID="SU">
        <O OID="MN">
      <S SName="BFirstTEST" Description="Test PDF" /> 
      <S SName="ASecondTEST" Description="Test PDF" /> 
        </O>
      </B>
    

    My IDE is Visual Basic .Net 2008. I need to reproduce the xml file with the SlugName attributes sorted in ascending order. Any help is appreciated. Thanks.

  • 8 years ago

    The root node which is being commented out is as follows:

    <List xsi:noNamespaceSchemaLocation="SlugList.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perl - The only language that looks the same before and after RSA encryption.” - Keith Bostic