码迷,mamicode.com
首页 > 其他好文 > 详细

XSLT 调用外部程序

时间:2014-12-18 10:10:13      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   io   color   os   sp   for   

通常可以通过xslt把一个xml转成html

cd.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="cd_apply.xslt"?>
<collection>
  <cd>
    <title>Boys for Pele</title>
    <artist>Tori Amos</artist>
    <tracks>
      <track type="vocal">
        <name>Horses</name>
        <length>3.5</length>
      </track>
      <track type="instrumental">
        <name>Blood roses</name>
        <length>3.2</length>
      </track>
      <track type="vocal">
        <name>Father lucifer</name>
        <length>3.8</length>
      </track>
      <track type="instrumental">
        <name>Professional widow</name>
        <length>4.1</length>
      </track>
      <track type="vocal">
        <name>Mr. Zebra</name>
        <length>3.6</length>
      </track>
    </tracks>
  </cd>
  <cd>
    <title>The Ghosts that Hunt me</title>
    <artist>Crash TestDummies</artist>
    <tracks>
      <track type="vocal">
        <name>Winter song</name>
        <length>4.3</length>
      </track>
      <track type="instrumental">
        <name>Comin‘s back soon</name>
        <length>4.1</length>
      </track>
      <track type="vocal">
        <name>Superman‘s song</name>
        <length>4.1</length>
      </track>
      <track type="vocal">
        <name>Here on earth</name>
        <length>3.1</length>
      </track>
    </tracks>
  </cd>
</collection>

cd.apply.xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body><xsl:apply-templates/></body>
        </html>
    </xsl:template>
    
    <xsl:template match="collection">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="cd">
        <center>
            <xsl:apply-templates/>
        </center>
    </xsl:template>
    
    <xsl:template match="title">
        <font size="5"><b><xsl:value-of select="."/>:</b></font>
    </xsl:template>
    
    <xsl:template match="artist">
        <font size="5"><i><xsl:value-of select="."/></i></font>    
    </xsl:template>

    <xsl:template match="tracks">    
        <table border="1">
            <tr>
                <th>Name</th>
                <th>Length</th>
                <th>Type</th>
            </tr>
            <xsl:apply-templates/>        
        </table>
        <br/><br/>
    </xsl:template>    
    
    <xsl:template match="track">
        <tr>
            <td><xsl:value-of select="name"/></td>
            <td><xsl:value-of select="length"/></td>
            <td><xsl:value-of select="@type"/></td>
        </tr>
    </xsl:template>
</xsl:stylesheet>

有时可以调用一个外部程序的方法进行转换,比如外部类叫做Transformer

XslCompiledTransform xslt = new XslCompiledTransform();
XsltSettings xsltset = new XsltSettings();
xslt.Load(path, xsltset, null);
var arguments = new XsltArgumentList();
arguments.AddExtensionObject("urn:transformer", new Transformer());
xsltDOE2.Transform(.., arguments, ..);

xslt做相应修改

xmlns:transformer="urn:transformer"  //加一个namespace
<xsl:value-of select="transformer:Transform(.)"/>

C#代码

 public class Transformer
    {
        public string Transform(XPathNavigator navigator, double value1, bool value2)
        {
           ...
        }
        public string Transform(XPathNodeIterator iterator)
        {
            ...
        }
}            

 

XSLT 调用外部程序

标签:style   blog   http   ar   io   color   os   sp   for   

原文地址:http://www.cnblogs.com/cqcmdwym/p/4171059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!