2013-03-07 65 views
0

下面是我的代碼,我傳遞Xslt參數&檢查參數是否爲真然後xslt:文本應該返回,但我不geting任何值。我是新的xslt 。xslt參數傳遞問題 - <xsl:text>返回空結果

string filename = "tmp.xml"; 
    string stylesheet = "result.xslt"; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     //Create the XslTransform and load the stylesheet. 
     XslCompiledTransform xslt = new XslCompiledTransform(); 
     xslt.Load(stylesheet); 

     //Load the XML data file. 
     XPathDocument doc = new XPathDocument(filename); 
     XsltArgumentList obj = new XsltArgumentList(); 
     bool category = ConfigurationManager.AppSettings["Code"].Contains("Software"); 
     obj.AddParam("category", "", category); 
     //Create an XmlTextWriter to output to the console.    
     StringWriter sw = new StringWriter(); 
     XmlTextWriter writer = new XmlTextWriter(sw); 
     writer.Formatting = Formatting.Indented; 
     //Transform the file. 
     xslt.Transform(doc, obj, writer, null); 
     writer.Close(); 
    } 

----- result.xslt文件----

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:user="urn:my-scripts"> 
    <xsl:param name="category" /> 
    <xsl:template match="data"> 
    <xsl:if test="$category=true"> 
     <xsl:text>Software is avilable.</xsl:text> 
    </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

------ tmp.xml文件-------------

<?xml version="1.0" encoding="utf-8" ?> 

<data> 
    <circle> 
    <radius>12</radius> 
    </circle> 
    <circle> 
    <radius>37.5</radius> 
    </circle> 
</data> 

回答

0

test="$category=true"意味着test="$category = child::true",除非你有一個名爲「真」的元素,比較的結果將是錯誤的。我想你的意思是test="$category=true()",但編寫test="$category"更簡單。 (我對.NET轉換API並不熟悉,但我認爲當AddParam()提供一個布爾值時,樣式表所看到的也是一個布爾值)。

+0

這是非常有用的信息,謝謝 – intelliWork 2013-03-08 17:33:05