在XML - VB.net上应用XSL转换 [英] Apply XSL transformation on XML - VB.net

查看:69
本文介绍了在XML - VB.net上应用XSL转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在加载XML文档,然后我加载XSLT代码,我使用XSL对加载的XML应用转换,然后我想将转换的XML保存在已加载的XML的位置,基本上替换了对象。这就是我的尝试:



  Dim  myXmlDoc 作为  XmlDocument()
Dim xsltTrans 作为 XslCompiledTransform()
Dim tempMyXmlDoc 作为 XmlDocument()
' 加载从数据库中获取的xml字符串'
myXmlDoc.LoadXml(xmlTilbudTilWord)
' 加载样式表'
xsltTrans.Load(xsltTransformerCode.transformationXSLTcode())

Dim xmlNavigator As XPathNavigator = myXmlDoc.CreateNavigator()
Dim xmlWriter As XmlWriter = xmlNavigator.ReplaceRange(xmlNavigator)

' 这里我想去的地方将第一个DOC转换为TEMPORARY ONE
xsltTrans.Transform(myXmlDoc, Nothing ,tempMyXmlDoc)
myXmlDoc.Load( tempMyXmlDoc)





我已经关注了MSDN的教程,但是它不起作用,我真的不明白导航器 - >作家连接。



我没有正确使用Transform()方法,但我不知道如何使用编写器和导航器,因为我必须使用它们...

解决方案

这是一种方法。在这种情况下,我使用的是Altova库,因为对于这个客户端我需要XSLT2支持。



 使用系统; 
使用 System.Text.RegularExpressions;
使用 Altova.AltovaXML;

namespace RedCell.App.XsltTransformer
{
/// < 摘要 >
/// Transformer完成工作使用XSLT将输入转换为输出。
/// < / summary >
public class Transformer
{
#region Initialization
/// < < span class =code-summarycomment>摘要 >
// / 初始化< 的新实例 cref =Transformer/ & gt; 类。
/// < / summary >
public Transformer()
{
this .Input = ;
this .Xslt = ;
.Output = ;
}

/// < 摘要 >
/// 初始化< 参见 cref =Transformer/ > 类。
/// < / summary >
/// < param < span class =code-summarycomment> name =input > 输入。< / param >
/// < param name =xslt > XSLT。< / param >
public Transformer( string input, string xslt)
{
this .Input = input;
this .Xslt = xslt;
}
#endregion

#region属性
/// < ; 摘要 >
/// 获取或设置输入。
/// < / summary >
/// < value < span class =code-summarycomment>> 输入。< / value >
public string 输入{获取; set ; }

/// < 摘要 >
/// 获取或设置XSLT。
/// < / summary >
/// < value < span class =code-summarycomment>> XSLT。< / value >
public string Xslt { get ; set ; }

/// < 摘要 >
/// 获取或设置输出。
/// < / summary >
/// < value < span class =code-summarycomment>> 输出。< / value >
public string 输出{获取; set ; }
#endregion

#region方法
/// < 摘要 >
/// 转换此实例。
/// < / summary >
public void Transform()
{
// < span class =code-comment>完整性检查
如果 string .IsNullOrEmpty(输入))
throw new ArgumentNullException( Entrée);
if string .IsNullOrEmpty(Xslt))
throw new ArgumentNullException( XSLT);

// 从输入中删除DTD。
输入= Regex.Replace(输入, <!DOCTYPE。+?> ,RegexOptions.Singleline);

// 转换!
var app = new ApplicationClass();
XSLT2 xslt2 = app.XSLT2;
xslt2.XSLFromText = Xslt;
xslt2.InputXMLFromText =输入;
输出= xslt2.ExecuteAndGetResultAsString();
}
#endregion
}
}


文档中的示例似乎非常简单:



  ' 加载样式表。 
Dim xslt 作为 XslCompiledTransform()
xslt.Load( output.xsl

' 执行转换并将结果输出到文件。
xslt.Transform( books.xml books.html





转换方法对其他类型的输入和输出有很多重载。


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...

解决方案

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
    }
}


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.


这篇关于在XML - VB.net上应用XSL转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆