Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

xml xsl example not working

Hi,

I am trying below example

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

<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>

<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>

<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>

<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>

<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>

</breakfast_menu>

Open in new window


<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="breakfast_menu/food">
  <div style="background-color:teal;color:white;padding:4px">
    <span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
    <xsl:value-of select="price"/>
    </div>
  <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
    <p>
    <xsl:value-of select="description"/>
    <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
    </p>
  </div>
</xsl:for-each>
</body>
</html>

Open in new window


from link
http://www.w3schools.com/xml/xml_xsl.asp

I am getting error when i select xml and xsl and 'run as' xsl transformation
No compatible processor is found for XSLT version null

i supposed to get
Belgian Waffles - $5.95
two of our famous Belgian Waffles with plenty of real maple syrup (650 calories per serving)

Strawberry Belgian Waffles - $7.95
light Belgian waffles covered with strawberries and whipped cream (900 calories per serving)

Berry-Berry Belgian Waffles - $8.95
light Belgian waffles covered with an assortment of fresh berries and whipped cream (900 calories per serving)

French Toast - $4.50
thick slices made from our homemade sourdough bread (600 calories per serving)

Homestyle Breakfast - $6.95
two eggs, bacon or sausage, toast, and our ever-popular hash browns (950 calories per serving)

Please advise
Avatar of hielo
hielo
Flag of Wallis and Futuna image

As far as I know, every xsl stylesheet requires <xsl:stylesheet> (or <xsl:transform> -- http://www.w3.org/TR/xslt#stylesheet-element) element, which contains all your transformations.

Try saving the following as "breakfast.xsl" in the same folder where you have "breakfast.xml":
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html"/>
	
	<xsl:template match="/"> 
        <html>
        <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
        <xsl:for-each select="breakfast_menu/food">
          <div style="background-color:teal;color:white;padding:4px">
            <span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
            <xsl:value-of select="price"/>
            </div>
          <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
            <p>
            <xsl:value-of select="description"/>
            <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
            </p>
          </div>
        </xsl:for-each>
        </body>
        </html>
	</xsl:template>
</xsl:stylesheet>

Open in new window


To test the above xsl file directly on the browser, in breakfast.xml "link" to the xsl file above:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="breakfast.xsl"?>
<breakfast_menu>...</breakfast_menu>

Lastly, open Firefox, and open breakfast.xml

FYR:
https://msdn.microsoft.com/en-us/library/ms256204%28v=vs.110%29.aspx
http://www.ibm.com/developerworks/library/x-tiphtml/
http://www.ibm.com/developerworks/library/x-tipxslt/index.html
Avatar of gudii9

ASKER

both in same folder of eclipse as attached. Do i need to refer xsl name in xml somewhere or otherway?

i wan to run in the eclipse though not in browser
xmlXsl.png
Use the xsl file I posted above.  Your screenshot shows the one that doesn't have the <xsl:stylesheet> element.  As for the transformation, refer to:
https://docs.oracle.com/javase/tutorial/jaxp/xslt/transformingXML.html

Look on the heading "Writing the Basic Program"
Avatar of gudii9

ASKER

My code on other laptop has below
 <xsl:template match=
 But it does not have below
 <h2> <xsl:apply-templates
what is the difference between these two. when to use which one and why we need templates in XSL.
I am also reading that link which is good.
please advise
You are not obligated to create templates for your nodes -- ex: <xsl:template match="food">...</xsl:template>.  You can do all the processing in the "starting/entry" template ( in the example you posted, this is <xsl:template match="/">...</xsl:template> -- if you are familiar with c/c++, think of it a main).  However, the advantage of templates is that they simplify the processing logic significantly.

If you are completely new to xsl, I suggest you invest some time reading at least the first three chapters of Tidwell's book:
https://www.google.com/search?tbm=bks&hl=en&q=xsl%2B%22The+Obligatory+Hello+World+Example%22+site%3Abooks.google.com

If you feel like skipping Chapter 1, I suggest you at least read the "XML Basics" section. The section titled "How a Stylesheet is Processed" in Chapter 2 is particularly illuminating. Chapter three covers node types and the "context node".  These chapters should demystify these concepts and give you a good foundation into xsl.

Regards,
Hielo
Avatar of gudii9

ASKER

i di d added
<?xml-stylesheet type="text/xsl" href="breakfast.xsl"?>
to xml file i see in compatible xsl error as attached. please advise.
i will try to buy that book as well soon.
compatibleErr.png
>> <?xml-stylesheet type="text/xsl" href="breakfast.xsl"?>
That was meant for you to do testing by opening the XML file directly through the browser.  I don't know if Eclipse has built-in capabilities to do XSL transformation, but to the oracle link I referred you to earlier shows you how to do so in java.
Avatar of gudii9

ASKER

http://www.vogella.com/tutorials/XSLT/article.html

i tried this example which works fine though
Avatar of gudii9

ASKER

<?xml version="1.0"?>
  <!-- This is a comment -->
<people>
  <address type="personal">
    <name>Lars </name>
    <street> Test </street>
    <telephon number="0123" />
  </address>
  <address type="personal">
    <name>Joe </name>
    <street> Test2 </street>
    <telephon number="1234" />
  </address>
  <address type="business">
    <name>Jim</name>
    <street> Test3 </street>
    <telephon number="2345" />
  </address>
</people> 

Open in new window


<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="xml" />

  <!-- Copy everything -->
  <xsl:template match="node()">
    <xsl:copy>
      <xsl:copy-of select="@*" />
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

  <!-- Do some adjustments for the address -->
  <xsl:template match="address">
    <xsl:element name="place-where-person-live">
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>

  <!-- Put the name in a <hubba> tag -->
  <xsl:template match="name">
    <xsl:element name="name">
      <hubba>
        <xsl:apply-templates />
      </hubba>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet> 

Open in new window


ABOVE code from link is similar to my code below

<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="breakfast_menu/food">
  <div style="background-color:teal;color:white;padding:4px">
    <span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
    <xsl:value-of select="price"/>
    </div>
  <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
    <p>
    <xsl:value-of select="description"/>
    <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
    </p>
  </div>
</xsl:for-each>
</body>
</html>

Open in new window

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="breakfast.xsl"?>
<breakfast_menu>

<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>

<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>

<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>

<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>

<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>

</breakfast_menu>

Open in new window

still do not work. I may missing some thing.
please advise
Like I said, '<?xml-stylesheet type="text/xsl" href="breakfast.xsl"?>' was meant for you to test on the browser.  Since you are not using the browser, get rid of it.
Avatar of gudii9

ASKER

i removed still same kind of err.
err3.png
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   version="1.0">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="breakfast_menu/food">
  <div style="background-color:teal;color:white;padding:4px">
    <span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
    <xsl:value-of select="price"/>
    </div>
  <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
    <p>
    <xsl:value-of select="description"/>
    <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
    </p>
  </div>
</xsl:for-each>
</body>
</xsl:stylesheet>

Open in new window


<?xml version="1.0"?>

<breakfast_menu>

<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>

<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>

<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>

<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>

<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>

</breakfast_menu>

Open in new window


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


Belgian Waffles
$5.95
Two of our famous Belgian Waffles with plenty of real maple syrup
650



Strawberry Belgian Waffles
$7.95
Light Belgian waffles covered with strawberries and whipped cream
900



Berry-Berry Belgian Waffles
$8.95
Light Belgian waffles covered with an assortment of fresh berries and whipped cream
900



French Toast
$4.50
Thick slices made from our homemade sourdough bread
600



Homestyle Breakfast
$6.95
Two eggs, bacon or sausage, toast, and our ever-popular hash browns
950

Open in new window

it seemed worked as above finally