The ASU.WebUtilities.XmlEngine is a quick way to transform xml documents using xsl/xslt files into a string or xml reader.
Current production version is 1.0.0.5, and is available for checkout from: https://svn.asu.edu/svn/webcommune/.net/asu.webutilities.xmlengine/tags/1.0.0.5/.
If you wish to expand on the XmlEngine please check it out from: https://svn.asu.edu/svn/webcommune/.net/asu.webutilities.xmlengine/trunk/.
You must be a member of the ASU Web Developer Commune group to get access to this project.
If you want this assembly to appear in the Add Reference dialog box, simply place a copy of the assembly in the following folder C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PublicAssemblies.
Constructors
TransformXmlFile(String xmlFilePath, String xslFilePath)
You provide the location to the xml file and xsl file that you wish to transform.
// create xml engine instance and pass in the xml file and xsl file
TransformXmlFile xmlMenu = new TransformXmlFile(Server.MapPath(xmlFilePath), Server.MapPath(xslFilePath));
TransformXmlFile(String xmlFilePath, String xslFilePath, Boolean enableDocumentFunction)
You provide the location to the xml file, xsl file and a boolean value to enable the xslt document() function. Learn more about the xslt document() function at W3Schools and Microsoft.
// create xml engine instance and pass in the xml file and xsl file
TransformXmlFile xmlMenu = new TransformXmlFile(Server.MapPath(xmlFilePath), Server.MapPath(xslFilePath), enableDocumentFunction);
TransformXmlFile(String xmlFilePath, String xslFilePath, XsltArgumentList argumentList)
You provide the location to the xml file, xsl file and xslt argument list that you wish to transform.
// load the arguments
XsltArgumentList args = new XsltArgumentList();
// web page argument
args.AddParam("webPage", "", Current URL);// create xml engine instance and pass in the xml file, xsl file and xslt arg list
TransformXmlFile xmlMenu = new TransformXmlFile(Server.MapPath(xmlFilePath), Server.MapPath(xslFilePath), args);
TransformXmlFile(String xmlFilePath, String xslFilePath, Boolean enableDocumentFunction, XsltArgumentList argumentList)
You provide the location to the xml file, xsl file, boolean value for enableDocumentFunction, and xslt argument list. Learn more about the xslt document() function at W3Schools and Microsoft.
// load the arguments
XsltArgumentList args = new XsltArgumentList();
// web page argument
args.AddParam("webPage", "", Current URL);// create xml engine instance and pass in the xml file and xsl file
TransformXmlFile xmlMenu = new TransformXmlFile(Server.MapPath(xmlFilePath), Server.MapPath(xslFilePath), enableDocumentFunction, args);
Methods
TransformToString()
Use this method if you wish to return a string representation of the transform. For instance, if the xsl is returning html that is to be written directly to the browser (like the left navigation menu in the ASU template).
// transform the xml file
string menu = xmlMenu.TransformToString();if (menu == null || menu.ToString() == string.Empty)
{
...
}
else
{
...
}
TransformToXmlReader()
Use this method if you wish to have an xml object to process/parse the data that was returned with the transform (like determining the event log to write to in the Exceptions project).
// transform the xml file
XmlReader xrdr = txml.TransformToXmlReader();if (xrdr != null && xrdr.Read())
{
...
}
ExtensionObjects
The ExtensionObjects class is a collection of properties and methods that provide more flexibility that the xslt language cannot provide. You can reference this object from within your xsl files to provide additional processing capabilities. To reference this object in the xsl file, you must provide the following statement into the <xsl:stylesheet> tag:
xmlns:eobjs="urn:ASUXmlEngine-ExtensionObjects"
like the following:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eobjs="urn:ASUXmlEngine-ExtensionObjects" version="1.0">
Constructor
ExtensionObjects(String webPage, IPrincipal user)
The constructor for this object requires two values:
- webPage - the URL for the web page that is currently being loaded
- user - the IPrincipal object for the current user (HttpContext.Current.User). This object is required for the UserHasAccess method. If you are not using this method in your xsl, you can just pass in null.
To pass the reference to the ExtensionObjects into the xsl file, you use the XslArgumentList like the following:
XsltArgumentList args = new XsltArgumentList();
ExtensionObjects eobj = new ExtensionObjects(Current URL, HttpContext.Current.User);
args.AddExtensionObject("urn:ASUXmlEngine-ExtensionObjects", eobj);// create xml engine instance and pass in xslt arg list
TransformXmlFile xmlMenu = new TransformXmlFile(Server.MapPath(xmlFile), Server.MapPath(xslFile), args);
Properties
Level1HyperLink
This property is populated from the method SetLevel1Hyperlink and then used in the method ShowSubMenuItem. These methods are no longer used any more.
WebPage
This property is populated with the value webPage from the constructor.
User
This property is populated with the value user from the constructor.
Methods
UserHasAccess (String roles)
You can setup your xml files to restrict access to the data by providing a list of roles that have access to it. The list of roles from the xml file is compared to the roles the user has been assigned to in the HttpContext.Current.User object in asp.net. If the user has a role that matches the method returns true. You can call this method like the following (taken from the menu.xsl file):
<xsl:with-param name="items" select="menu/menuItem[eobjs:UserHasAccess(@roles)]" />
UserHasAccess (String roles, String denyRoles)
You can setup your xml files to restrict access to the data by providing a list of roles that have access, and a list of roles that don't have access to it. The list of roles and denyRoles from the xml file is compared to the roles the user has been assigned to in the HttpContext.Current.User object in asp.net. If the user has a role that matches and doesn't have a denyRole that matches, the method returns true. You can call this method like the following (taken from the menu.xsl file):
<xsl:with-param name="items" select="menu/menuItem[eobjs:UserHasAccess(@roles, @denyRoles)]" />
SetLevel1Hyperlink (String level1Hyperlink)
This method was used in conjunction with ShowSubMenuItem to create the menu for the ASU template. The xsl for loading the xml menu does not use this method anymore. All this method does is save the level1Hyperlink value to the local property Level1HyperLink. This property value is then used in ShowSubMenuItem.
ShowSubMenuItem()
This method was used in conjunction with SetLevel1Hyperlink to determine if the sub menu items should be generated. The xsl for loading the xml menu does not use this method anymore. If the Level1Hyperlink property was part of the path of the current web page being loaded, it would return true to indicate that the sub-menu items should be generated.
CompareStringValues(String string1, String string2)
Xsl does not have a method to compare two strings while ignoring case. This method determines if string1.ToLower() == string2.ToLower(). If it does, the method returns true.
You can call this method like the following (taken from the menu.xsl file):
<xsl:when test="eobjs:CompareStringValues($path, $webPage)">
CompareStringValues(String string1, String string2, Boolean ignoreQueryString)
Xsl does not have a method to compare two strings while ignoring case. This method determines if string1.ToLower() == string2.ToLower(). If it does, the method returns true. The ignoreQueryString option allows you to ignore any query string parameters that are included in the URL.
You can call this method like the following (taken from the menu.xsl file):
<xsl:when test="eobjs:CompareStringValues($path, $webPage, @ignoreQueryString)">