#22 Iterating over empty nodes -> leak

open
nobody
None
5
2013-01-15
2006-01-02
Mikael
No

Given:

***
XML
***

<?xml version="1.0" encoding="UTF-8"?>
<node>
<foo>
<blah>abc</blah>
<bar>123</bar>
<bar>456</bar>
</foo>
<foo>
<blah>abc</blah>
<bar>123</bar>
<bar>456</bar>
</foo>
<foo>
<blah>abc</blah>
</foo>
<foo>
<blah>abc</blah>
</foo>
</node>

***
XSL
***

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" indent="yes"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

<xsl:template match="/">
<xsl:apply-templates select="node"/>
</xsl:template>

<xsl:template match="node">
<xsl:for-each select="foo">
<xsl:value-of select="blah"/>
<xsl:for-each select="bar">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

...XMLPartner will leak 56 bytes of memory (as reported
by MemCheck). No solution, but a 'fix' involves
evaluating whether any nodes exist to be iterated over:

***
XSL
***

<xsl:template match="/">
<xsl:apply-templates select="node"/>
</xsl:template>

<xsl:template match="node">
<xsl:for-each select="foo">
<xsl:value-of select="blah"/>
<xsl:if test="count([.//bar]) &gt; 0">
<xsl:for-each select="bar">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

...node the addition of the 'xsl:if' statement - this
removes the leak

Discussion


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks