How does Java Transformer use multiple xslt files - java

I have one XSLT file including other two xml files to generate html page, like below
<xsl:include href="xsltcache://UtilityTemplates.xml" />
<xsl:include href="xsltcache://eCertSpecificTemplates.xml" />
I do not know how to add multiple xsl sources (three files in this case) to transformer,
Source xslSource = new StreamSource(xslFile);
Transformer trasformToXml=tFactory.newTransformer(xslSource);

You are using a very specialized form of URI in the xsl:include directives, these look as if they were designed to be handled by custom URI dereferencing logic, perhaps an OASIS catalog.
The bog-standard way to do this would be to use relative URIs in the xsl:include (for example <xsl:include href="UtilityTemplates.xsl"/>, to put all three files in the same directory, and then to make sure that the base URI of the main file is known (which it will be if xslFile is a File object).
If there's some special magic to the "xsltcache" URIs then you need to find out what it is. But if you don't want to change the source, you can set a URIResolver on the TransformerFactory. The URIResolver will be called when an xsl:include declaration is encountered, and it can do anything it likes to convert the supplied URI to a Source object containing the included module.

Related

Parsing a file containing a list of xmls and modifying the same [duplicate]

I was trying to merge with git, but it apparently caused a problem with the XML file making a project unavailable. I know nothing about XML. Here is an excerpt of my file:
<ItemGroup>
<Content Include="MetroFramework.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
An XML documents must have a single root element. To solve your problem:
Remove all root elements except for one, or
Wrap all top-level elements in a single root element.
Until you ensure that your XML document has a single root element, your file will not be well-formed (and will not actually be XML). Also, if your document is intended to follow a schema, make sure the root element and its contents (recursively) is valid according to the schema (XSD, DTD, etc). For more on this, see well-formed vs invalid XML.
As Alex K points out, your XML document looks like it's intended to be a MSBuild Project file.

How to add custom XML storage part to Word doc - preferrably with docx4j

I'm trying to populate a Word content control with XML data using docx4j (version 3.2.1). I'm evaluating this in order to use it for invoice generation. The documents we want to generate are not very complicated so this looks like a good approach to me.
I have created the content control through Word 2010 dev tools. This is how I try to inject the XML into the docx (taken from this example):
WordprocessingMLPackage wordMLPackage = Docx4J.load(new File(input_DOCX));
FileInputStream xmlStream = new FileInputStream(new File(input_XML));
Docx4J.bind(wordMLPackage, xmlStream, Docx4J.FLAG_BIND_INSERT_XML & Docx4J.FLAG_BIND_BIND_XML);
I get the following exception:
org.docx4j.openpackaging.exceptions.Docx4JException: Couldn't find CustomXmlDataStoragePart! exiting..
at org.docx4j.Docx4J.bind(Docx4J.java:300)
at org.docx4j.Docx4J.bind(Docx4J.java:271)
How can I add the CustomXmlDataStoragePart with docx4j, if it doesn't exist yet? Or should/can I do this in Word directly?
Note: I decided to prepare templates in Word directly, because later on these templates must be edited by non-technical users and I don't want to burden them with extra tools, if possible.
You say you "created the content control through Word 2010 dev tools". Unless you mean the content control toolkit, you need to use that or better, either of the OpenDoPE Word addins. Not both.
These tools add a custom xml part into the docx, and allow you to associate it with your content controls via XPath data bindings.
Then, when at runtime you invoke Docx4J.bind, docx4j finds that existing custom xml part, and replaces it with the xml file you provide which contains your runtime data.

Java jax-ws catalog files: difference between PUBLIC and SYSTEM entries

Can somebody explain the difference between SYSTEM and PUBLIC entries in the following TR9401 catalog format (which I use with Java jax-ws tools like xjc, wsimport etc.) The same concepts (SYSTEM and PUBLIC) appear in the other XML catalog formats as well.
In other words, I see the catalog file as a map: namespace (key) to location (value). What does it mean for a key-value pair in that map to be "SYSTEM" as opposed to "PUBLIC" ?
SYSTEM "http://www.w3.org/2001/xml.xsd" "xml.xsd"
SYSTEM "http://www.ivoa.net/xml/STC/STCregion/v1.10" "STCregion-v1.10.xsd"
PUBLIC "-//W3C//DTD XMLSCHEMA 200102//EN" "XMLSchema.dtd"
PUBLIC "http://www.ivoa.net/xml/STC/STCcoords/v1.10" "STCcoords-v1.10-mod.xsd"
I've experimented a bit and have found that I can change certain entries from PUBLIC to SYSTEM and it still works, while the same change on other lines breaks the build.
I'd think it comes from SGML/DTD where every document-type can have a PUBLIC-ID and a SYSTEM-ID. The former is just a sequence of characters (e.g. -//W3C//DTD HTML 4.01//EN), the latter is a filename or an URL.
In XML, xmlns is more or less the public-id while schemaLocation can be regarded as system-id.
An example I've just found on my computer: there is a /usr/share/xml/schema/xml-core/tr9401.dtd file that has a public-id -//GlobalTransCorp//DTD XML Catalogs V1.0-Based Extension V1.0//EN and a system-id http://globaltranscorp.org/oasis/catalog/xml/tr9401.dtd.
Now the catalog.xml file contains these lines:
<public publicId="-//GlobalTransCorp//DTD XML Catalogs V1.0-Based Extension V1.0//EN" uri="tr9401.dtd"/>
<system systemId="http://globaltranscorp.org/oasis/catalog/xml/tr9401.dtd" uri="tr9401.dtd"/>
The first resolves the public-id into an actual local file; the second resolves the system-id into an actual local file.

How to extract data from a lot of URLs?

I have about 3200 URLs to small XML files which have some data in the form of strings(obviously).The XML files are displayed(not downloaded) when I go to the URLs. So I need to extract some data from all those XMLs and save it in a single .txt file or XML file or whatever. How can I automate this process?
*Note: This is what the files look like. I need to copy the 'location' and 'title' from all of them and put them in one single file. Using what methodology can this be achieved?
<?xml version="1.0"?>
-<playlist xmlns="http://xspf.org/ns/0/" version="1">
-<tracklist>
<location>http://radiotool.com/fransn.mp3</location>
<title>France, Paris radio 104.5</title>
</tracklist>
</playlist>
*edit: Fixed XML.
It's easy enough with XQuery or XSLT, though the details will depend on how the URLs are held. If they're in a Java List, then (with Saxon at least) you can supply this list as a parameter to the following query:
declare variable urls as xs:string* external;
<data>{
for $u in $urls return doc($u)//*:tracklist
}</data>
The Java code would be something like:
Processor proc = new Processor();
XQueryCompiler c = proc.newXQueryCompiler();
XQueryEvaluator q = c.compile($query).load();
List<XdmItem> urls = new ArrayList();
for (url : inputUrls) {
urls.append(new XdmAtomicValue(url);
}
q.setExternalVariable(new QName("urls"), new XdmValue(urls));
q.setDestination(...)
run();
Have a look at the JSoup library here: http://jsoup.org/
It has facilities for pulling and fixing the contents of a URL, it is intended for HTML though, so I'm not sure it will be good for XML, but it is worth a look.

Dynamic XML creation in Java

I am trying to dynamically y create an XML file in Java to display a timetable. I have created a DTD for my XML file and I have an XSL file I would like to use to transform the XML. I don't know exactly how to continue.
What I've tried so far is onClick of some button a Servlet is called which generates the string of the content of the XML file (inserting the dynamic parts of the XML into the String. I now have a String containing the content of the XML file. I would now like to transform the XML file using an XSL file i have on my server and display the result in the page which has called the Servlet (doing this via AJAX).
I'm not sure if I'm in the direction, perhaps I shouldn't even create the XML code in String form from the beginning. So my question is, how do I continue from here? how do I transform the XML string, using the XSL file, and send it as a response to the AJAX call so I can plant the generated code into the page? Or if this is not the way to do it, how do I create a dynamic XML file in a different way producing the same result?
You can use JAXP for this. It's part of standard Java SE API.
StringReader xmlInput = new StringReader(xmlStringWhichYouHaveCreated);
InputStream xslInput = getServletContext().getResourceAsStream("file.xsl"); // Or wherever it is. As long as you've it as an InputStream, it's fine.
Source xmlSource = new StreamSource(xmlInput);
Source xslSource = new StreamSource(xslInput);
Result xmlResult = new StreamResult(response.getOutputStream()); // XML result will be written to HTTP response.
Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource);
transformer.transform(xmlSource, xmlResult);
Depending on how complicated and large your XML is going to be I would suggest two options. For small, simple structures Java's DOM implementation (Document) will suffice.
If your XML is more elaborate I would look into JAXB. The benefit there is that there are tools that automatically create Java classes from an XML schema (XSD). So you'd have to transform your DTD into an XSD first, but that shouldn't be a problem. You end up with plain data transfer objects (plain objects with getters/setters for the values of the corresponding XML elements) and parsing/encoding plus setting namespaces correctly is done for you. It's quite convenient but can also be a bit of an overkill for simple XML structures.
In both cases, you will end up with a Document instance that you can finally transform using JAXP.
Apache XMLBeans are a nice solution to serializing to and from XML. Here's what you need to do:
Download XMLBeans from http://www.apache.org/dyn/closer.cgi/xmlbeans/binaries
Use the XMLBeans inst2xsd executable (in the bin dir0 to convert your DTD to an XSD
Use the XMLBeans ANT task to convert the XSD into classes which you can use in your app
Here's an example ANT script to use XMLBeans to create the classes:
<project name="my_project" basedir="..">
<property name="my_project.project.path" value="${basedir}"/>
<property name="xbean.dir" value="C:/lib/xmlbeans-2.2.0/lib" />
<path id="classpath">
<fileset dir="${xbean.dir}" includes="**/*.jar" />
</path>
<taskdef name="xmlbean" classname="org.apache.xmlbeans.impl.tool.XMLBean" classpathref="classpath" />
<xmlbean schema="${testing_project.project.path}/my.xsd" srcgendir="${my_project.project.path}/src-tms-template-filter-fields" classgendir="${my_project.project.path}/bin">
<classpath><path refid="classpath" /></classpath>
</xmlbean>
You'll now have nice Java classes which you can use for clean code to create the XML from the data stored in your DB. Use BalusC's answer for the XSLT.

Categories