Unable to transform XML with XSL in java code
Hi,
Could somebody please tell me what's wrong with my code, why it isn't transform the XML with XSL to the output that I want. If I use the command line to transform the XML, it output perfectly:
java org.apache.xalan.xslt.Process -in marc.xml -xsl MARC21slim2MODS.xsl -out out.xml
Here is the code of my program to transform the XML with XSL, I am using xalan-j_2_2-bin:
import java.io.*;
import java.net.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import java.math.BigInteger;
String xslDoc = "MODS.xsl";
String xmlResult = "out.xml";
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
Document xmlDoc = docBuilder.newDocument();
Element root = xmlDoc.createElement("collection");
root.setAttribute("xmlns", "http://www.loc.gov/MARC21/slim");
xmlDoc.appendChild(root);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xslDoc));
FileWriter fw = new FileWriter(new File(xmlResult));
StreamResult output = new StreamResult(fw);
transformer.transform(new DOMSource(xmlDoc), output);
fw.flush();
fw.close();
========================
marc.xml -- source XML file
========================
<?xml version="1.0" encoding="UTF-8"?>
<collection xmlns="http://www.loc.gov/MARC21/slim"><record><leader>01488cam 2200337 a 4500</leader><controlfield tag="001">2502929</controlfield><controlfield tag="005">19930521155141.9</controlfield><controlfield tag="008">920219s1993 caua j 000 0 eng </controlfield><datafield ind1=" " ind2=" " tag="035"><subfield code="9">(DLC) 92005291</subfield></datafield><datafield ind1=" " ind2=" " tag="906"><subfield code="a">7</subfield><subfield code="b">cbc</subfield><subfield code="c">orignew</subfield><subfield code="d">1</subfield><subfield code="e">ocip</subfield><subfield code="f">19</subfield><subfield code="g">y-gencatlg</subfield></datafield>
</record></collection>
========================
out.xml -- result using command line
========================
<?xml version="1.0" encoding="UTF-8"?>
<collection xmlns="http://www.loc.gov/mods/" xmlns:xlink="http://www.w3.org/TR/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/mods/ http://www.loc.gov/standards/marcxml/schema/mods.xsd">
<mods>
<titleInfo>
<title>Arithmetic</title>
</titleInfo>
<name type="personal">
<namePart>Sandburg, Carl</namePart>
<namePart type="date">1878-1967</namePart>
<role>creator</role>
</name>
</mods>
</collection>
========================
out.xml -- result using my java program
========================
<?xml version="1.0" encoding="UTF-8"?>
<collection xmlns="http://www.loc.gov/mods/" xmlns:xlink="http://www.w3.org/TR/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/mods/ http://www.loc.gov/standards/marcxml/schema/mods.xsd">01488cam 2200337 a 4500250292919930521155141.9920219s1993 caua j 000 0 eng (DLC) 920052917cbcorignew1ocip19y-gencatlgpc16 to br00 02-19-92; br02 to SCD 02-21-92; fd11 02-24-92 (PS3537.A618 A...); fa00 02-26-92; fa05 03-02-92; fm31 03-06-92; CIP ver. pv08 04-16-93; pv01 to CLT 04-20-93; lb10 05-21-93
</collection>
I am using the same XSL file. My Java program use the same XSL file I used in the command line.
It is possible that my Java code is using a different parser, but I developed a seperate program to parse the XML using the same parser that my Java code is using. It output the result I expected. Here is the code for the program:
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
public class Convertor {
public static void main(String[] args) throws Exception {
String xslDoc = "MARC21slim2MODS.xsl";
String xmlResult = "out.xml";
String xmlDoc = marc.xml";
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xslDoc));
StreamSource xmlSource = new StreamSource(xmlDoc);
FileWriter fw = new FileWriter(new File(xmlResult));
StreamResult output = new StreamResult(fw);
transformer.transform(xmlSource, output);
}
Similar Messages
-
How can I use JavaScript extention functions with Xalan for transforming XML with XSL
While transforming standart XML and XSL files to HTML with this servlet:
package mypackage1;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.URL;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import org.mozilla.javascript;
public class Servlet2 extends HttpServlet
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
public void init(ServletConfig config) throws ServletException
super.init(config);
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
try
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
TransformerFactory tFactory = TransformerFactory.newInstance();
Source xmlSource = new StreamSource(new FileReader("c:/aaa.xml"));
Source xslSource = new StreamSource(new FileReader("c:/bbb.xsl"));
Transformer transformer = tFactory.newTransformer(xslSource);
transformer.transform (xmlSource, new StreamResult(out));
catch (Exception e)
e.printStackTrace();
everything is going ok,
but when try to use javascript function in XSL file, for example like in this:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:lxslt="http://xml.apache.org/xslt" xmlns:my-ext="ext1"
extension-element-prefixes="my-ext">
<lxslt:component prefix="my-ext"
functions="getdate">
<lxslt:script lang="javascript">
function getdate() {
var d = new Date();
return d.toUTCString();
</lxslt:script>
</lxslt:component>
<xsl:template match="/">
<p><xsl:copy-of select="my-ext:getdate()"/></p>
</xsl:template>
</xsl:stylesheet>
recieve error-message:
XSL-1000: (Fatal Error) Error while parsing XSL file (Extension function namespace should start with 'http://www.oracle.com/XSL/Transform/java/'.).
What kind of namespace I should specify?Hello, Paul.
I'm sure you may not use JavaScript as a language for creating XSLT extention functions with Oracle XDK Parser. This is since parser might have JavaScript interpreter to work with JavaScript, but it has not.
If you need to build any XSLT extention functions you must build them as Java class' static methods.
After that, you define the usage of the class by mean of namespace declaration as:
xmlns:your-ns="http://www.oracle.com/XSL/Transform/java/yourpackage.Yourclass"
(Prefix "http://www.oracle.com/XSL/Transform/java/" may differs if you use non-Oracle XML parser)
and use class' static method in XSLT:
<xsl:value-of select="your-ns.staticMethodName(paramsIfAny)"/>
In your case you may wish to use standard Date class:
xmlns:date="http://www.oracle.com/XSL/Transform/java/java.util.Date"
<xsl:value-of select="date:toString(date:new)"/> -
Unable to transform xml with xmlns:xsi and xmlns
Hi
My xml is like below. If i dont have and xmlns and xmlns:xsi it works fine. but if this is added. it outputs wrong xml
<Request xmlns="http://www.rx.com/xone/1_0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.rx.com/xone/1_0 C:\rx.xsd" PharComments="String" CustomizableInfo="String">
<Phar ZipCode="1234"/>
<VoicePhone>123-658-9032</VoicePhone>
<FaxPhone>123-658-5678</FaxPhone>
</Phar>
</Request>
How to resolve this.
thankssorry, if this was not worded correctly
when i mean if this was not added- this refers to the xmlns and xmlns:xsi i the xml file.
wrong xml is not the desired xml output.
Anyways i resolved it by adding the xmlns in the xsl file and preceeding it with the element names to get the correct value This work.
Can anyone tell how to add the CDATA to the textnodes of the output xml during transformation.
Thanks -
How to insert into table from a xml with XDE for java?
want to insert into the oracle tables from the xml with XDE for java, some sample better. thank you.
XML Document may be stored in a SQL database with XML SQL Utility.
http://download-west.oracle.com/docs/cd/B13789_01/appdev.101/b10794/adx08xsu.htm#i1008168
XML Document may be stored in a SQL database with Oracle XML DB.
http://download-west.oracle.com/docs/cd/B13789_01/appdev.101/b10790/xdb03usg.htm#CEGFECFH -
How to read the contents of XML file from my java code
All,
I created an rtf report for one of my EBS reports. Now I want to email this report to several people. Using Tim's blog I implemented the email part. I am sending emails to myself based on the USERID logic.
However I want to email to different people other then me. My email addresses are in the XML file.
From the java program which sends the email, how can I read the fields from XML file. If any one has done this, Please point me to the right examples.
Please let me know if there are any exmaples/BLOG's which explain how to do this(basically read the contents of XML file in the Java program).
Thank You,
PadmaIke,
Do you have a sample. I am searched so much in this forum for samples. I looked on SAX Parser. I did not find any samples.
Please help me.
Thank you for your posting.
Padma. -
Fill a pdf form with data using Java code
Hi,
Modern formats of Acrobat ".pdf" files allows to define form fields.
I have a datasource, let's say in a database.
I want to fill my ".pdf" with my database-stored data automatically, and want to write Java code for that.
1. What is the best way to achieve this ?
2. Is it necessary to install Acrobat Distiller server and if so what web services should I invoke ? Where is the doc ?
3. Are there efficient Java libraries to do so ?
Thank you for helping... As ever, this is kind of an emergency case
Have a good day.
PhilTake a look at the FDF Toolkit for Java:
http://www.adobe.com/devnet/acrobat/fdftoolkit.html
That will allow you to, through Java, construct an FDF that contains all your form data. You then add a reference to your "template" PDF file in the FDF as well and send the FDF to the client. Acrobat will open the FDF, see the reference to the PDF, open the PDF and then merge any data contained in the FDF with the PDF. -
Registering db's with ODBC through Java code?
Hey, I have a project which uses an msacces database, I first have to register the database with odbc in windows, but I wonder if you can't do this with Java code instead of letting to user do some things he'd probably screw up while deploying the app.
nope, no api. You will need to JNI to native C++ code to effect things like ODBC setup, registry, and paths, etc. There are third party apps, $$, that you can find on google.com
-
How to compare two huge xml files(50MB+) using Java Code
I want to compare two huge xml files using java code and need to find the difference of those xml files
is there any API for thatYou should find third party API
-
Hello,
I have an OutOfMemoryError when applying an xsl transformation to generate html
code in a web page. I'm running Weblogic 6.1 sp2
I think the problem comes from the objectin which th html is stored during the
transformation. The problem only occurs from a minimal xml size. I could fix
it by increasing JVM allocated size but I would like to find a more flexible solution.
If I know an other way to solve this problem, I really would appreciate !
Thanl you,
Laurent.
[AgentsGeres.jsp]Hello,
You might try asking your question in the JSP newsgroup:
http://newsgroups.bea.com/cgi-bin/dnewsweb?cmd=xover&group=weblogic.developer.interest.jsp
There just may not be a more flexible solution :-)
This doc may be of some value, see chapter 2:
http://edocs.bea.com/wls/docs61/pdf/perform.pdf
HTH,
Bruce
Laurent Gosuin wrote:
>
Hello,
I have an OutOfMemoryError when applying an xsl transformation to generate html
code in a web page. I'm running Weblogic 6.1 sp2
I think the problem comes from the objectin which th html is stored during the
transformation. The problem only occurs from a minimal xml size. I could fix
it by increasing JVM allocated size but I would like to find a more flexible solution.
If I know an other way to solve this problem, I really would appreciate !
Thanl you,
Laurent.
Name: AgentsGeres.jsp
AgentsGeres.jsp Type: Hypertext Markup Language (text/html)
Encoding: base64 -
Showing xml with xsl in a java webapplication
Dear members,
what are the possible ways to show an XML file in a web application written in java?
The xml file has to be transformed by a corresponding xsl file.
Regards,
Katjabhupesh,
Try my solution described here:
/people/valery.silaev/blog/2005/11/23/display-formatted-text-using-webdynpro-for-java
final String xml = <yorXmlStringFromRfc>;
final IWDCachedWebResource resource = WDWebResource.getWebResource
xml.getBytes("UTF-8"),
WDWebResourceType.XML
resource.setResourceName("XML_inline.xml");
resource.setAttachement( false );
resource.setReadOnce( false );
wdComponentAPI.getWindowManager.createExternalWindow
resource.getAbsoluteURL(), "XML Window Title", false
).open();
VS
Message was edited by: Valery Silaev -
Transform XML using XSL, output XML
Hi all,
I have an xml document and I want to transform it using an xsl file. As a result I want an xml document, it's possible? Anyone could help me?That 's exactly what XSLT does. Learn more about it here: http://www.ibiblio.org/xml/books/bible2/chapters/ch17.html
Good luck. -
Unable to establish connection with SQLServer from Java Application
Dear All,
I am facing the issue of establishing connection with SQL Server from Java project developed in NWDS. I am writing a jaas plug-in, as a part of that I am writing a login module to authenticate a web application through jaas, and I got need of fetching some data from SQL Server which is located in remote host. I am getting error in the line of code
<b>Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")</b>
I also added the 3 jar files (msbase.jar,mssqlserver.jar,and msutil.jar) to my login module project and copied the same files at C:\usr\sap\J2E\JC01\j2ee\cluster\server0\bin\ext\MSSQL.
My MI Landscape is j2ee stack + abap stack +WAS 6.40 with MAXDb as a database.
Could anyone please let me know the solution if you are aware of it.
Thanks and Regards,
KishoreI solved it by writing the following code
Connection con;
Statement stmt;
DataSource ds;
Context ctx;
ctx = new InitialContext();
if(ctx == null) throw new Exception("Boom - No Context");
ds=(DataSource)ctx.lookup("jdbc/SQLDB");
con = ds.getConnection();
stmt = con.createStatement();
String query = "SELECT * FROM SubjectBIR where Subject_ID='" + user + "'";
ResultSet result = stmt.executeQuery(query);
stmt.close();
con.close(); -
Java & xml: using .getXMLString() in JAVA code
I was trying to experiment in what XML output I would get from a query on a database of mine so in a JAVA program outside the
database I tried:
OracleXMLQuery oxq = new OracleXMLQuery(conn, query);
oxq.setRowsetTag(row set");
oxq.setRowTag("row");
return oxq.getXMLString();
If oxq.getXMLString() is commented out it runs fine (but does nothing visable) but when is there I get a comolation error:
oxq.getXMLString(); gets error on compiling: Error(776,21): cannot access class org.w3c.dom.Node; file org\w3c\dom\Node.class not
found
Why is this happening? Sorry if this a silly question
Many thanks for your help
Hannah BirchWhat do your imports and class/lib path look like?
-
How can i get rulefiles name in appllication with using custom java code.
Hi.
I want to get rulefiles names from analytic service with using java api.
How can i get rulefiles name. Which api can i use for this issue?Hi,
Use IEssCube.getOlapFileObjects() api to retrieve rule files.
Insert this code snipid in your code, it will list all the rule files.
IEssCube cube = olapSvr.getApplication("Sample").getCube("Basic");
IEssIterator itr = cube.getOlapFileObjects(IEssOlapFileObject.TYPE_RULES);
IEssBaseObject[] rfs = itr.getAll();
System.out.println("Rule file count: "+rfs.length);
for (int i = 0; i < rfs.length; i++) {
System.out.println("RuleFile"+i+": "+((EssOlapFileObject)rfs).getName());
you can also list data files and other file objects by changing IEssOlapFileObject.TYPE in getOlapFileObjects()
Regards
Rajeev Singh -
How can i create a project with my exisiting java code
Hi All,
I have a source code of one project and i want to execute this in jdeveloper 10.1.3.1.0 version.
can u please suggest me how can i execute that project
its using struts and ejb's
i need clear steps of this.
Thanks in Advance.
Regards,
588872Under file->new->Projects you'll find two options that are relevant to you:
One is called project from WAR file - this will allow you to point to a project that has been packaged as a WAR and will create a JDeveloper project for it.
The other option is called "Project from existing source" this will allow you to point to your existing code directory and will create the JDeveloper project for this.
After you created the project, you might want to do a bit of tweaking under the project properties->content and sub categories for Web content, resources etc...
And you might also need to add any missing JAR files you are depending on to the libraries node of your project properties.
Maybe you are looking for
-
Webcliiping integrate with Oracle Portal
Hello, Is it possible to integrate a webclipping which is created using RoboSuite with Oracle Portal? The webclipping created using RoboSuite can be saved as JSP. We are finding some limitations with Oracle web clipiing studio. Hence we are planning
-
Do Oracle Designer and Worklfow Builder "talk"?
Is anyone aware of any form of integration between Oracle Designer and Workflow Builder? I am specifically curious about storing workflow processes in Designer Repository and whether or not Designer process flows can be used as a starting point for w
-
Run time error "Time_out'
Hi All, we got run time error saying 'Time_out' at Modify statement. The statement is Modify zvbuk. zvbuk is a data base table. this table consists of arround 4 crore records. till now we dint get the dump but from last two days we are getting dump p
-
i lost all my data, dont know how this happen, screenis not working, i am using iphone5 is there any way apple can recover my data i have 400+ photos that i have cliked in last 2-3 days
-
Outlook 2013 not compatible with Exchange 2003
Hello Some of my customers bought SBS 2003 R2 just before SBS 2008 was released so they have systems which are only 4+ years old. SBS 2003 came with Exchange 2003 pre-installed. Outlook 2013 does not support Exchange 2003. So they cannot upgrade thei