一剑飞虹

道可道非常道,名可名非常名
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

遭遇xsl

Posted on 2006-04-29 10:45  greatqn  阅读(543)  评论(3编辑  收藏  举报

xsl语法:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:myObj="urn:myObj">

<xsl:template match="/">
  <a>
    <xsl:apply-templates select="SearchResult" />
  </a>
</xsl:template>
<xsl:template match="SearchResult">
//模板内容
</xsl:template>
</xsl:stylesheet>

说明:
xmlns:xsl="http://www.w3.org/1999/XSL/Transform XML 命名空间 MSXML 3.0 bata 版
<xsl:template match="/"> 主节点模板
<xsl:apply-templates select="SearchResult" /> 应用模板
<xsl:for-each select ="ADInfos"> 在xml中循环找一个节点

<a class="LINK">
  <xsl:attribute name="href">
    <xsl:value-of select="URL"/>
  </xsl:attribute>
  <xsl:value-of select="ADInfoTitle"/>
</a> 链接的写法。通过加attribute来实现

<xsl:choose>
  <xsl:when test ="IsUrlInfo = 0">
     条件判断。
    

xml引用:

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="search1.xsl"?>
  <SearchResult>
    <ADInfos></ADInfos>
   

用程序进行样式表转换:
XslCompiledTransform 类  
注意:此类在 .NET Framework 2.0 版中是新增的。
使用 XSLT 样式表转换 XML 数据。
命名空间:System.Xml.Xsl
程序集:System.Xml(在 system.xml.dll 中)

// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("output.xsl");

// Execute the transform and output the results to a file.
xslt.Transform("books.xml", "books.html");

XSLT 扩展对象
扩展对象用于扩展样式表的功能。扩展对象通过 XsltArgumentList 类来维护。
使用 AddExtensionObject 方法将 XSLT 扩展对象添加到 XsltArgumentList 对象。此时,限定名和命名空间 URI 与扩展对象关联。
从扩展对象返回的数据类型是四种 XPath 基本数据类型之一:number、string、Boolean 和 node set。
StringBuilder sb = new StringBuilder();
XsltArgumentList xa = new XsltArgumentList();
xa.AddExtensionObject("urn:myObj", new XslUtility());
_xslCompiledTransform.Transform(input, xa,XmlWriter.Create(sb));
return sb;