在XSLT参数列表中传递对象列表,并在C#中的xsl中访问它

本文关键字:列表 中的 xsl 并在 访问 对象 参数 XSLT | 更新日期: 2023-09-27 17:58:46

我有一个在XSL 中使用的类

public class PurchaseOrderDTO
{
    public IList<PurchaseOrderDetailsDTO> PurchaseOrderDetailsDTOList { get; set; }
    public String PONo { get; set; }
    public Decimal POValue { get; set; }
    public DateTime? PODate { get; set; }
}

以下是我使用的xsl

<P>
  Dear,
</P>
Purchase Order No <xsl:value-of select="PurchaseOrderDTO:get_PONo()" /> has
being created <BR />
Purchase Order Value <xsl:value-of select="PurchaseOrderDTO:get_POValue()" /> <BR />
<BR />
Purchase Order Date <xsl:value-of select="PurchaseOrderDTO:get_PODate()" /> <BR />
<BR />
<xsl:for-each select="PurchaseOrderDTO:get_PurchaseOrderDetailsDTOList()">
  <ul>
    <li>
      <p>      
        Item name
      </p>
      <xsl:value-of select="PurchaseOrderDetailsDTO:get_Item()"></xsl:value-of>
    </li>
  </ul>
</xsl:for-each>

  private static PurchaseOrderDTO GetPurchaseOrderDTO()
    {
        PurchaseOrderDTO _PurchaseOrderDTO = new PurchaseOrderDTO();
        Random rand = new Random();
        _PurchaseOrderDTO.PODate = DateTime.Now;
        _PurchaseOrderDTO.PONo = "PO3333";
        _PurchaseOrderDTO.POValue = rand.Next(40200, 960000);
        IList<PurchaseOrderDetailsDTO> _PurchaseOrderDetailsDTOList = new List<PurchaseOrderDetailsDTO>();
        for (int i = 1; i < 3; i++)
        {
            PurchaseOrderDetailsDTO _PurchaseOrderDetailsDTO = new PurchaseOrderDetailsDTO();
            _PurchaseOrderDetailsDTO.ID = (Int16)i;
            _PurchaseOrderDetailsDTO.Item = "Item" + i;
            _PurchaseOrderDetailsDTO.Value = Convert.ToDecimal(rand.Next(100, 500) * i);
            _PurchaseOrderDetailsDTOList.Add(_PurchaseOrderDetailsDTO);
        }
        _PurchaseOrderDTO.PurchaseOrderDetailsDTOList = _PurchaseOrderDetailsDTOList;
        return _PurchaseOrderDTO;

    }
    private static void GetEmailTemplate()
    {
        PurchaseOrderDTO _PurchaseOrderDTO = GetPurchaseOrderDTO();
        XslTransform _XslTransform = new XslTransform();
        _XslTransform.Load("../../Transformer.xslt");
        XmlDocument _XmlDocument = new XmlDocument();
        _XmlDocument.AppendChild(_XmlDocument.CreateElement("Root"));
        XPathNavigator _XPathNavigator = _XmlDocument.CreateNavigator();
        XsltArgumentList _XsltArgumentList = new XsltArgumentList();

        Hashtable _Hashtable = new Hashtable();
        _Hashtable["PurchaseOrderDTO"] = _PurchaseOrderDTO;
        if (_Hashtable != null)
        {
            foreach (DictionaryEntry entry in _Hashtable)
            {
                _XsltArgumentList.AddExtensionObject(entry.Key.ToString(), entry.Value);
            }
        }
        StringBuilder _sb = new StringBuilder();
        XmlTextWriter _XmlTextWriter = new XmlTextWriter(new StringWriter(_sb));
        _XslTransform.Transform(_XPathNavigator, _XsltArgumentList, _XmlTextWriter);
        Console.WriteLine(_sb.ToString());

    }

但是在ouput中,我没有得到子属性值。是否可以访问XsltArgumentList中的列表项值?

在XSLT参数列表中传递对象列表,并在C#中的xsl中访问它

恐怕我不这么认为。玩你的代码时,我收到错误,告诉我不支持List'1的返回类型(即详细对象的集合)

检查XSLT扩展对象文档告诉我:

从扩展返回的数据类型对象是四个基本对象之一数字、字符串等XPath数据类型,布尔值和节点集

这有点明确,在您尝试使用任意CLR对象时,它们不受支持。

可能值得以稍微不同的方式来处理这个问题——如果你想将CLR对象与这样的模板合并,那么预编译的T4模板可能是一个更好的选择。我有一篇博客文章在这里描述了这一点:使用T4在运行时生成内容