Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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

Here is one way of doing it. In this case I am using the Altova library because for this client I needed XSLT2 support.

C#
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
    }
}
 
Share this answer
 
The example from the docs seems pretty straightforward:

VB
' 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.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900