2010-02-17 8 views
0

XSL 파일에서 매개 변수 나 변수 값을 가져 오는 방법이 있는지 알고 싶었습니다. 예를 들어, 내가 가지고있는 경우 다음변수 값을 XSLT 파일로 가져 오는 중

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

내가 XSL에서 usernamepassword 값을 읽고 인증을 사용하고 싶습니다. ASP.Net 및 C#을 사용하여 XML 파일에서 실제 변환을 수행하고 있습니다.

누군가 나와 코드를 공유하면 ASP.NET/C#에서 XSL 변수를 읽을 수 있습니다. 도움에 미리 감사드립니다.

답변

0

감사합니다. Everone. 여기에 마지막 일 것입니다 : 테스트 목적으로 사용

클라이언트 (VBScript를 가진 ASP) :

<% 

//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; 
      } 
     } 
2

귀하의 질문은 실제 코드가 누락되었습니다. 그러나 설명에서 찾고있는 것은 XPath입니다. XSL은 하나의 XML 문서를 다른 XML 문서로 변형하므로 XPath를 사용하여 결과 XML을 쿼리하여 원하는 값을 얻을 수 있습니다.

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

4

이 쉽습니다 :

이 마이크로 소프트 KB 기사는 C#을에서 XPath를 사용하는 방법에 대한 정보가 있습니다. XSL 파일은 XML 자체이므로 처리 할 수 ​​있습니다.

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; 
+0

좋아요 .. 다행 알고 .. +1 :-) –

관련 문제