开发者

Getting Variable values out of and XSLT file

I wanted to find out if there is a way of getting a parameter or variable value out of an XSL file. For example, if I have the following:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:variable name="username" select ="usertest"/>
  <xsl:variable name="password" select ="pass"/>
  <!-- ... -->
</xsl:stylesheet>

I would like to read the username and password values from the XSL and use them for authentication. I am using ASP.Net and C# to perform the actual transform on an XML file.

Could someone please share code with me that would allow me t开发者_运维技巧o read the XSL variables from ASP.NET/C#. Thanks in advance for the help.


This is easy. XSL files are XML themselves, so you can treat them as such.

XmlDocument xslDoc = new XmlDocument();
xslDoc.Load("myfile.xsl");

XmlNamespaceManager nsMgr = new XmlNamespaceManager(xslDoc.NameTable);
nsMgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");

XmlNode usrNode = xslDoc.SelectSingleNode("/xsl:stylesheet/xsl:variable[@name='username']", nsMgr);
XmlNode pwdNode = xslDoc.SelectSingleNode("/xsl:stylesheet/xsl:variable[@name='password']", nsMgr);

string usr = usrNode.Attributes["select"].Value;
string pwd = pwdNode.Attributes["select"].Value;


Your question is (edit: was) missing the actual code, but from the description it appears what you are looking for is XPath. XSL will transform one XML document into another XML document, you can then use XPath to query the resulting XML to get out the values that you want.

This Microsoft KB article has information about how to use XPath from C#:

http://support.microsoft.com/kb/308333


Thanks Everone. Here is what finally worked:

Client (asp with vbscript) Used for Testing Purposes:

<%

//Create Object
Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
//Set up the object with the URL
'xmlhttp.open "POST" ,"http://localhost/ASP_Test/receiveXML.asp",False

//Create DOM Object
Set xmldom = CreateObject("Microsoft.XMLDOM")
xmldom.async = false

//Load xls to send over for transform
xmldom.load(Server.MapPath("/ASP_Test/masterdata/test.xsl"))

//Send transform file as DOM object
xmlhttp.send xmldom
%>
//////////////////////////////////////////////////////////////////////////
On the Server Side: (aspx with C#) Accepts xslt and process the transform:

//file path for data xml
String xmlFile = ("\\masterdata\\test.xml");
//file path for transformed xml
String xmlFile2 = ("\\masterdata\\out.xml");


XmlTextReader reader = new XmlTextReader(Request.InputStream);
Transform(xmlFile, reader, xmlFile2);

 public static string Transform(string sXmlPath, XmlTextReader xslFileReader, string outFile)
        {
            try
            {
                //load the Xml doc
                XPathDocument myXPathDoc = new XPathDocument(sXmlPath);
                XslCompiledTransform myXslTrans = new XslCompiledTransform();

                //load the Xsl
                myXslTrans.Load(xslFileReader);

                //create the output stream
                XmlTextWriter myWriter = new XmlTextWriter
                    (outFile, null);

                //do the actual transform of Xml
                myXslTrans.Transform(myXPathDoc, null, myWriter);

                myWriter.Close();
                return "Done";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜