zoukankan      html  css  js  c++  java
  • Transform XML using XSLT

    Use XslTransform class in System.Xml.Xsl ;

     

    Original XML(testCases.xml):

    <?xml version="1.0" encoding="utf-8" ?>
    <testcases>
        
    <testcase id="001" bvt="yes">
            
    <inputs>
                
    <arg1>red</arg1>
                
    <arg2>blue</arg2>
            
    </inputs>
            
    <expected>purple</expected>
        
    </testcase>
    </testcases>

    Transformed XML(transformed.xml):

    <?xml version="1.0" encoding="utf-8"?>
    <allOfTestCases>
      
    <aCase caseID="001">
        
    <bvt>yes</bvt>
        
    <expRes>purple</expRes>
        
    <inputs>
          
    <input1>red</input1>
          
    <input2>blue</input2>
        
    </inputs>
      
    </aCase>
    </allOfTestCases>

    XSLT file(transformer.xslt):

    <?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"/>

        
    <xsl:template match="/">
            
    <allOfTestCases>
                
    <xsl:for-each select="//testcase">
                    
    <aCase>
                        
    <xsl:attribute name ="caseID">
                            
    <xsl:value-of select ="@id"/>
                        
    </xsl:attribute>
                        
    <bvt>
                            
    <xsl:value-of select ="@bvt"/>
                        
    </bvt>
                        
    <expRes>
                            
    <xsl:value-of select ="expected"/>
                        
    </expRes>
                        
    <inputs>
                            
    <xsl:for-each select ="inputs">
                                
    <input1>
                                    
    <xsl:value-of select ="arg1"/>
                                
    </input1>
                                
    <input2>
                                    
    <xsl:value-of select ="arg2"/>
                                
    </input2>
                            
    </xsl:for-each>
                        
    </inputs>
                    
    </aCase>
                
    </xsl:for-each>
            
    </allOfTestCases>
        
    </xsl:template>
    </xsl:stylesheet>

    Program.cs:

    using System;
    using System.Collections.Generic;
    using System.Xml.Xsl ;

    namespace TransformXML
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
                Console.WriteLine(
    "Start Transform!");
                XslTransform xst 
    = new XslTransform();
                xst.Load (
    "..\\..\\transformer.xslt");
                xst.Transform(
    "..\\..\\testCases.xml""..\\..\\transformed.xml");
                Console.WriteLine(
    "Done");
            }
        }
    }
  • 相关阅读:
    斯坦福第十六课:推荐系统(Recommender Systems)
    斯坦福第十五课:异常检测(Anomaly Detection)
    斯坦福第十四课:降维(Dimensionality Reduction)
    斯坦福第十三课:聚类(Clustering)
    斯坦福第十二课:支持向量机(Support Vector Machines)
    斯坦福第十一课:机器学习系统的设计(Machine Learning System Design)
    斯坦福第十课:应用机器学习的建议(Advice for Applying Machine Learning)
    斯坦福第九课:神经网络的学习(Neural Networks: Learning)
    斯坦福第八课:神经网络表述(Neural Networks: Representation)
    斯坦福第七课:正则化(Regularization)
  • 原文地址:https://www.cnblogs.com/qixue/p/2149954.html
Copyright © 2011-2022 走看看