2012-04-18 6 views
-1

Первый XMLОдин XSLT/Два XML в качестве источника

<?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 version="1.0"?> 
<response> 
<status> 
<code>0</code> 
</status> 
<newsList> 
<news> 

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

Я преобразование XSLT в браузере.

В результате должно быть указано название даты и короткий текст из первого 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> 

Наконец, сценарий, который я использую для загрузки преобразования XSLT в HTML в браузере.

<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

Вы можете использовать функцию document() - http://www.w3schools.com/xsl/func_document.asp в своем xslt. Вы можете попытаться добавить < ?xml-stylesheet href="nameofxslt" type= "text/xsl" > в один из ваших xmls и использовать функцию документа для доступа к содержимому xml другого. Добавив директиву xml (<?xml-stylesheet), вам не потребуется применять преобразование в javascript.

+0

Нужно ли добавить xslt href в xml для использования документа? – user1341765

+0

Если вы используете свой код javascript для преобразования xml, вам не нужно. Если вы попытаетесь загрузить xml в браузере напрямую без кода преобразования, вам нужно добавить «xslt href». – Mircea

Смежные вопросы