Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

java - Create xslt files programmatically

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...