2008-09-15 3 views
14

브라우저를 사용하여 XML (Google 크롬 또는 IE7)을 변환 할 때 URL을 통해 매개 변수를 XSLT 스타일 시트에 전달할 수 있습니까?브라우저를 사용하여 XML을 변환 할 때 URL을 통해 매개 변수를 XSLT로 전달할 수 있습니까?

예 :

인 data.xml

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="sample.xsl"?> 
<root> 
    <document type="resume"> 
     <author>John Doe</author> 
    </document> 
    <document type="novella"> 
     <author>Jane Doe</author> 
    </document> 
</root> 

당신은 클라이언트 - 변환이 경우에도 XSLT 서버 측을 생성 할 수

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fo="http://www.w3.org/1999/XSL/Format"> 

    <xsl:output method="html" /> 
    <xsl:template match="/"> 
    <xsl:param name="doctype" /> 
    <html> 
     <head> 
      <title>List of <xsl:value-of select="$doctype" /></title> 
     </head> 
     <body> 
      <xsl:for-each select="//document[@type = $doctype]"> 
       <p><xsl:value-of select="author" /></p> 
      </xsl:for-each> 
     </body> 
    </html> 
</<xsl:stylesheet> 

답변

3

sample.xsl 측면.

동적 스크립트를 사용하여 매개 변수를 처리 할 수 ​​있습니다.

예를 들어, 다음을 지정할 수 있습니다

<?xml-stylesheet type="text/xsl"href="/myscript.cfm/sample.xsl?paramter=something" ?> 

을 그리고 myscript.cfm 당신은 출력 XSL 파일은 있지만, 동적 스크립트는 쿼리 문자열 매개 변수를 처리하여 (이 달라질 것이다 것이다 스크립트 언어 당신에 따라 용도).

+4

파트에만 가능 클라이언트 측입니다 다음과 같이

xmlDoc.documentElement.setAttribute("myparam",getParameter("myparam")) 

그리고 자바 스크립트 기능은 무엇입니까? – user9547

6

아니요 - 클라이언트 측에서만 XSLT에 매개 변수를 전달할 수 없습니다. 웹 브라우저는 XML에서 처리 명령을받습니다. 그것을 XSLT로 직접 변환합니다.


쿼리 문자열 URL을 통해 값을 전달한 다음 JavaScript를 사용하여 값을 동적으로 읽을 수 있습니다. 그러나 브라우저가 이미 XML/XSLT를 변형 했으므로 XSLT (XPath 표현식)에서는 사용할 수 없습니다. 렌더링 된 HTML 출력에서만 사용할 수 있습니다.

6

매개 변수를 속성으로 XML 소스 파일에 추가하고 스타일 시트와 함께 속성으로 사용하기 만하면됩니다. 질문이 될 것

//Get querystring request paramter in javascript 
function getParameter (parameterName) { 

    var queryString = window.top.location.search.substring(1); 

    // Add "=" to the parameter name (i.e. parameterName=value) 
    var parameterName = parameterName + "="; 
    if (queryString.length > 0) { 
     // Find the beginning of the string 
     begin = queryString.indexOf (parameterName); 
     // If the parameter name is not found, skip it, otherwise return the value 
     if (begin != -1) { 
     // Add the length (integer) to the beginning 
     begin += parameterName.length; 
     // Multiple parameters are separated by the "&" sign 
     end = queryString.indexOf ("&" , begin); 
     if (end == -1) { 
     end = queryString.length 
     } 
     // Return the string 
     return unescape (queryString.substring (begin, end)); 
    } 
    // Return "null" if no parameter has been found 
    return "null"; 
    } 
} 
관련 문제