2013-03-29 48 views
0

一年使用計算器我決定加入後XML,但當然,由於這樣的事實,掙扎了一天之後,我堅持。添加樣式表與JavaScript不正確顯示或根本

我的問題:我使用JavaScript來建立由XML文檔和樣式表的文檔。這在我的兩個案例中運行良好,(在Firefox中,根本不在IE中),但不是第三種。

的JavaScript ========================================

function GetBookInfo(object) 
{ 
    try 
    { 
     var xmlCont=loadXMLDoc("files/books/"+object.id+"/META-INF/container.xml"); 
     opfFile=xmlCont.getElementsByTagName("container")[0].getElementsByTagName("rootfiles")[0].getElementsByTagName("rootfile")[0].getAttribute("full-path");   
     xmlCont=loadXMLDoc("files/books/"+object.id+"/"+opfFile); 
     xsl=loadXMLDoc("files/content.xsl");   
     displayXML(xmlCont, xsl, object.id); 
    } 
    catch (EventException) 
    { 
     alert(EventException.toString()); 
    } 
} 

function displayXML(xmlIn, xslIn, place) 
{ 
    // code for IE 
    if (window.ActiveXObject) 
    { 
      ex=xmlIn.transformNode(xslIn); 
      document.getElementById(place).innerHTML=ex; 
    } 
    // code for Mozilla, Firefox, Opera, etc. 
    else 
     if (document.implementation && document.implementation.createDocument) 
    { 
      xsltProcessor=new XSLTProcessor(); 
      xsltProcessor.importStylesheet(xslIn); 
      resultDocument = xsltProcessor.transformToFragment(xmlIn,document);  
      document.getElementById(place).appendChild(resultDocument); 
    } 
} 

function loadXMLDoc(dname) 
{ 
    if (window.ActiveXObject) 
    { 
     xhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    else 
    { 
     if (window.XMLHttpRequest) 
     { 
      xhttp=new XMLHttpRequest(); 
     } 
    } 
     try 
     { 
      xhttp.open("GET",dname,false); 
      xhttp.send(""); 
      return xhttp.responseXML; 
     } 
     catch (EventException) 
     { 
      alert("Could not open: " +dname); 
     } 
} 

javascript ======================================== xsl ===== =========================================

<?xml version="1.0"?> 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/package"> 
     <html> 
      <head> 
      </head> 
      <body> 
       <div id="METADATA"> 
        <p>Hello</p> 
        <div id="title"> 
         <xsl:value-of select='metadata/dc:title'/> 
        </div> 
        <br />      
        <div id="creator"> 
         <xsl:value-of select='metadata/dc:creator'/> 
        </div> 
        <br /> 
        <div id="publisher"> 
         <xsl:value-of select='metadata/dc:publisher'/> 
        </div> 
        <br /> 
        <div id="language"> 
         <xsl:value-of select='metadata/dc:language'/> 
        </div> 
        <br />       
        <div id="rights"> 
         <xsl:value-of select='metadata/dc:rights'/> 
        </div>       
        <br /> 
        <div id="subject"> 
         <xsl:value-of select='metadata/dc:subject'/> 
        </div> 
        <br /> 
        <div id="publicationDate"> 
         <xsl:value-of select='metadata/dc:date[@opf:event="publication"]'/> 
        </div> 
        <br /> 
        <div id="conversionDate"> 
         <xsl:value-of select='metadata/dc:date[@opf:event="conversion"]'/> 
        </div> 
        <br /> 
        <div id="source"> 
         <xsl:value-of select='metadata/dc:source'/> 
        </div> 
        <br /> 
       </div>     

      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

XSL = =============================================

的xml文件是在epub文件夾中找到的典型opf文件。 OPF(標準xml)============================================ ======

<?xml version='1.0' encoding='UTF-8'?> 

<package xmlns:opf="http://www.idpf.org/2007/opf" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="id"> 
    <metadata> 
    <dc:rights>Public domain in the USA.</dc:rights> 
    <dc:identifier id="id" opf:scheme="URI">http://www.gutenberg.org/ebooks/1065</dc:identifier> 
    <dc:creator opf:file-as="Poe, Edgar Allan">Edgar Allan Poe</dc:creator> 
    <dc:title>The Raven</dc:title> 
    <dc:language xsi:type="dcterms:RFC4646">en</dc:language> 
    <dc:subject>Poetry, English</dc:subject> 
    <dc:date opf:event="publication">1997-10-01</dc:date> 
    <dc:date opf:event="conversion">2010-06-08T08:59:43.376234+00:00</dc:date> 
    <dc:source>http://www.gutenberg.org/files/1065/1065-h/1065-h.htm</dc:source> 
    <meta content="item5" name="cover"/> 
    </metadata> 
    <manifest> 
    <item href="pgepub.css" id="item1" media-type="text/css"/> 
    <item href="0.css" id="item2" media-type="text/css"/> 
    <item href="1.css" id="item3" media-type="text/css"/> 
    <!--Chunk: size=31062--> 
    <item href="[email protected]@[email protected]@1065-h-0.htm" id="item4" media-type="application/xhtml+xml"/> 
    <item href="cover.jpg" id="item5" media-type="image/jpeg"/> 
    <item href="toc.ncx" id="ncx" media-type="application/x-dtbncx+xml"/> 
    </manifest> 
    <spine toc="ncx"> 
    <itemref idref="item4" linear="yes"/> 
    </spine> 
</package> 

OPF(標準xml)================================ ==================

任何幫助將不勝感激。 PS:如果有人知道爲什麼IE好好嘗試一下顯示任何XSL中創建的文檔,出現錯誤:「該樣式表不包含文檔元素的樣式表可能是空的,或者它可能不是一個格式良好的XML文檔」 它也會幫助,謝謝。

+0

考慮以顯示該代碼失敗的XML的樣品。當代碼失敗時,Firefox的錯誤控制檯顯示什麼?我強烈建議將代碼更改爲對要使用的功能使用功能檢查,例如, '如果(typeof運算XSLTProcessor中!= '不確定'){/ *現在使用XSLTProcessor中這裏* /}'不是檢查如'document.implementation'無關的功能。 –

+0

@MartinHonnen 我從我的try catch得到的第一個錯誤是來自firefox: 「[Exception ...」組件返回失敗代碼:0x80004005(NS_ERROR_FAILURE)[nsIXSLTProcessor.importStylesheet]「nsresult:」0x80004005(NS_ERROR_FAILURE)「location :「JS frame :: file:/// C:/xampp/htdocs/files/options.js :: displayXML :: line 54」data:no]「 54行是:」xsltProcessor。importStylesheet(xslIn);「 – Gerardlamo

+0

我測試了一個隨機opf文件,手動添加鏈接到樣式表,然後在web瀏覽器中打開opf文件,它在Firefox中表示:」加載樣式表時出錯:未知錯誤()「我已經通過樣式表掃描了我不知道發生了什麼 – Gerardlamo

回答

1

您的樣式表失敗,因爲它無效。它使用dc:opf:命名空間在幾個地方,但沒有申報。要解決這個問題,請將標記xsl:stylesheet更改爲包含必要的名稱空間聲明。由於源XML使用默認名稱空間,因此您還需要爲這些節點使用名稱空間。我還修改了XSLT,以顯示它如何變得更簡潔:

<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:dc="http://purl.org/dc/elements/1.1/" 
       xmlns:opf="http://www.idpf.org/2007/opf"> 
    <xsl:template match="opf:package"> 
    <html> 
     <head> 
     </head> 
     <body> 
     <xsl:apply-templates select="opf:metadata" /> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="opf:metadata"> 
    <div id="METADATA"> 
     <p>Hello</p> 
     <xsl:apply-templates select='dc:title'/> 
     <xsl:apply-templates select='dc:creator'/> 
     <xsl:apply-templates select='dc:publisher'/> 
     <xsl:apply-templates select='dc:language'/> 
     <xsl:apply-templates select='dc:rights'/> 
     <xsl:apply-templates select='dc:subject'/> 
     <xsl:apply-templates select='dc:date[@opf:event="publication"]'> 
     <xsl:with-param name='id' select='"publicationDate"' /> 
     </xsl:apply-templates> 
     <xsl:apply-templates select='dc:date[@opf:event="conversion"]'> 
     <xsl:with-param name='id' select='"conversionDate"' /> 
     </xsl:apply-templates> 
     <xsl:apply-templates select='dc:source'/> 
    </div> 
    </xsl:template> 

    <xsl:template match='opf:metadata/*'> 
    <xsl:param name='id' select='local-name()' /> 

    <div id='{$id}'> 
     <xsl:value-of select='.'/> 
    </div> 
    <br /> 
    </xsl:template> 

</xsl:stylesheet> 
+0

我還發現,當使用''開始我的樣式表時,它會顯示所有數據opf的元數據,無論我的樣式表在這個模板中有多少 現在我的問題是,當我拿走包裝時,它不顯示標題,因爲我希望在這種情況下'

T T

'它顯示的是「TT」現在可能是什麼問題,我想是什麼問題,但是什麼? – Gerardlamo

+0

正如Martin在他的回答中指出的那樣,一個默認的命名空間,您需要在XPath中使用名稱空間。在這種情況下,該缺省命名空間與'opf'命名空間相同,因此也可以用於'package'和'metadata'節點。 – JLRishe

0

你需要採取的命名空間,包括默認的命名空間考慮在你的XSLT代碼編寫路徑表達式時。

所以,你的代碼應該是

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:opf="http://www.idpf.org/2007/opf" 
    xmlns:dc="http://purl.org/dc/elements/1.1/" 
    exclude-result-prefixes="opf dc"> 

    <xsl:output method="html" indent="yes"/> 

    <xsl:template match="/opf:package"> 
       <div id="METADATA"> 
        <p>Hello</p> 
        <div id="title"> 
         <xsl:value-of select='opf:metadata/dc:title'/> 
        </div> 
        <br />      
        <div id="creator"> 
         <xsl:value-of select='opf:metadata/dc:creator'/> 
        </div> 
        <br /> 
        <div id="publisher"> 
         <xsl:value-of select='opf:metadata/dc:publisher'/> 
        </div> 
        <br /> 
        <div id="language"> 
         <xsl:value-of select='opf:metadata/dc:language'/> 
        </div> 
        <br />       
        <div id="rights"> 
         <xsl:value-of select='opf:metadata/dc:rights'/> 
        </div>       
        <br /> 
        <div id="subject"> 
         <xsl:value-of select='opf:metadata/dc:subject'/> 
        </div> 
        <br /> 
        <div id="publicationDate"> 
         <xsl:value-of select='opf:metadata/dc:date[@opf:event="publication"]'/> 
        </div> 
        <br /> 
        <div id="conversionDate"> 
         <xsl:value-of select='opf:metadata/dc:date[@opf:event="conversion"]'/> 
        </div> 
        <br /> 
        <div id="source"> 
         <xsl:value-of select='opf:metadata/dc:source'/> 
        </div> 
        <br /> 
       </div>     

    </xsl:template> 
</xsl:stylesheet> 

我已刪除了htmlbody元素作爲你的Javascript代碼表示要創建的HTML片段將包括已有的結構現有的HTML文檔中元素。

+0

+1正確答案,非常感謝@Martin – Gerardlamo