4

I know that I can create xml files programmatically by using DOM api in java like the following:

DocumentBuilderFactory documentBuilderFactory = 
    DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

Document document = documentBuilder.newDocument();
Element rootElement = document.createElement("map");
document.appendChild(rootElement);

Element em = document.createElement("string");
em.setAttribute("name", "FirstName");
....

But are there any API 's to construct an xslt tree? (an api like Dom for example)

I need somehing like this:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">
<xsl:template match="root">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="my-page">
      <fo:region-body margin="1in"/>
    </fo:simple-page-master>
  </fo:layout-master-set>

  <fo:page-sequence master-reference="my-page">
    <fo:flow flow-name="xsl-region-body">
      <fo:block>
        <fo:external-graphic width="100pt" height="100pt" content-width="50pt" content-height="50pt" src="images/shopping-cart_100.jpg"/>
      </fo:block>
      <fo:block>Good Morning, <xsl:value-of select="name" />!</fo:block>
      <fo:block>
        <fo:table>
             <fo:table-body>
                <fo:table-row>
                    <fo:table-cell border="solid 1px black" text-align="center" font-weight="bold">
                        <fo:block>

and:

              <xsl:for-each select="./friend">
                <fo:table-row>
                <fo:table-cell border="solid 1px black" text-align="center">
                    <fo:block>
                        <xsl:value-of select="position()" />
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell border="solid 1px black" text-align="center">
                    <fo:block>
                        <xsl:value-of select="name" />
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell border="solid 1px black" text-align="center">

Thanks in advance.

5
  • 2
    XSLT is valid XML, so yes you can. Commented May 8, 2012 at 15:26
  • @climbage Please take your time to stress on your comment with an answer. Commented May 8, 2012 at 15:57
  • 2
    if you can construct xml, then you can construct xslt.. xslt is XML... Commented May 8, 2012 at 17:02
  • 1
    hi, have you ever found the API for that. please share Commented Jun 24, 2016 at 7:21
  • I personally used the accepted answer, but you can choose any of the answers below that would suit your case. Commented Jun 27, 2016 at 7:35

3 Answers 3

4

Since XSLT it's XML too, you can simply use the same strategy:

...
Document document = documentBuilder.newDocument();

Element rootElement = document.createElement("xsl:stylesheet");
// adding attributes like namespaces etc...

document.appendChild(rootElement); 
Element em = document.createElement("xsl:template");
em.setAttribute("match", "/");

and so on...

But it's not very elegant. You should use a library or a framework instead, you should easily find one googling around.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, it's quite complex :) You can either create the XSLT like I've shown: 'Element rootElement = document.createElement("xsl:stylesheet"); rootElement.setAttribute("version", "1.1");' etc. OR you could try using a library which can help you in doing so... maybe the Apache Cocoon stuff mentioned by Simone Gianni will do the work, but I cannot say as I've never used it before...
2

You can create an XSLT the same way you create an XML file, since XSLTs are XML files.

However, if you have to deal with XML/XSLT transformations a lot, Apache Cocoon 3 has a very lightweight XML/XSLT pipeline system to use as a library instead of dealing with all XML dom stuff and XSLT transformations manually.

1 Comment

Cacoon does nothing in the terms pf what my question asks!
1

DOM is a pretty cumbersome way of creating XML.

There's a far better way - use XSLT.

The more complex the XML, the bigger the win from using XSLT rather than DOM to create it.

There's no reason why you can't use XSLT to create XSLT (there's even a special declaration xsl:namespace-alias to make it a little bit easier - searching for xsl:namespace-alias will show up examples of its use.

5 Comments

Please can you specify of how this could be helpful to me??? I need an API to create xslt file through java dynamically!!!
@MikeMyers: See an example here: dnovatchev.wordpress.com/2006/10/21/…
@DimitreNovatchev there is nothing found on this page - must be old!
@MikeMyers: I access it OK -- must be a problem with your internet connectivity.
it opens the page but it says apologies but no results were found for the requested archive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.