根据xslt定义xml文件 生成结果xml文件

xml文件如下:
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    
<title>The Autobiography of Benjamin Franklin</title>
    
<author>
      
<first-name>Benjamin</first-name>
      
<last-name>Franklin</last-name>
    
</author>
    
<price>8.99</price>
  
</book>
  
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    
<title>The Confidence Man</title>
    
<author>
      
<first-name>Herman</first-name>
      
<last-name>Melville</last-name>
    
</author>
    
<price>11.99</price>
  
</book>
  
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    
<title>The Gorgias</title>
    
<author>
      
<name>Plato</name>
    
</author>
    
<price>9.99</price>
  
</book>
</bookstore>


xsl文件如下:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    
<xsl:template match="/">
    
<root>
        
<xsl:apply-templates/>
    
</root>
    
</xsl:template>

    
<xsl:template match="bookstore">
    
<!-- Prices and books -->
        
<bookstore>
            
<xsl:apply-templates select="book"/>
        
</bookstore>
    
</xsl:template>

    
<xsl:template match="book">
        
<book>
            
<xsl:attribute name="ISBN">
                
<xsl:value-of select="@ISBN"/>
            
</xsl:attribute>
            
<price><xsl:value-of select="price"/></price><xsl:text>
            
</xsl:text>
        
</book>
    
</xsl:template>

</xsl:stylesheet>


.Net 代码如下:
//if(!(txtSource.Text.EndsWith(".xml")&& (txtXslt.Text.EndsWith(".xsl"))))
            
//{
                sourceDoc = txtSource.Text + ".xml";
                xsltDoc 
= txtXslt.Text + ".xsl";
            
//}

            
if ((txtSource.Text.Trim() == ""|| (txtXslt.Text.Trim() == "")) 
            
{
                MessageBox.Show(
"Enter the filename!""File Name Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                
/*if (MessageBox.Show ("Do you want to exit?", "My Application", 
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    == DialogResult.Yes) 
                {
                    Application.Exit();
                }
*/


            }


            
try
            
{
                
//txtResult.Text = "Generating";

                XPathDocument myXPathDocument 
= new XPathDocument (sourceDoc);
                XslTransform myXslTransform 
= new XslTransform();
        
                XmlTextWriter writer 
= new XmlTextWriter(resultDoc, null);
                myXslTransform.Load(xsltDoc);

                myXslTransform.Transform(myXPathDocument, 
null, writer);
                writer.Close();

                StreamReader stream 
= new StreamReader (resultDoc);
                txtResult.Text 
= stream.ReadToEnd();
                
//Console.Write("**This is result document**\n\n");
                
//Console.Write(stream.ReadToEnd());
  
            }


            
catch (FileNotFoundException filexc)
            
{
                MessageBox.Show(
"File Not Found!""File Not Found Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }



            
catch (Exception exc)
            
{
                Console.WriteLine (
"Exception: {0}", exc.ToString());
            }

生成结果xml如下:
<root>
<bookstore>
<book ISBN="1-861003-11-0"><price>8.99</price></book>
<book ISBN="0-201-63361-2"><price>11.99</price></book>
<book ISBN="1-861001-57-6"><price>9.99</price></book>
</bookstore>
</root>

posted @ 2008-07-08 16:41  jhtchina  阅读(421)  评论(0)    收藏  举报