XSLT output missing XML elements

M

mjarends

I'm using JAXP for XSLT - I'm using the examples from
http://www.w3.org/TR/xslt#section-Examples. I'm using the following XML
file:

<?xml version="1.0" encoding="UTF-8"?>
<sales>

<division id="North">
<revenue>10</revenue>
<growth>9</growth>
<bonus>7</bonus>
</division>

<division id="South">
<revenue>4</revenue>
<growth>3</growth>
<bonus>4</bonus>
</division>

<division id="West">
<revenue>6</revenue>
<growth>-1.5</growth>
<bonus>2</bonus>
</division>

</sales>

and the following XSL file:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns="http://www.w3.org/Graphics/SVG/SVG-19990812.dtd">

<xsl:eek:utput method="xml" indent="yes" media-type="image/svg"/>

<xsl:template match="/">

<svg width = "3in" height="3in">
<g style = "stroke: #000000">
<!-- draw the axes -->
<line x1="0" x2="150" y1="150" y2="150"/>
<line x1="0" x2="0" y1="0" y2="150"/>
<text x="0" y="10">Revenue</text>
<text x="150" y="165">Division</text>
<xsl:for-each select="sales/division">
<!-- define some useful variables -->

<!-- the bar's x position -->
<xsl:variable name="pos"
select="(position()*40)-30"/>

<!-- the bar's height -->
<xsl:variable name="height"
select="revenue*10"/>

<!-- the rectangle -->
<rect x="{$pos}" y="{150-$height}"
width="20" height="{$height}"/>

<!-- the text label -->
<text x="{$pos}" y="165">
<xsl:value-of select="@id"/>
</text>

<!-- the bar value -->
<text x="{$pos}" y="{145-$height}">
<xsl:value-of select="revenue"/>
</text>
</xsl:for-each>
</g>
</svg>

</xsl:template>
</xsl:stylesheet>

The result of running the JAXP code is:

<?xml version="1.0" encoding="UTF-8"?>



10
9
7



4
3
4



6
-1.5
2


and is missing the XML elements within the result. The JAXP code that
I'm using is the following:

// Create the tranformation object
TransformerFactory factory = TransformerFactory.newInstance();
StreamSource xslSource = new StreamSource(xslFile);
xslSource.setSystemId(xslFile);
Templates template = factory.newTemplates(xslSource);

// Set the source that the tranformation will be
performed on
Source source = new DOMSource(xmlInput);

// Create a output stream to hold the results
StreamResult result = new StreamResult(output);

// Transform the document
Transformer transformer = template.newTransformer();
transformer.transform(source, result);

Does anyone have any idea as to why the XML elements are not being
output? Thanks in advance.
 
J

Joe Kesselman

xsl:value-of copies only the text content of the node. You probably
wanted xsl:copy-of.
 
M

mjarends

The problem is that the XSL is correct - I took it directly from the
XSLT w3c web site. The result of the transformation should look like
this:

<svg width="3in" height="3in"
xmlns="http://www.w3.org/Graphics/SVG/svg-19990412.dtd">
<g style="stroke: #000000">
<line x1="0" x2="150" y1="150" y2="150"/>
<line x1="0" x2="0" y1="0" y2="150"/>
<text x="0" y="10">Revenue</text>
<text x="150" y="165">Division</text>
<rect x="10" y="50" width="20" height="100"/>
<text x="10" y="165">North</text>
<text x="10" y="45">10</text>
<rect x="50" y="110" width="20" height="40"/>
<text x="50" y="165">South</text>
<text x="50" y="105">4</text>
<rect x="90" y="90" width="20" height="60"/>
<text x="90" y="165">West</text>
<text x="90" y="85">6</text>
</g>
</svg>

However when I use the JAXP code above I get the output that I
specified.
 
J

Joe Kesselman

OK, that's what I get for shooting from the hip rather than stopping to
check it in detail.

Ran it myself, using Xalan via the TrAX API, and it runs fine.

So the next question is, what are you doing with the output? It sounds
as if you're running it through something which is discarding all the
element markup.


Here's a trivial TrAX wrapper; try this and see what it does for you.



import java.io.FileNotFoundException;
public class TrAX {

/**
* @param args
*/
public static void main(String[] args) throws FileNotFoundException
{
String xsl=(args.length>0) ? args[0] : "test.xsl";
String xml=(args.length>1) ? args[1] : "test.xml";
java.io.PrintStream out=(args.length>2)
? new java.io.PrintStream("test.out")
: System.out;

try {
// 1. Instantiate a TransformerFactory.
javax.xml.transform.TransformerFactory tFactory =
javax.xml.transform.TransformerFactory.newInstance();

// 2. Use the TransformerFactory to process the
// stylesheet Source and generate a Transformer.
javax.xml.transform.Transformer transformer =
tFactory.newTransformer(
new javax.xml.transform.stream.StreamSource(xsl)
);

// 3. Use the Transformer to transform an
// XML Source and send the output to a Result.
transformer.transform(
new javax.xml.transform.stream.StreamSource(xml),
new javax.xml.transform.stream.StreamResult(out)
);
} catch (Exception e) {
e.printStackTrace();
}
}

}

}
 
J

Joe Kesselman

Source source = new DOMSource(xmlInput);

You didn't tell us what xmlInput is -- specifically, what the
implementation is and how it's being constructed.

(Are you using the obsolete createElement(), createAttribute(),
setAttribute() calls rather than the namespace-aware createElementNS(),
createAttributeNS(), setAttributeNS()?)
StreamResult result = new StreamResult(output);

You didn't tell us what output is. It may be doing something to the
result before you see it.
 
M

mjarends

Yep - sorry just saw that. The xmlInput is constructed outside of the
method specified above in the following manner:
DocumentBuilder builder = getDocumentBuilder();
// Parser the input source
Document doc = builder.parse(new FileInputStream(EXAMPLE1_XML));

Node example1DocRoot = example1Doc.getDocumentElement();

The example1DocRoot is the xmlInput.

The output is constructed outside of the method in the following
manner:
FileOutputStream fos = new FileOutputStream(EXAMPLE1_RESULT);

In the simple example that I constructed above I don't make any of the
API calls that you specified (createElement(), createAttribute(),
.....). Let me know what else I can provide - I really appreciate the
help. I spent the better part of today trying to find info on why the
transformation isn't working correctly. I also forgot to mention that
the version of Xalan that is being used is 2.6.5.

Additionally I will try the code you posted above.
 
M

mjarends

I just tried your code and it appears that it's a problem with using a
DOMSource vs. a StreamSource. I'm going to have to research why that is.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top