Complex o2x:object - 9
test contentList variable size, late binding.
More...
Test for o2x:contentList
use. This o2x:object
is a generalization of two_poly: it can contain any number of Triangle and Rectangle objects.
- data
- This
o2x:object
has no data, only a contentList.
- methods
- Only one final method inline: "Polygons.total_area".
- Returns
- An HTML fragment, a list with the name and the area of every polygon and the total area.
◆ XML_code()
Visibility and contest are not required with only one method.
The output polygon list is build by a loop 'for-each'. The total area is colculated by the recursive function Polygon.totalAreaSum() (named template).
This solution is not optimal (it realizes every polygon 2 times), but so the code is clearer than it would be by putting all jobs in the recursive function.
The 'public' methods xxxxxx.getFigure in Triangle and Rectangle are called (late bindig, group_id = 'CPolygon.getFigure') the to get the polygon's name. The Realization of objects in contentList is now as simple as <xsl:apply-templates select="." />.
- CORE constraint
- Utility methods: A method 'not-final' of a child
o2x:object
can be called by the methods of the container object.
-<o2x:object name="many_poly" xmlns:o2x="http://www.o2xdl.org/3.0/o2xdl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
-<o2x:contentList>
-<!--
any number of a_triangle.o2xSobj and a_rectangle.o2xSobj
-->
<o2x:object URIref="file:///C:/o2xdl-30/CORE/samples/CPolygon/a_triangle.o2xSobj"/>
<o2x:object URIref="file:///C:/o2xdl-30/CORE/samples/CPolygon/a_rectangle.o2xSobj"/>
<o2x:object URIref="file:///C:/o2xdl-30/CORE/samples/CPolygon/a_triangle.o2xSobj"/>
</o2x:contentList>
-<o2x:method key="Polygons.total_area">
-<o2x:inline>
-<xsl:template match="o2x:object[@key='Polygons.total_area']">
-<xsl:for-each select="o2x:contentList/o2x:object">
-<!--
call the public methods xxxxxxxx.getFigure to get the name, late bindig
-->
<xsl:apply-templates select="." mode="CPolygon.getFigure"/>
<xsl:text> </xsl:text>
-<!--
realize a polygon in contentList, get area
-->
<xsl:apply-templates select="."/>
<br/>
</xsl:for-each>
<xsl:text disable-output-escaping="yes"><br /><b>Total = </xsl:text>
-<xsl:call-template name="Polygon.totalAreaSum">
<xsl:with-param name="polygonlist" select="o2x:contentList/*"/>
</xsl:call-template>
<xsl:text disable-output-escaping="yes"></b><br /></xsl:text>
</xsl:template>
-<!--
recursive function (hearly binding) to sum all polygon areas
-->
-<xsl:template name="Polygon.totalAreaSum">
<xsl:param name="polygonlist"/>
-<xsl:if test="count($polygonlist) <= 1">
-<!--
only one item: recursion done, returns this area value as totalAreaSum()
-->
<xsl:apply-templates select="$polygonlist[1]"/>
</xsl:if>
-<xsl:if test="count($polygonlist) > 1">
-<xsl:variable name="progress">
-<xsl:call-template name="Polygon.totalAreaSum">
-<!--
recursive step with shorten list: polygon[2..n]
-->
<xsl:with-param name="polygonlist" select="$polygonlist[position()>1]"/>
</xsl:call-template>
</xsl:variable>
-<!--
var increment: area for polygon[1]
-->
-<xsl:variable name="increment">
<xsl:apply-templates select="$polygonlist[1]"/>
</xsl:variable>
-<!--
returns sum progress + increment
-->
<xsl:value-of select="number($progress) + number($increment)"/>
</xsl:if>
</xsl:template>
</o2x:inline>
</o2x:method>
</o2x:object>