XSL模板变量不打印

时间:2012-08-09 18:07:49

标签: c# xslt .net-4.0

我不太了解XSL模板,因为我只在两小时前第一次看到它。

我有以下模板:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:FormData="ext:FormVal" 
     xmlns:FormUrl="ext:UrlFinder"                    
     >
  <xsl:param name="note"/>
  <xsl:param name="title"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>EW Email Notification- File Note insertion Failed</title>
      </head>
      <body>
        <p>
          Failed to insert File note for form with refrence - <xsl:value-of select="FormData:get_RefFormId()" /> <br/>
          <a>
            <xsl:attribute name="href">
              <xsl:value-of select="FormUrl:get_RootUrl()"></xsl:value-of>FormAction/ViewForm?formId=<xsl:value-of select="FormData:get_FormId()"></xsl:value-of>
            </xsl:attribute>Click here </a> to open the form.
        </p>
        <p>
          Please insert this file note manually.
        </p>
        <p>
          <h3>
            Title:
          </h3>
          <out>          <xsl:value-of select="$title"></xsl:value-of>
          </out>

        </p>
        <p>
          <h3>
            Note:
          </h3>
         <xsl:value-of select="$note"></xsl:value-of>
        </p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

其中大部分是由其他人写的。我添加了notetitle参数。但是,出于某种原因,它们没有出现在打印输出中。

但是,FormData:get_FormId()有效。

我传递的变量如下:

dictionary["note"] = note;
dictionary["title"] =title;
dictionary["ext:FormVal"] = formVal;
dictionary["ext:UrlFinder"] = urlFinder;
var objxslt = new XslCompiledTransform();

objxslt.Load(Helper.GetEmbeddedXmlResource(templateAssembly, templateName));

var xmldoc = new XmlDocument();
xmldoc.AppendChild(xmldoc.CreateElement("DocumentRoot"));

XPathNavigator xpathnav = xmldoc.CreateNavigator();
var xslarg = new XsltArgumentList();

foreach (DictionaryEntry entry in dictionary)
{
    xslarg.AddExtensionObject(entry.Key.ToString(), entry.Value);
}

var emailbuilder = new StringBuilder();
var xmlwriter = new XmlTextWriter(new System.IO.StringWriter(emailbuilder));
objxslt.Transform(xpathnav, xslarg, xmlwriter, null);

var xemaildoc = new XmlDocument();
xemaildoc.LoadXml(emailbuilder.ToString());

XmlNode titlenode = xemaildoc.SelectSingleNode("//title");
string subjecttext = titlenode.InnerText;

XmlNode bodynode = xemaildoc.SelectSingleNode("//body");
string bodytext = bodynode.InnerXml;

变量bodytext包含所有模板,但是我希望打印参数的空白。

有人可以帮我确定notetitle参数未显示在输出中的原因吗?

1 个答案:

答案 0 :(得分:1)

好的,这就是我做的。我没有直接传递字符串notetitle,而是创建了一个类:

public class InsertFileNoteContainer
{
    public String Note { get; set; }
    public String Title { get; set; }
}

我把它放在带有密钥ext:IFC的字典中。

在模板中我做了:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:FormData="ext:FormVal" 
     xmlns:FormUrl="ext:UrlFinder" 
     xmlns:Container="ext:IFC"          
     >

<xsl:value-of select="Container:get_Title()"></xsl:value-of>

<xsl:value-of select="Container:get_Note()"></xsl:value-of>

请注意,因为我是XSL模板的新手,所以我不知道为什么会这样。如果任何人都能明白为什么在这里引入一个类,那么使用字符串参数会失败,请告诉我。