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");
            }
        }
    }
  • 相关阅读:
    iOS开发之 Xcode6 添加xib文件,去掉storyboard的hello world应用
    iOS开发之Xcode 相对路径与绝对路径
    iOS开发之 在release版本禁止输出NSLog内容
    iOS开发之 xcode6 APP 打包提交审核详细步骤
    iOS开发之 UIScrollView的frame、contentSize、contentOffset和contentInset属性
    10.2&10.3 Xcode开发包
    Reason: image not found
    如何下架app
    UIStackView before iOS9.0
    Reason: image not found
  • 原文地址:https://www.cnblogs.com/qixue/p/2149954.html
Copyright © 2011-2022 走看看