0

下面是我的代码,我正在传递 Xslt 参数并检查参数是否为真,然后 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();
    }

----- 结果.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>
4

1 回答 1

0

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

(我对 .NET 转换 API 不太熟悉,但我假设当 AddParam() 提供一个布尔值时,样式表看到的也是一个布尔值)。

于 2013-03-07T22:32:32.050 回答