2012-04-18 4 views
-1

먼저 XML 내가 브라우저에서 XSLT 변환하고있어한 XSLT/두 XML

<?xml version="1.0"?> 
<response> 
<status> 
<code>0</code> 
</status> 
<newsList> 
<news> 

<id>1</id> 
<text> 
Some text here 
</text> 
</news> 

<?xml version="1.0"?> 
<response> 
<status> 
<code>0</code> 
</status> 
<newsList> 
<news> 

<id>1</id> 
<title>some</title> 
<date>30.11.2011T00:00.00</date> 
<shortText>some short text</shortText> 
<important>LOW</important> 

</news> 

두 번째 XML.

결과는 첫 번째 XML의 제목 날짜와 짧은 텍스트 및 두 번째 XML의 텍스트와 일치해야합니다.

내가 어떻게 XSLT에서 그것을 할 수 있습니까? 나는이 문서를 사용할 생각이다.

아래의 XSLT를 보았습니다.

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/"> 
<h2>News list</h2> 
<table border="1"> 

<xsl:for-each select="newsList/news"> 
<tr> 
    <td><xsl:value-of select="title" /></td> 
    <td><xsl:value-of select="shortText" /></td> 
    <td><xsl:value-of select="date" /></td> 
</tr> 
</xsl:for-each> 
</table> 
</xsl:template> 

</xsl:stylesheet> 

마지막으로 브라우저에서 HTML로 변환 XSLT를로드하는 데 사용하는 스크립트입니다.

<script> 
    // the loadXMLDoc function loads the XML and XSL files. 
    //It checks what kind of browser the user has and loads the file. 

    function loadXMLDoc(dname) 
    { 
    if (window.XMLHttpRequest) 
     { 
     xhttp=new XMLHttpRequest(); 
     } 
    else 
     { 
     xhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
    xhttp.open("GET",dname,false); 
    xhttp.send(null); 
    xhttp.open 
    return xhttp.responseXML; 
    } 


    //The displayResult() function is used to display the XML file styled by the XSL file. 


    function displayNewsOverview(xm_l,xs_l) 
    { 
    // Load XML and XSL file 
    xml=loadXMLDoc(xm_l +".xml"); 
    xsl=loadXMLDoc(xs_l +".xsl"); 
    //Test what kind of browser the user has 
    // If the user has a browser supporting the ActiveX object (IE) 
    if (window.ActiveXObject) 
     { 
    // Use the transformNode() method to apply the XSL style sheet to the xml document 
    // Set the body of the current document (id="news-overview") to contain the styled xml document 

     ex=xml.transformNode(xsl); 
     document.getElementById("news-overview").innerHTML=ex; 
     } 
    // If the user has a browser that does not support the ActiveX object 
    else if (document.implementation && document.implementation.createDocument) 
     { 
    // Create a new XSLTProcessor object and import the XSL file to it 
     xsltProcessor=new XSLTProcessor(); 
     xsltProcessor.importStylesheet(xsl); 

    // Use the transformToFragment() method to apply the XSL style sheet to the xml document 
     resultDocument = xsltProcessor.transformToFragment(xml,document); 

    // Set the body of the current document (id="example") to contain the styled xml document 
     document.getElementById("news-overview").appendChild(resultDocument); 
     } 
    } 

    function displayNewsDetails(xm_l,xs_l) 
    { 
    document.getElementById("news-overview").innerHTML=""; 

    // Load XML and XSL file 
    xml=loadXMLDoc(xm_l +".xml"); 
    xsl=loadXMLDoc(xs_l +".xsl"); 

    //Test what kind of browser the user has 
    // If the user has a browser supporting the ActiveX object (IE) 
    if (window.ActiveXObject) 
     { 
    // Use the transformNode() method to apply the XSL style sheet to the xml document 
    // Set the body of the current document (id="news-overview") to contain the styled xml document 

     ex=xml.transformNode(xsl); 
     document.getElementById("news-overview").innerHTML=ex; 
     } 
    // If the user has a browser that does not support the ActiveX object 
    else if (document.implementation && document.implementation.createDocument) 
     { 
    // Create a new XSLTProcessor object and import the XSL file to it 
     xsltProcessor=new XSLTProcessor(); 
     xsltProcessor.importStylesheet(xsl); 

    // Use the transformToFragment() method to apply the XSL style sheet to the xml document 
     resultDocument = xsltProcessor.transformToFragment(xml,document); 

    // Set the body of the current document (id="example") to contain the styled xml document 
     document.getElementById("news-overview").appendChild(resultDocument); 
     } 
    } 
    </script> 
    </head> 


onLoad="displayNewsOverview('news-overview', 'news-overview')"> 

도움이 될 것입니다. 그것은 직업입니다.

감사합니다.

답변

1

xslt에서 document() 함수 - http://www.w3schools.com/xsl/func_document.asp을 사용할 수 있습니다. 하나의 xml에 < ?xml-stylesheet href="nameofxslt" type= "text/xsl" >을 추가하고 document 함수를 사용하여 다른 xml 콘텐츠에 액세스 할 수 있습니다. xml 지시문 (<?xml-stylesheet)을 추가하면 자바 스크립트에서 변형을 적용 할 필요가 없습니다.

+0

xml에 xslt href를 추가해야 문서를 사용할 수 있습니까? – user1341765

+0

자바 스크립트 코드를 사용하여 XML을 변환하면 필요하지 않습니다. 변환 코드없이 브라우저에서 XML을 직접로드하려고하면 "xslt href"를 추가해야합니다. – Mircea

관련 문제