xslt参数传递问题 - <xsl:text>返回空结果</xsl:text>

时间:2013-03-07 18:41:41

标签: c# xslt

下面是我的代码,我正在传递Xslt参数&amp;检查参数是否为true然后xslt:text应该返回,但我没有找到任何值。我是xslt的新手。

    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 file ----

<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>

1 个答案:

答案 0 :(得分:0)

test="$category=true"表示test="$category = child::true",除非您有一个名为“true”的元素,否则比较结果将为false。我猜你的意思是test="$category=true()",虽然写test="$category"更简单。

(我不熟悉.NET转换API,但我认为当AddParam()提供布尔值时,样式表看到的也是布尔值。

相关问题