XML不使用XSL显示

时间:2013-04-14 10:54:43

标签: xml xslt

加载XML文件时,没有数据显示。它似乎没有使用XSL文件,我只是得到一个空白屏幕。请有人看看,看看为什么这样做。提前致谢。

继承我的代码:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="computers.xsl"?>
<!DOCTYPE computers SYSTEM "computers.dtd">
<computers>
    <computer>
    <cpu ghz="3.5" unlocked="Yes">Intel i7 3770k</cpu>
    <motherboard>MSI M-Power</motherboard>
    <ram>8</ram>
    <storage>
        <hdd>1000</hdd>
        <ssd>128</ssd>
    </storage>
    <gpu group="ATI">7950</gpu>
    <psu>750</psu>
</computer>
<computer>
    <cpu ghz="3.5" unlocked="Yes">Intel i5 3570k</cpu>
    <motherboard>ASRock Extreme 4</motherboard>
    <ram>8</ram>
    <storage>
        <hdd>1000</hdd>
    </storage>
    <gpu group="ATI">7870</gpu>
    <psu>600</psu>
</computer>
<computer>
    <cpu ghz="3.5" unlocked="No">Intel i5 3550</cpu>
    <motherboard>ASRock Extreme 3</motherboard>
    <ram>8</ram>
    <storage>
        <hdd>500</hdd>
    </storage>
    <gpu group="GeForce">9600GT</gpu>
    <psu>500</psu>
</computer>
 </computers>

DTD:

<!ELEMENT computer (cpu,motherboard,ram,storage,gpu,psu) >
<!ELEMENT cpu (#PCDATA)>
<!ELEMENT motherboard (#PCDATA)>
<!ELEMENT ram (#PCDATA)>
<!ELEMENT storage(hdd,ssd*)>
<!ELEMENT hdd (#PCDATA)>
<!ELEMENT ssd (#PCDATA)>
<!ELEMENT gpu (#PCDATA)>
<!ELEMENT psu (#PCDATA)>

<!ATLIST cpu
ghz CDATA #REQUIRED
unlocked (Yes|No) "No">
<!ATLIST gpu
group (ATI|GeForce) "ATI">

XSL:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
    <title>HELLO WORLD</title>
</head>
<body>
    <xsl:for-each select="computers/computer">
        <table border="1">
            <tr>
                <td>
                    <xsl:value-of select="cpu"/>
                </td>
                <td>
                    <xsl:value-of select="cpu/@ghz"/>
                </td>
                <td>
                    <xsl:value-of select="cpu/@unlocked"/>
                </td>
            </tr>
            <tr>
                <td>
                    <xsl:value-of select="motherboard"/>
                </td>
            </tr>
            <tr>
                <td>
                    <xsl:value-of select="ram"/>
                </td>
            </tr>
            <tr>
                <td>
                    <xsl:for-each select="storage"/>
                        <xsl:value-of select="hdd"/>
                        <xsl:value-of select="ssd"/>
                    </xsl:for-each>
                </td>
            </tr>
            <tr>
                <td>
                    <xsl:value-of select="gpu"/>
                </td>
            </tr>
            <tr>
                <td>
                    <xsl:value-of select="psu"/>
                </td>
            </tr>
        </table>
    </xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

2 个答案:

答案 0 :(得分:2)

您的XSLT格式不正确,您的XSLT处理器(我怀疑是浏览器)可能只是拒绝处理它。您应该从此for-each

中删除斜杠
<xsl:for-each select="storage"/>

一旦你这样做,我相信XSLT将成功运作。

答案 1 :(得分:1)

除了XSLT样式表格式错误(不是格式良好的XML)文档之外,似乎还有其他问题。

<强> 1。替换第33行:

                <xsl:for-each select="storage"/>

<强>与

                <xsl:for-each select="storage">

<强> 2。从源XML文档中删除<!DOCTYPE computers SYSTEM "computers.dtd"> - 它不是由XSLT转换使用的,并且是不必要的。 XSLT转换仅使用有关ID属性的DTD数据 - 并且在提供的DTD 中没有。

在更换和删除之后,转换运行没有问题,并且应用于提供的XML文档时:

<computers>
    <computer>
        <cpu ghz="3.5" unlocked="Yes">Intel i7 3770k</cpu>
        <motherboard>MSI M-Power</motherboard>
        <ram>8</ram>
        <storage>
            <hdd>1000</hdd>
            <ssd>128</ssd>
        </storage>
        <gpu group="ATI">7950</gpu>
        <psu>750</psu>
    </computer>
    <computer>
        <cpu ghz="3.5" unlocked="Yes">Intel i5 3570k</cpu>
        <motherboard>ASRock Extreme 4</motherboard>
        <ram>8</ram>
        <storage>
            <hdd>1000</hdd>
        </storage>
        <gpu group="ATI">7870</gpu>
        <psu>600</psu>
    </computer>
    <computer>
        <cpu ghz="3.5" unlocked="No">Intel i5 3550</cpu>
        <motherboard>ASRock Extreme 3</motherboard>
        <ram>8</ram>
        <storage>
            <hdd>500</hdd>
        </storage>
        <gpu group="GeForce">9600GT</gpu>
        <psu>500</psu>
    </computer>
</computers>

以下(我猜是想要的)结果生成

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

      <title>HELLO WORLD</title>
   </head>
   <body>
      <table border="1">
         <tr>
            <td>Intel i7 3770k</td>
            <td>3.5</td>
            <td>Yes</td>
         </tr>
         <tr>
            <td>MSI M-Power</td>
         </tr>
         <tr>
            <td>8</td>
         </tr>
         <tr>
            <td>1000128</td>
         </tr>
         <tr>
            <td>7950</td>
         </tr>
         <tr>
            <td>750</td>
         </tr>
      </table>
      <table border="1">
         <tr>
            <td>Intel i5 3570k</td>
            <td>3.5</td>
            <td>Yes</td>
         </tr>
         <tr>
            <td>ASRock Extreme 4</td>
         </tr>
         <tr>
            <td>8</td>
         </tr>
         <tr>
            <td>1000</td>
         </tr>
         <tr>
            <td>7870</td>
         </tr>
         <tr>
            <td>600</td>
         </tr>
      </table>
      <table border="1">
         <tr>
            <td>Intel i5 3550</td>
            <td>3.5</td>
            <td>No</td>
         </tr>
         <tr>
            <td>ASRock Extreme 3</td>
         </tr>
         <tr>
            <td>8</td>
         </tr>
         <tr>
            <td>500</td>
         </tr>
         <tr>
            <td>9600GT</td>
         </tr>
         <tr>
            <td>500</td>
         </tr>
      </table>
   </body>
</html>

所有四种浏览器:IE,FF,Safari和Opera正确处理computers.xml并生成想要的显示

我无法通过Chrome获取任何内容 - 众所周知,此浏览器在处理本地文件时会遇到问题。