0

I'm going through a book called ineasysteps, and I am stuck on one of the questions about XSL and XML.

For some reason, I am not able to open up the xml document in the browser. I believe my code is incorrect, but please let me know what I'm doing wrong here.

Thank You, Code is posted Below:

Here is the XML Document: 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>

Here is the value-of.xsl document:

     <?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
    Possible duplicate: **[XSLT not working in web browser](http://stackoverflow.com/questions/29941662/xslt-not-working-in-web-browser)** – kjhughes Apr 05 '16 at 23:33

1 Answers1

0

Consider using a second template for the car:doc level and then car:item level. Also, use exclude-prefixes to remove the namespaces in root <html> tag:

<?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>

Transformed Output

<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>
Parfait
  • 104,375
  • 17
  • 94
  • 125