0

Can I use script blocks in 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>

Update

I'll explain a little more of what I'm trying to do. Simply, I'm trying to use XSL transform to dynamically generate an image.

So here's an illustration of the web page I'm trying to generate:

enter image description here

This is the XSL transform I've got (Note: I'm not using the XML part yet, I don't know if that might be causing issues):

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>
";
        }
    }
}

The problem I'm having in the preceding code is in the GetXsl method (you may need to scroll down):

enter image description here

And here's the stack trace:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Rod
  • 14,529
  • 31
  • 118
  • 230
  • Possible duplicate of [How to include javaScript file in xslt](http://stackoverflow.com/questions/7444317/how-to-include-javascript-file-in-xslt) – Seano666 Mar 24 '17 at 23:18

1 Answers1

1

You can call C# method from xsl

so you could wrap what you want into a C# method then call it

The way to do it is:

  1. declare a namespace
  2. call it in xsl with namespace
  3. when run the transformation - pass in the class

So something like

define namespace

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

your C# code

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

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

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

when calling the transform

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

for more detail example, checkout https://www.intertech.com/Blog/calling-net-functions-from-an-xml-stylesheet/

Alan Tsai
  • 2,465
  • 1
  • 13
  • 16