Archive for the category “XML-XSLT”
XSL transform with arguments
Using XsltArgumentList, we can pass arguments or variable values in to the XSLT while transforming an Xml document. One can also pass in another stream of Xml document such that the XSLT operates on two Xml documents at once.
Following is a piece of code that makes it happen in C#. It also passes in a parameter to sort a column in the Xml document.
XslCompiledTransform xslt = new XslCompiledTransform();
XmlReader xReader = XmlReader.Create(new StringReader(Properties.Resources.XsltFile));
xslt.Load(xReader);
StringWriter stringOut = new StringWriter();
XsltArgumentList xsltArgs = new XsltArgumentList();
xsltArgs.AddParam(“extraXmlDocument”, string.Empty, xpathNavigableObject.CreateNavigator());
if (!string.IsNullOrEmpty(sortBy))
{
xsltArgs.AddParam(“sortBy”, string.Empty, sortBy);
if (sortType.Trim().Equals(“down”))
{
sortType = “descending”;
}
else
{
sortType = “ascending”;
}
xsltArgs.AddParam(“sortType”, string.Empty, sortType);
}
else
{
xsltArgs.AddParam(“sortBy”, string.Empty, string.Empty);
xsltArgs.AddParam(“sortType”, string.Empty, “ascending”);
}
xslt.Transform(xpathNavigableObject, xsltArgs, stringOut);
// Set the new sorted html content back to the webbrowser control’s html.
// Just an example below.
m_webBrowser.Document.Body.InnerHtml = stringOut.ToString();
Notes: Always use “XslCompiledTransform” and ‘XpathDocument‘ when you need readonly access.
The “true” Checklist for XML Performance
String replace in XSLT
It is possible to do string manipulation like replace etc in XSLT as well. Javascript has str.replace right? So in XSLT you can do this by creating templates and by doing call-template.
Follow this link for complete info and source code
http://www.dpawson.co.uk/xsl/sect2/StringReplace.html
Check for nodename in XSLT
Its always necessary sometimes in XSLT processing that we check for things like whether a node exists to do certain operations. Checking for nodenames is the clue. This is how you do it.
<xsl:variable name=”varname”>
<xsl:value-of select=”ParentNode/*[name()='childnodename to check for']“/>
<xsl:variable>
<xsl:if test=”$varname=””> Processing here…
<xsl:if>

