2Big2BeSmall
2Big2BeSmall

Reputation: 1378

Convert xml to csv with xsl java

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>&#10;</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)

Upvotes: 1

Views: 516

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117053

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.

Upvotes: 1

Related Questions