[xsl] Problem with apply-template match="/"

Subject: [xsl] Problem with apply-template match="/"
From: Derrick Shoemake <dshoemake@xxxxxxxxxx>
Date: Thu, 22 Jul 2004 09:59:44 -0500
This isn't a priority, but it's a problem that is bugging me and I would like to know why it's happening.

I'm using xercesImpl- 2.2.1.jar,xercesImpl.jar, xalan-2.4.1.jar
and I have fop.jar  (0.20.5) in the classpath.

When I create a Dom object of the xml and run the following code in java:

// Create the Transformer that takes the XML DOM and converts it
// into XML in memory
TransformerFactory transfact = TransformerFactory.newInstance();
Transformer transformer = transfact.newTransformer(xslSource);

DOMSource dsSource = new DOMSource(xmlDoc.getDocumentElement());

 if (transformer != null) {
      transformer.transform(dsSource, drResult);
      foOutDoc = (Document)drResult.getNode();
 }else{
      System.err.println("No Transformer");
}

The resulting DOM object starts with the information in the apply-template match="root" not "/"

Here is the xml:
<?xml version="1.0" encoding="UTF-8"?><root><piechartamount>77.3</piechartamount></root>


And here is the xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:fo="http://www.w3.org/1999/XSL/Format";
	xmlns:fox="http://xml.apache.org/fop/extensions";
	xmlns:math="java.lang.Math" exclude-result-prefixes="math"
	xmlns:lxslt="http://xml.apache.org/xslt";
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
	xmlns:svg="http://www.w3.org/2000/svg";>

<xsl:param name="color1" select="'#003366'" />
<xsl:param name="color2" select="'#E6E6E6'" />
<xsl:param name="total" select="'0'" />
<xsl:param name="runningTotal" select="'0'" />

<xsl:attribute-set name="table-piechart-style">
	<xsl:attribute name="font-size">11pt</xsl:attribute>
	<xsl:attribute name="font-weight">bold</xsl:attribute>
	<xsl:attribute name="background-color">#ffffff</xsl:attribute>
	<xsl:attribute name="color">#003366</xsl:attribute>
	<xsl:attribute name="text-align">center</xsl:attribute>
	<xsl:attribute name="display-align">center</xsl:attribute>
</xsl:attribute-set>

<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format";>
<fo:layout-master-set>
<fo:simple-page-master margin=".25in .25in .25in .25in" master-name="main" page-height="11in" page-width="8.5in">
<fo:region-body margin="0 0 0 0" region-name="xsl-region-body" />
<fo:region-after extent="2cm" region-name="xsl-region-after" />
</fo:simple-page-master>
</fo:layout-master-set>

<xsl:apply-templates />

</fo:root>
</xsl:template>


<!-- Main Body of Report -->
<xsl:template match="root">

<fo:page-sequence master-reference="main">
<fo:flow flow-name="xsl-region-body">
<fo:block>This is a test</fo:block>
<fo:block xsl:use-attribute-sets="table-piechart-style">
<xsl:variable name="number" select="piechartamount" />
<xsl:variable name="difference" select="100 - $number" />
<xsl:variable name="currentAngle" select="math:toRadians(($number div 100) * 360.0)" />
<xsl:variable name="halfAngle" select="math:toRadians((($number div 2) div $total) * 360.0)" />
<xsl:variable name="rotation" select="270 + (360.0 * ($number div 100))" />
<xsl:variable name="x1" select="math:cos($halfAngle) * 70" />
<xsl:variable name="y1" select="math:sin($halfAngle) * 70" />
<xsl:variable name="cosTheta" select="math:cos(math:toRadians($rotation))" />
<xsl:variable name="sinTheta" select="math:sin(math:toRadians($rotation))" />

<fo:instream-foreign-object>
<svg height="100" viewBox="0 0 200 100" width="200" xmlns="http://www.w3.org/2000/svg"; xmlns:xlink="http://www.w3.org/1999/xlink";>
<g>
<path style="fill:{$color1};stroke:black;stroke-width:1;fillrule:evenodd; stroke-linejoin:bevel;">
<xsl:attribute name="transform">
<xsl:text>translate(100,50)</xsl:text>
<xsl:text>rotate(</xsl:text>
<xsl:value-of select="$rotation" />
<xsl:text>)</xsl:text>
</xsl:attribute>
<xsl:attribute name="d">
<xsl:text>M 50 0 A 50 50 0 </xsl:text>
<xsl:choose>
<xsl:when test="$currentAngle &gt; 3.14">
<xsl:text>1 </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>0 </xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:text>1 </xsl:text>
<xsl:value-of select="math:cos($currentAngle) * 50" />
<xsl:text> </xsl:text>
<xsl:value-of select="math:sin($currentAngle) * 50" />
<xsl:text> L 0 0 Z</xsl:text>
</xsl:attribute>
</path>
</g>
</svg>
</fo:instream-foreign-object>
</fo:block>
</fo:flow>
</fo:page-sequence>


</xsl:template>

</xsl:stylesheet>

Here is the result:

<?xml version="1.0" encoding="UTF-8"?><fo:page-sequence master-reference="main" xmlns:fo="http://www.w3.org/1999/XSL/Format"; xmlns:fox="http://xml.apache.org/fop/extensions"; xmlns:lxslt="http://xml.apache.org/xslt"; xmlns:svg="http://www.w3.org/2000/svg";><fo:flow flow-name="xsl-region-body"><fo:block>This is a test</fo:block><fo:block background-color="#ffffff" color="#003366" display-align="center" font-size="11pt" font-weight="bold" text-align="center"><fo:instream-foreign-object><svg height="100" viewBox="0 0 200 100" width="200" xmlns="http://www.w3.org/2000/svg"; xmlns:xlink="http://www.w3.org/1999/xlink";><g><path d="M 50 0 A 50 50 0 1 1 7.200539127612638 -49.478805930132545 L 0 0 Z" style="fill:#003366;stroke:black;stroke-width:1;fillrule:evenodd; stroke-linejoin:bevel;" transform="translate(100,50)rotate(548.28)"/></g></svg></fo:instream- foreign-object></fo:block></fo:flow></fo:page-sequence>

Sorry for the long email, Like I said it's puzzling me and I would like to understand why it is happening.

Thanks,

Derrick Shoemake

Current Thread