【问题标题】:asp.net xsl code blocks, will it workasp.net xsl 代码块,能用吗
【发布时间】:2017-08-17 23:43:29
【问题描述】:

更新
请允许我解释一下我正在尝试做的事情。简单地说,我正在尝试使用 xsl 转换来动态生成图像。而已。暂时。

下面是我尝试生成的网页的图示:

这是我得到的 xsl 转换(注意:我还没有使用 xml 部分,我不知道这是否会导致问题):

using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;

namespace WebApplication1
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string transform = GetXsl();
            string input = GetXml();

            StringWriter sw = new StringWriter();
            using (XmlReader xrt = XmlReader.Create(new StringReader(transform)))
            using (XmlReader xri = XmlReader.Create(new StringReader(input)))
            using (XmlWriter xwo = XmlWriter.Create(sw))
            {
                XslCompiledTransform xslt = new XslCompiledTransform();
                xslt.Load(xrt);
                xslt.Transform(xri, xwo);
            }
            out11.InnerHtml = sw.ToString();
        }

        private string GetXml()
        {
            return
@"<?xml version='1.0' encoding='UTF-8'?>
<catalog>
    <data id='1' option1='key1' option2='0' />
    <data id='2' option1='' option2='1' />
</catalog>
";
        }

        private string GetXsl()
        {
            return
@"<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

    <xsl:template match='/'>
        <img src='<%= Class1.ImageName(""arg1"") %>' alt='alt text' />
    </xsl:template>

</xsl:stylesheet>
";
        }
    }
}

我在前面代码中遇到的问题是在 GetXsl 方法中(您可能需要向下滚动):

这是堆栈跟踪:

原帖

我可以在 xsl 中使用脚本块吗?

<xsl:template match="mytest">
    Todo:
    <h3>In progress...</h3>

    '&lt;%="hello-world" %&gt;' CAN THIS WORK SOMEHOW

    <span id="spnIcon" runat="server" class="fa-1x"></span>  

  </xsl:template>

【问题讨论】:

标签: c# asp.net xslt


【解决方案1】:

你可以从 xsl 调用 C# 方法

这样你就可以将你想要的东西包装到一个 C# 方法中然后调用它

方法是:

  1. 声明一个命名空间
  2. 在带有命名空间的 xsl 中调用它
  3. 运行转换时 - 传入类

类似

定义命名空间

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
            xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
            xmlns:ext="http://XsltSampleSite.XsltFunctions/1.0">

你的 C# 代码

public class MyXsltExtensionFunctions
 {
    public const string Namespace = "http://XsltSampleSite.XsltFunctions/1.0";

    public string HelloWorld()
    {
       return "Hello World";
    }
}

在 xls 中

<xsl:template match="mytest">
    Todo:
    <h3>In progress...</h3>

    <xsl:value-of select="ext:HelloWorld()" />

    <span id="spnIcon" runat="server" class="fa-1x"></span>  

  </xsl:template>

调用变换时

XsltArgumentList xal = new XsltArgumentList();
     xal.AddExtensionObject(
         MyXsltExtensionFunctions.Namespace,
        new MyXsltExtensionFunctions());

如需更多详细示例,请查看https://www.intertech.com/Blog/calling-net-functions-from-an-xml-stylesheet/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-17
    • 2018-09-05
    • 2017-08-01
    • 2010-11-02
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多