count(parent)

N

Nutshell

Hi,

I would like to add an attribute to the child node if it is the only child
node. The problem is I am not getting the correct count of child nodes with
this template:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:eek:utput method="xml" indent="yes" encoding="UTF-8" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:if test="name()='par' and count(parent::node()) = 1">
<xsl:attribute name="repeatCount">
<xsl:text>1</xsl:text>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Here, the 'par' node is the current child node and I would like to check
something like: if parent node of the current 'par' node has only one 'par'
node then add 'repeatCount' attribute to the only 'par' child node. But the
condition
<xsl:if test="name()='par' and count(parent::node()) = 1"> does not work for
me. What am I doing wrong in this condition?

Btw, I am using Xalan.

Thx, in advance!
 
J

Joris Gillis

Tempore 20:01:16 said:
Here, the 'par' node is the current child node and I would like to check
something like: if parent node of the current 'par' node has only one 'par'
node then add 'repeatCount' attribute to the only 'par' child node.

Hi,

The test expression could be something like this:
<xsl:if test="self::par and count(../par) = 1">


regards,
 
J

Joris Gillis

me. What am I doing wrong in this condition?

'parent::node()' (abbreviated '..') selects all parent nodes of the context node. Since all nodes (all descendants of the document root) have exactly 1 parent, the test 'count(parent::node()) = 1' will always return true.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top