I'm getting this error when trying to convert a XML document with XSL. I've only just started coding with XML, so I am sure I am missing something simple, but cant seem to find much information about it online.
Booklist.XML
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="booklist.xsl"?>
<CATALOG>
<BOOK>
<NAME>
<TITLE>Caring for Sheep</TITLE>
</NAME>
<PAGES> 60 </PAGES>
<PRICE>$34.99</PRICE>
</BOOK>
</CATALOG>
And my XSL File:
Booklist.XSL
<?xml version="1.0"?>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="CATALOG/BOOK/NAME">
<div style="background-color:teal;color:white;padding:4px">
<xsl:value-of select="name">;
</div>
</xsl:for-each>
<xsl:for-each select="CATALOG/BOOK">
<div style="background-color:teal;color:white;padding:4px">
</div>
</xsl:for-each>
</body>
Can anyone see an error here?
You're sort of mixing the Literal Result Element as Stylesheet simplified syntax with the regular syntax by putting a literal result element <body> outside any template, yet not at the top level. If you want to use this simplified syntax, get rid of the <xsl:stylesheet> and <xsl:output> elements, so that <body> is at the top level.
Also, XML and XPath are case-sensitive with regard to element names, so you'll need to fix the inconsistency between e.g. "catalog" and <CATALOG>.
Well, if the for some reasons which are hard to figure out the simplified syntax doesn't work for you I would suggest using the "normal coding", which would be something like this in your case:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:template match="/">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="/CATALOG/BOOK/NAME">
<div style="background-color:teal;color:white;padding:4px">
<xsl:value-of select="name"/>
</div>
</xsl:for-each>
<xsl:for-each select="/CATALOG/BOOK">
<div style="background-color:teal;color:white;padding:4px">
</div>
</xsl:for-each>
</body>
</xsl:template>
</xsl:stylesheet>
Related
For some reason, colons in the tags of my XML files seem to be causing problems when I'm trying to render it in HTML format using XSL. I think it's the im: portion of the tag name, so are special characters just not allowed? Below is my xml.
<?xml version='1.0' encoding='ISO-8859-1'?>
<?xml-stylesheet type="text/xsl" href="formatting.xsl" xmlns:im="im"?>
<im:Command xmlns:im="im">
<im:CommandHdr>
<im:Name>John</im:Name>
<im:DisplayName>Jo</im:DisplayName>
</im:CommandHdr>
</im:Command>
Here is my XSL.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="im:Command/im:CommandHdr">
<tr>
<td><xsl:value-of select="im:Name"/></td>
<td><xsl:value-of select="im:DisplayName"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
How do I fix this problem.
Hi Im having trouble linking my XML and XSLT, My XML is long but heres a extract, The problem is my XSLT is not formatting it, I am trying to get my XML displayed under each other with a title. forgive me but english is not my first language
XML:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="Product_List.xsl"?>
<ProductCatalogue>
<drinks>
<product>
<name Product_Code="D001">Lemonade</name>
<price>6.50</price>
<amount>20</amount>
<supplier>Coca-Cola</supplier>
</product>
</drinks>
</ProductCatalogue>
XSLT
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h3>Product List</h3>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="drinks">
<div style="color:#0000FF">
<h3>
<xsl:value-of select="name"/>
</h3>
</div>
<div>
<xsl:value-of select="name#Product_code"/>
</div>
<p>Price : <xsl:value-of select="price"/></p>
<p>Supplier : <xsl:value-of select="supplier"/></p>
<p>Amount : <xsl:value-of select="amount"/></p>
</xsl:template>
</xsl:stylesheet>
I believe its a problem with my templates but im not sure how its fixed
Yes, there is indeed a problem with your templates. The easiest way to make your transformation actually output the values is to change the second template match, originally
<xsl:template match="drinks">
to
<xsl:template match="product">
Inside this template, you are selecting elements like name and price, which are child elements of product, not of drinks.
Also, change
<xsl:value-of select="name#Product_code"/>
to
<xsl:value-of select="name/#Product_Code"/>
XML and all the technologies related to it are case-sensitive - Product_code is not the same as Product_Code.
Then, the output will be
<html>
<body>
<h3>Product List</h3>
<div style="color:#0000FF">
<h3>Lemonade</h3>
</div>
<div>D001</div>
<p>Price : 6.50</p>
<p>Supplier : Coca-Cola</p>
<p>Amount : 20</p>
</body>
</html>
I am attempting to write an XSLT for an XML file I have and I am getting no results.
A simple example of my XML:
<Monsters>
<Monster>
<Name>Dracula</Name>
<Actor>Bela Lugosi</Actor>
</Monster>
</Monsters>
And a simple XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
</head>
<body style="background: #FFFFCC">
<xsl:for-each select="Monsters/Monster">
<xsl:value-of select="Name" />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl>
The resulting display is entirely empty and I just know it's something silly that I'm missing.
You don't have a proper closing tag for xsl:stylesheet element.
I am trying to use the tokenize function in XSLT and it is not working
<xsl:variable name="stringList" select="tokenize('XPath,is,fun', ',')"/>
<xsl:for-each select="$stringList">
<xsl:value-of select="." />
</xsl:for-each>
Is this anything wrong in this ? I tried this both in eclipse and in the w3schools tutorial editor
Actual code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rg="http://www....."
xmlns="http://wwww.w3.org/1999/xhtml" >
<xsl:template match="/">
<html>
<head>
<title>Temp</title>
</head>
<body>
<xsl:variable name="stringList" select="tokenize('XPath,is,fun', ',')" />
<xsl:for-each select="$stringList">
<xsl:value-of select="." /><br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Tokenize is actually an XSLT 2.0 function, and so you will need to use an XSLT processor that supports XSLT 1.0. The editor at W3Schools is XSLT 1.0 only.
I tried your XSLT at http://xslttest.appspot.com/ as an example, and it works happily.
I'm starting using XSLT and write this scipt:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8" />
<xsl:template match="span[#class='thumb']" >
Link: <xsl:value-of select="$base" /><xsl:value-of select="a/#href" />
</xsl:template>
<xsl:template match="/">
Base href: <xsl:value-of select="$base" />
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
And using this command:
xsltproc --html --param base "'http://example.com'" lista.xslt test.html
I need to get list of Links, but I get whole page on output. What's wrong? How can I get it works?
There are some default templates which are unseen here. The really easy way to resolve it is to just explicitly limit to the span elements you're matching as below. Otherwise, you can override the default templates.
<xsl:template match="/">
Base href: <xsl:value-of select="$base" />
<xsl:apply-templates select="//span[#class='thumb']" />
</xsl:template>
There's a default template that matches essentially everything if you let it. Your 4th last line calls that template.
That's part of the problem. The rest can probably be taking care of by matching just the stuff you're looking for, directly in the top-level template.