2016-04-05 98 views
0

我正在閱讀一本名爲ineasysteps的書,並且我遇到了有關XSL和XML的問題之一。爲XML/XSL選擇元素值

出於某種原因,我無法在瀏覽器中打開xml文檔。我相信我的代碼不正確,但請讓我知道我在這裏做錯了什麼。

謝謝你,代碼是下面貼:

這裏是XML文檔:cars.xml

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

<!-- XML in easy steps - Page 99. --> 


<?xml-stylesheet type = "text/xsl" href = "value-of.xsl" ?> 

<car:doc 
xmlns:xsi = 
"http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation = 
"http://www.ineasysteps.com/xsd cars.xsd" 

xmlns:car = "http://www.ineasysteps.com/xsd" > 

<car:item id = "corvette"> 
    <car:make>Chevrolet</car:make> 
    <car:model>Corvette</car:model> 
    <car:ltr>6.0</car:ltr> 
    <car:cyl>8</car:cyl> 
    <car:hp>400</car:hp> 
    <car:price>53000</car:price> 
</car:item> 

<car:item id = "viper"> 
    <car:make>Dodge</car:make> 
    <car:model>Viper</car:model> 
    <car:ltr>8.3</car:ltr> 
    <car:cyl>10</car:cyl> 
    <car:hp>510</car:hp> 
    <car:price>85000</car:price> 
</car:item> 


<car:item id = "solstice"> 
    <car:make>Pontiac</car:make> 
    <car:model>Solstice</car:model> 
    <car:ltr>2.4</car:ltr> 
    <car:cyl>4</car:cyl> 
    <car:hp>177</car:hp> 
    <car:price>22000</car:price> 
</car:item> 

這裏是價值of.xsl文件:

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

    <!-- XML in easy steps - Page 100. --> 

    <xsl:stylesheet version = "1.0" 
    xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" 
    xmlns:c = "http://www.ineasysteps.com/xsd" > 

    <xsl:output method="html" encoding="UTF-8" indent="yes"/> 


    <xsl:template match = "/"> 

    <html> <head><title>XSL Output</title> </head> <body> 

    <ul style = "list-style-type:square;color:red"> 

    <li> <xsl:value-of select = "car:item/car:make" /> </li> 
    <li> <xsl:value-of select = "car:item/car:model" /> </li> 
    <li> <xsl:value-of select = "car:item/car:ltr" /> Liters </li> 
    <li> <xsl:value-of select = "car:item/car:cyl" /> Cylinders</li> 
    <li> <xsl:value-of select = "car:item/car:hp" /> Horsepower</li> 
    <li> <xsl:value-of select = "car:item/car:price" /> </li> 

    </ul> 

    </body> </html> 

    </xsl:template> 

    </xsl:stylesheet> 
+2

可能重複:** [XSLT在Web瀏覽器不工作(HTTP://計算器。 COM /問題/ 29941662/XSLT - 不工作,在-Web瀏覽器)** – kjhughes

回答

0

考慮在car:doc級別使用第二個模板,然後使用car:item級別。此外,使用排除前綴去除根<html>標籤的命名空間:

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

<!-- XML in easy steps - Page 100. -->  
<xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" 
           xmlns:car = "http://www.ineasysteps.com/xsd" 
           exclude-result-prefixes="xsl car">  
<xsl:output method="html" encoding="UTF-8" indent="yes"/> 

    <!-- ROOT LEVEL --> 
    <xsl:template match="car:doc"> 
    <html> 
     <head><title>XSL Output</title></head> 
     <body> 
     <xsl:apply-templates select="car:item"/> 
     </body> 
    </html>  
    </xsl:template> 

    <!-- FIRST CHILD LEVEL --> 
    <xsl:template match="car:item">  
    <ul style = "list-style-type:square;color:red"> 
     <li> <xsl:value-of select = "car:make" /> </li> 
     <li> <xsl:value-of select = "car:model" /> </li> 
     <li> <xsl:value-of select = "car:ltr" /> Liters </li> 
     <li> <xsl:value-of select = "car:cyl" /> Cylinders</li> 
     <li> <xsl:value-of select = "car:hp" /> Horsepower</li> 
     <li> <xsl:value-of select = "car:price" /> </li>  
    </ul>  
    </xsl:template>  
</xsl:stylesheet> 

變換的輸出

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>XSL Output</title> 
</head> 
<body> 
<ul style="list-style-type:square;color:red"> 
<li>Chevrolet</li> 
<li>Corvette</li> 
<li>6.0 Liters </li> 
<li>8 Cylinders</li> 
<li>400 Horsepower</li> 
<li>53000</li> 
</ul> 
<ul style="list-style-type:square;color:red"> 
<li>Dodge</li> 
<li>Viper</li> 
<li>8.3 Liters </li> 
<li>10 Cylinders</li> 
<li>510 Horsepower</li> 
<li>85000</li> 
</ul> 
<ul style="list-style-type:square;color:red"> 
<li>Pontiac</li> 
<li>Solstice</li> 
<li>2.4 Liters </li> 
<li>4 Cylinders</li> 
<li>177 Horsepower</li> 
<li>22000</li> 
</ul> 
</body> 
</html>