i am following this link
i want to convert an xml to csv
i have an xml and written and xsl for it
openning xml with href to the xsl work on internet explorer
but runing the code from this link return an error
Can not resolve namespace prefix: xmlns
what should fix it ?
i have a working xml & xsl
xml
<?xml version='1.0' encoding='utf-8'?>
<?xml-stylesheet type="text/xsl" href="student2.xsl"?>
<rankings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ign="http://api.DomainName.com/2.0" count="438" total="438" offset="0" xsi:schemaLocation="http://api.DomainName.com/2.0 http://api.DomainName.com/2.0/api.xsd" >
<ranking keyword="80s fancy dress">
<ranks>
<rank week="201526" country="uk" searchengine="google_uk_en">NR</rank>
<rank week="201527" country="uk" searchengine="google_uk_en">NR</rank>
<rank week="201528" country="uk" searchengine="google_uk_en">NR</rank>
<rank week="201529" country="uk" searchengine="google_uk_en">NR</rank>
<rank week="201530" country="uk" searchengine="google_uk_en">NR</rank>
<rank week="201531" country="uk" searchengine="google_uk_en">NR</rank>
</ranks>
</ranking>
</rankings>
xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://api.DomainName.com/2.0" exclude-result-prefixes=xmlns>
<xsl:output method="text" encoding="UTF-8" omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr bgcolor="#9acd32">
</tr>
<xsl:for-each select="rankings/ranking/ranks/rank">
<tr>
<keyword><xsl:value-of select="../../#keyword"/></keyword>
<xsl:text>,</xsl:text>
<week><xsl:value-of select="#week"/></week>
<xsl:text>,</xsl:text>
<country><xsl:value-of select="#country"/></country>
<xsl:text>,</xsl:text>
<searchengine><xsl:value-of select="#searchengine"/></searchengine>
<xsl:text>,</xsl:text>
<rank><xsl:value-of select="../rank"/></rank>
<xsl:value-of select="." />
<xsl:text>
</xsl:text>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
java code
after running the java code from the above link
i get an error
(Location of error unknown)org.xml.sax.SAXException: Can not resolve namespace prefix: xmlns
Exception in thread "main" java.lang.NullPointerException
at org.apache.xalan.transformer.TransformerImpl.createSerializationHandler(TransformerImpl.java:1171)
at org.apache.xalan.transformer.TransformerImpl.createSerializationHandler(TransformerImpl.java:1060)
at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1268)
at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1251)
what should fix it ?
To eliminate the error, you need to remove this:
exclude-result-prefixes=xmlns
from your xsl:stylesheet tag. You also need to remove the default namespace declaration:
xmlns="http://api.DomainName.com/2.0"
otherwise all your output will be placed in that namespace, which you most certainly don't want to happen if - as it seems - you want it to be HTML.
There are other changes you need to make - for example, if you want to output an HTML table, set the output method to "html" instead of "text", and make sure your table structure is valid.
Note:
Your XML declares a namespace: xmlns:ign="http://api.DomainName.com/2.0" but this namespace declaration isn't used anywhere. Therefore your stylesheet can ignore it. Provided you are showing us a representative sample of the real XML.
Related
I have an XSLT which i use to transform an XML using Java. The code is working fine when i run it in eclipse and use Apache Tomcat. But when i deploy the ear file to WebSphere, the field is showing as blank. Does anyone have ideas?
The java variables 'reportId' and 'proposalId' are set as i used System.out.println() and could see the value is set.
Java Code
// Use the factory to create a template containing the xsl file
Templates template = factory.newTemplates(new StreamSource(is));
// Use the template to create a transformer
Transformer xformer = template.newTransformer();
xformer.setParameter("reportId", reportId);
xformer.setParameter("proposalId", proposalId);
<xsl:param name="proposalId"/>
<xsl:param name="reportId"/>
I then use the following in the XSLT to read the parameter:
<td align="left"><b>Proposal Ref: </b> <xsl:value-of select="$proposalId"/>
</td>
<td align="left"><b>Report Id: </b> <xsl:value-of select="$reportId"/>
</td>
I found that i had the param inside the template tag. The transformer can not set the value of a template level variable. It can only set it on a global level variable. So my code was like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:param name="proposalId"/>
<xsl:param name="reportId"/>
</xsl:template>
</xsl:stylesheet>
but it should have been:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:param name="proposalId"/>
<xsl:param name="reportId"/>
<xsl:template match="/">
</xsl:template>
</xsl:stylesheet>
So I am trying to pull results from two different XML files using XSLT in order to show a Restaurant Review. I have Restaurant details in allRestaurants.xml and have all of the reviews for these restaurants in allReviews.xml. I have currently stored a tag against each restaurant, and the reviews are each associated with a specific restaurant as well, so carry the same tag. I need to build a page that takes the restaurant with ID 1 and beneath is show the reviews for that restaurant. The reviews are stored with the exactly the same 1 as per below. Please help.
allRestaurants.xml
<restaurants>
<restaurant>
<restaurant_id>1</restaurant_id>
<name>The Jackaroo</name>
<street_address>107-109 Darlinghurst Road</street_address>
<postcode>2011</postcode>
<city>Sydney</city>
<state>NSW</state>
<country>Australia</country>
<email>info#jackaroo.com.au</email>
<telephone>93322244</telephone>
<stars>3</stars>
</restaurant>
<restaurant>
<restaurant_id>2</restaurant_id>
<name>Four Seasons restaurant Sydney</name>
<street_address>199 George Street</street_address>
<postcode>2000</postcode>
<city>Sydney</city>
<state>NSW</state>
<country>Australia</country>
<email>info#sydneyfourseasons.com.au</email>
<telephone>92503100</telephone>
<stars>5</stars>
</restaurant>
</restaurants>
allReviews.xml
<reviews>
<review id="1">
<restaurant_id>1</restaurant_id>
<author_id>1</author_id>
<headline>Clean Bare-Bones Hostel</headline>
<details>
Example text here
</details>
<rating>3</rating>
<date>1388782853</date>
</review>
<review id="2">
<restaurant_id>1</restaurant_id>
<author_id>3</author_id>
<headline>Wouldn't Recommend</headline>
<details>
Example text here
</details>
<rating>2</rating>
<date>1368748800</date>
</review>
<review id="3">
<restaurant_id>2</restaurant_id>
<author_id>2</author_id>
<headline>Overall I Enjoyed</headline>
<details>
Example text here
</details>
<rating>4</rating>
<date>1378788850</date>
</review>
</reviews>
I thought maybe merging them into one XML file like so would do the trick, but even then, I'm not sure where to start:
oneHotel.xml
<?xml-stylesheet type="text/xsl" href="oneHotel.xsl"?>
<list>
<entry name="allHotels.xml" />
<entry name="reviews.xml" />
</list>
This is as far as I got in the XSLT doc, and am drawing a massive blank. I don't even know where to start:
oneHotel.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:copy-of name="restaurant" select="document('allRestaurants.xml')
/restaurants/restaurant[restaurant_id=1]"/>
<xsl:copy-of name="reviews" select="document('allReviews.xml')
/reviews/review[restaurant_id=1]"/>
<xsl:template match="/">
<xsl:choose>
<xsl:when test="document('allRestaurants.xml')
/restaurants/restaurant[restaurant_id=1]"/>
<h2><xsl:value-of select="name"/></h2>
</xsl:choose>
<h2><xsl:value-of select="$restaurant/name"/></h2>
</xsl:template>
</xsl:stylesheet>
Try this as your starting point:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:param name="path-to-reviews" select="'allReviews.xml'"/>
<xsl:key name="review-by-restaurant-id" match="review" use="restaurant_id" />
<xsl:template match="/restaurants">
<html>
<body>
<h1>Restaurant Reviews</h1>
<xsl:apply-templates select="restaurant"/>
</body>
</html>
</xsl:template>
<xsl:template match="restaurant">
<h2>
<xsl:value-of select="name"/>
</h2>
<xsl:variable name="id" select="restaurant_id" />
<!-- switch context to lookup document in order to use key -->
<xsl:for-each select="document($path-to-reviews)">
<xsl:for-each select="key('review-by-restaurant-id', $id)">
<h3>
<xsl:value-of select="headline"/>
</h3>
<p>
<xsl:value-of select="details"/>
</p>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
This assumes that you are instructing your XSLT processor to process the allRestaurants.xml document and passing the path to the allReviews.xml document as a parameter.
You didn't tell us what you want your final result to look like, so I just made up a very basic page.
I have a simple HTML fragment similar to this:
link
I need to transform it to
<abc:href var="123">link</abc:href>
I do it with XSLT, so I had to add the namespace in xsl:stylesheet
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:abc="http://abc.ru">
It works almost fine, unfortunately the XSLT transform keeps on adding a XMLNS to the output, like here:
<abc:href var="123" xmlns:abc="http://abc.ru">link</abc:href>
I don't need the xmlns definition, can I remove it?
Although it really goes against the grain, and I advise strongly against it, if you need to produce this malformed XML, then you can use an instruction like...
<xsl:value-of disable-output-escaping="yes" select="
concat('<abc:href var="',$href,'">',$link,'</abc:href>')
"/>
... where $href and $link are place-markers for the appropriate expression.
Update
In response to the OP's comment, one could use a template like this...
<xsl:template match="a">
<xsl:value-of disable-output-escaping="yes" select="
concat('<abc:href var="',#href,'">',.,'</abc:href>')
"/>
</xsl:template>
This ugly solution should be used only as a last resort. A much better solution would be to use XSLT to produce your WHOLE document, not just an invalid fragment of it. This way you document would be well formed and you could bring to bear the full power and simplicity of XSLT.
It works almost fine, unfortunately the XSLT transform keeps on adding
a XMLNS to the output, like here:
<abc:href var="123" xmlns:abc="http://abc.ru">link</abc:href>
I don't need the xmlns definition, can I remove it?
The wanted removal of the namespace declaration would produce a (namespace-)non-well-formed XML document and for this reason the XSLT processor adds the namespace declaration -- as required by the W3C XSLT specifications.
You can cause these namespace declarations to "disappear" by placing the namespace declaration on a common ancestor (such as the top element of the generated XML document).
Here is a complete example:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<top xmlns:abc="http://abc.ru">
<xsl:apply-templates/>
</top>
</xsl:template>
<xsl:template match="a[#href]">
<xsl:element name="abc:href" namespace="http://abc.ru">
<xsl:attribute name="var">
<xsl:apply-templates/>
</xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the following document:
<html>
link1
link2
link3
link4
</html>
the wanted, correct result is produced:
<top xmlns:abc="http://abc.ru">
<abc:href var="link1"/>
<abc:href var="link2"/>
<abc:href var="link3"/>
<abc:href var="link4"/>
</top>
This is sad, but I really need an invalid xml
XSLT is designed to prevent you producing bad XML. If you want to produce bad XML, don't use XSLT.
Try it with exclude-result-prefixes, like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:abc="http://abc.ru"
exclude-result-prefixes="abc">
<xsl:template match="/">
<xsl:apply-templates select="#* | node()"/>
</xsl:template>
<xsl:template match="a">
<href var="{#href}"><xsl:value-of select="."/></href>
</xsl:template>
</xsl:stylesheet>
If I got this XML(first tag is empty)
<messages>
<message>
<name>Kiro</name>
<acc></acc>
<date>20120506</date>
<template>TMPL_ACC_UP</template>
</message>
<message>
<name>Third</name>
<acc>555</acc>
<date>20120507</date>
<template>TMPL_ACC_UP</template>
</message>
</messages>
Now i take all accpunts with this XPath expression: "//message/acc/text()", and get node list of 1(empty nodes not included). I want to get empty string for the empty tag ['555','']. How I can achieve this? Thanks!
In XPath 2.0 a one-liner like this can be used:
/*/message/acc/string()
However, this cannot be produced as the result of evaluating a single XPath 1.0 expression.
If you must use XPath 1.0, the solution is to select all /*/message/acc elements and then in the PL that is hosting XPath, output the string value of each of the selected elements.
For example, this XSLT 1.0 transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:text>[</xsl:text><xsl:apply-templates/><xsl:text>]</xsl:text>
</xsl:template>
<xsl:template match="acc">
<xsl:if test="preceding::acc">, </xsl:if>
<xsl:text>'</xsl:text><xsl:value-of select="."/><xsl:text>'</xsl:text>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
when applied on the provided XML document:
<messages>
<message>
<name>Kiro</name>
<acc></acc>
<date>20120506</date>
<template>TMPL_ACC_UP</template>
</message>
<message>
<name>Third</name>
<acc>555</acc>
<date>20120507</date>
<template>TMPL_ACC_UP</template>
</message>
</messages>
produces the wanted, correct result:
['', '555']
I would loop over nodes and extract their text as value
<?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>[
<xsl:for-each select="//message/acc">
'<xsl:value-of select="text()"/>',
</xsl:for-each>]
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Result: [ '', '555', ]
I have the following XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="http://www.fakedomain.com/sally.xsl"?>
And the following content in sally.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>
<xsl:for-each select="documentcollection/document">
<p>
<xsl:for-each select="rss/channel/item">
<xsl:value-of select="title"/><br />
<xsl:value-of select="description"/><br />
<xsl:value-of select="link"/><br />
</xsl:for-each>
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
However, the browser displays the XML as though the XSL line is not present. Do you know why the browser is ignoring the XSL stylesheet? Is the style sheet wrong?
Thanks
I have the following XML:
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="http://www.fakedomain.com/sally.xsl"?>
This isn't a well-formed XML document (no top element present), so it isn't much of a surprize the browser doesn't treat it as such.
Solution:
Update your "XML" to a really well-formed XML document, something like:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="file:///c:/temp/delete/xxx.xsl"?>
<t/>
With this stylesheet in c:\temp\delete\xxx.xsl:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
XXX
</xsl:template>
</xsl:stylesheet>
when the XML file is opened with IE, the browser displays the result of the transformation:
XXX
Looks like your not closing off one of your for-each tags?
the xsl:for-each loop of select="rss/channel/item" is not closed.
It could be same origin policy security restrictions. If your XML and XSLT are not hosted in the same location, then the browser may be refusing to fetch the XSLT and apply it to your XML file.