How to Apply an Xslt Stylesheet in C#

How to apply an XSLT Stylesheet in C#

I found a possible answer here: http://web.archive.org/web/20130329123237/http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63

From the article:

XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;
XslTransform myXslTrans = new XslTransform() ;
myXslTrans.Load(myStyleSheet);
XmlTextWriter myWriter = new XmlTextWriter("result.html",null) ;
myXslTrans.Transform(myXPathDoc,null,myWriter) ;

Edit:

But my trusty compiler says, XslTransform is obsolete: Use XslCompiledTransform instead:

XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;
XslCompiledTransform myXslTrans = new XslCompiledTransform();
myXslTrans.Load(myStyleSheet);
XmlTextWriter myWriter = new XmlTextWriter("result.html",null);
myXslTrans.Transform(myXPathDoc,null,myWriter);

Writing an XSLT Stylesheet Header

It seems the arguments need to alter the position and be used in proper way.

ref: https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlwriter.writeattributestring?view=netframework-4.7.2

In your case, It should be written as

For Example :

xmlWriter.WriteAttributeString("xmlns", "ss", null, "urn:schemas-microsoft-com:office:spreadsheet");

Because WriteAttributeString(String, String, String, String)

When overridden in a derived class, writes out the attribute with the specified prefix, local name, namespace URI, and value.

public void WriteAttributeString (string prefix, string localName, string ns, string value);

Get filepath of XSLT stylesheet in embedded C# code? (MSXML)

Assuming the stylesheet is loadable in a way that a base URI is available you could do it as follows:

<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 mf"
xmlns:mf="http://example.com/mf"
>
<xsl:output method="xml" indent="yes"/>

<msxsl:script language="C#" implements-prefix="mf">
public string GetBaseUri(XPathNavigator node) {
return node.BaseURI;
}

public string GetFilePath(string baseUri, string fileName) {
return new Uri(new Uri(baseUri), fileName).LocalPath;
}
</msxsl:script>


<xsl:template match="/">
<xsl:value-of select="mf:GetFilePath(mf:GetBaseUri(document('')), 'textInput.txt')"/>

</xsl:template>
</xsl:stylesheet>

Note that this requires both enabling of script as well as of the document function, then inside of XSLT document('') can be used to get a tree representation of the stylesheet, you can then pass that to an extension function taking an XPathNavigator which allows you to read out the BaseURI property, once you have that, you can use the Uri class to resolve a file name relative to that base URI and you can then get a LocalPath representation of that URI, which you could then use to load the text file.

So in the context of your code you could use




  public bool loadImageList(string baseUri)
{
imageList = System.IO.File.ReadAllText(GetFilePath(baseUri, "images.txt"));
return true;
}

public bool inImageList(string str)
{
return imageList.Contains("\r\n" + str + "\r\n");
}

public string GetBaseUri(XPathNavigator node) {
return node.BaseURI;
}

public string GetFilePath(string baseUri, string fileName) {
return new Uri(new Uri(baseUri), fileName).LocalPath;
}
]]>

 <xsl:variable name="loadImageList" select="local:loadImageList(local:GetBaseUri(document('')))"/>

Passing parameters to XSLT Stylesheet via .NET

You need to define the parameter within your XSLT and you also need to pass the XsltArgumentList as an argument to the Transform call:

private static void CreateHierarchy(string manID)
{
string man_ID = manID;

XsltArgumentList argsList = new XsltArgumentList();
argsList.AddParam("Boss_ID", "", man_ID);

XslCompiledTransform transform = new XslCompiledTransform(true);
transform.Load("htransform.xslt");

using (StreamWriter sw = new StreamWriter("output.xml"))
{
transform.Transform("LU AIB.xml", argsList, sw);
}
}

Please note that the xsl:param must be defined below the xsl:stylesheet element:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />

<xsl:param name="Boss_ID"></xsl:param>

<xsl:template match="OrgDoc">

<!-- template body goes here -->

</xsl:template>


</xsl:stylesheet>

This simple XSLT sample will create just a small output document containing one XML node with its contents set to the value of your parameter. Have a try:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:param name="Boss_ID"></xsl:param>

<xsl:template match="/">
<out>
<xsl:value-of select="$Boss_ID" />
</out>
</xsl:template>

</xsl:stylesheet>

how to apply XSL to XML with xsltargument using c#?

You have use , XsltArgumentList C# class to pass arguments. You can add all your parameters there and pass to the xsl.

Please look into following SO Link,

Passing parameters to XSLT Stylesheet via .NET



Related Topics



Leave a reply



Submit