Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XML XSLT VB.NET
So I am loading an XML document, then I load XSLT code, I apply transformation on the loaded XML with the XSL and then I want to save the transformed XML on the place of the loaded one, basically replacing the object. This is how I am trying:
 
Dim myXmlDoc As New XmlDocument()
                Dim xsltTrans As New XslCompiledTransform()
                Dim tempMyXmlDoc As New XmlDocument()
                'load the xml string taken from the database'
                myXmlDoc.LoadXml(xmlTilbudTilWord)
                'load the stylesheet'
                xsltTrans.Load(xsltTransformerCode.transformationXSLTcode())
 
                Dim xmlNavigator As XPathNavigator = myXmlDoc.CreateNavigator()
                Dim xmlWriter As XmlWriter = xmlNavigator.ReplaceRange(xmlNavigator)
 
                'HERE IS WHERE I WANT TO TRANSFORM THE 1st DOC TO THE TEMPORARY ONE'
                xsltTrans.Transform(myXmlDoc, Nothing, tempMyXmlDoc)
                myXmlDoc.Load(tempMyXmlDoc)
 
I have followed a tutorial from MSDN but it won't work and I don't really understand the thing with the Navigator -> Writer connection.
 
I am not using the Transform() method correctly, but I don't know how to make usage of the writer and the navigator, because I have to use them...
Posted 24-May-13 1:57am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here is one way of doing it. In this case I am using the Altova library because for this client I needed XSLT2 support.
 
using System;
using System.Text.RegularExpressions;
using Altova.AltovaXML;
 
namespace RedCell.App.XsltTransformer
{
    /// <summary>
    /// Transformer does the work of transforming input into output using XSLT.
    /// </summary>
    public class Transformer
    {
        #region Initialization
        /// <summary>
        /// Initializes a new instance of the <see cref="Transformer"/> class.
        /// </summary>
        public Transformer()
        {
            this.Input = "";
            this.Xslt = "";
            this.Output = "";
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="Transformer"/> class.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="xslt">The XSLT.</param>
        public Transformer(string input, string xslt)
        {
            this.Input = input;
            this.Xslt = xslt;
        }
        #endregion
 
        #region Properties
        /// <summary>
        /// Gets or sets the input.
        /// </summary>
        /// <value>The input.</value>
        public string Input { get; set; }
 
        /// <summary>
        /// Gets or sets the XSLT.
        /// </summary>
        /// <value>The XSLT.</value>
        public string Xslt { get; set; }
 
        /// <summary>
        /// Gets or sets the output.
        /// </summary>
        /// <value>The output.</value>
        public string Output { get; set; }
        #endregion
 
        #region Methods
        /// <summary>
        /// Transforms this instance.
        /// </summary>
        public void Transform()
        {
            // Sanity checks
            if(string.IsNullOrEmpty(Input))
                throw new ArgumentNullException("Entrée");
            if (string.IsNullOrEmpty(Xslt))
                throw new ArgumentNullException("Xslt");
 
            // Strip the DTD from the input.
            Input = Regex.Replace(Input, "<!DOCTYPE.+?>", "", RegexOptions.Singleline);
 
            // Transform!
            var app = new ApplicationClass();
            XSLT2 xslt2 = app.XSLT2;
            xslt2.XSLFromText = Xslt;
            xslt2.InputXMLFromText = Input;
            Output = xslt2.ExecuteAndGetResultAsString();
        }
        #endregion
    }
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The example from the docs seems pretty straightforward:
 
' Load the style sheet.
Dim xslt As New XslCompiledTransform()
xslt.Load("output.xsl")
 
' Execute the transform and output the results to a file.
xslt.Transform("books.xml", "books.html")
 
The Transform method has lots of overloads for other types of input and output.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 24 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100