2012-12-01 31 views
2

我從少數XML格式轉換爲一種標準。 我的XSL看起來像下列操作之一:在Firefox和IE中使用XSLT進行XML到XML轉換

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/> 

    <xsl:template match="list | store"> 
     <list> 
      <xsl:for-each select="item | product | product-store"> 
      <item> 
       <name> 
        <xsl:choose> 
         <xsl:when test="name"><xsl:value-of select="substring-before(name, ' ')" /></xsl:when> 
         <xsl:otherwise><xsl:value-of select="name | title" /></xsl:otherwise> 
        </xsl:choose> 
       </name> 
       <desc> 
        <xsl:choose> 
         <xsl:when test="name"><xsl:value-of select="substring-after(name, ' ')" /></xsl:when> 
         <xsl:otherwise><xsl:value-of select="desc" /></xsl:otherwise> 
        </xsl:choose> 
       </desc> 
       <nr><xsl:value-of select="index | number" /></nr> 
      </item> 
      </xsl:for-each> 
     </list> 
    </xsl:template> 
</xsl:stylesheet> 

我的示例XML是

<?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
<?xml-stylesheet type="text/xsl" href="transform.xsl"?> 
<list> 
    <item> 
     <index>1362242627</index> 
     <name>test 22</name> 
    </item> 
    <item> 
     <index>2362625609</index> 
     <name>test 4</name> 
    </item> 
    <item> 
     <index>736274650</index> 
     <name>test 76</name> 
    </item> 
</list> 

爲什麼它不正確地在瀏覽器中顯示如Firefox 17,IE9和谷歌Chrome?它們顯示爲普通文本,但返回類型爲「text/xml」。 它僅在Opera中正常工作。

回答

6

我認爲問題是要告訴什麼是「正確的」顯示。像Firefox或IE這樣的瀏覽器假設一旦你將一個xml-stylesheet類型爲text/xsl的處理指令的XML文檔加載到瀏覽器窗口中,你就想將XML轉換爲瀏覽器知道要呈現的東西,比如HTML或者像HTML(X) SVG(或加MathML)。然而,你的樣式表採用XML輸入並將其轉換爲瀏覽器未知的XML結果格式,因此它所做的只是呈現結果樹中文本節點的內容。 Opera似乎將XML輸入轉換爲XML結果,但似乎認識到結果格式未知,並決定渲染結果的源樹。這可能是你喜歡的,但我不確定是否有規範要求這種行爲。

+0

這似乎是一個好答案。剛纔我使用了一些工具來轉換XSL,並且它已被正確轉換。 – deem

0

如果在Firefox和Chrome中使用xsl:output="text",則換行符和製表符字符的預先設置將生效。具有諷刺意味的是,IE在文本模式下忽略了縮進。下面是一個說明這個自引用樣式表:

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type="text/xsl" href="newline-indent.xml"?> 
<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="" 
       > 

<!-- Output HTML doctype with text/html content-type and without XML declaration--> 
<xsl:output method="text" encoding="utf-8" version="1.0" media-type="text/plain" indent="yes" standalone="no" omit-xml-declaration="no"/> 

<!-- Output the HTML markup--> 
<xsl:template xml:space="preserve" match="/"> 
    <root> 
    <child>1 &#13;&#10;</child> 
    <child> 
     <grandchild>&#09; 1.1 &#13;&#10;</grandchild> 
    </child> 
    <child> 
     <grandchild> 
     <great-grandchild>&#09; &#09; 1.1.1</great-grandchild> 
     </grandchild> 
    </child> 
    </root> 
</xsl:template> 
</xsl:stylesheet> 

Mozilla bug下面的註釋解釋了爲什麼XML序列化不會對XML命名空間的工作:

在壁虎的當前版本, XML序列化器用於序列化XHTML內容。

使用style元素和文字空間在標籤格式在IE中輸出:

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type="text/xsl" href="newline-indent-ie.xml"?> 
<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="" 
       > 

<xsl:output method="xml" encoding="utf-8" version="1.0" media-type="application/xml" indent="yes" standalone="no" omit-xml-declaration="no"/> 

<xsl:template match="/"> 
    <style>* { white-space:pre-wrap; }</style> 
    <root> 
    <child >1</child> 
    <child> 
     <grandchild >1.1</grandchild> 
    </child> 
    <child> 
     <grandchild> 
     <great-grandchild >1.1.1</great-grandchild> 
     </grandchild> 
    </child> 
    </root> 
</xsl:template> 
</xsl:stylesheet> 

參考