count(parent)

Discussion in 'XML' started by Nutshell, Jan 13, 2005.

  1. Nutshell

    Nutshell Guest

    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!
     
    Nutshell, Jan 13, 2005
    #1
    1. Advertising

  2. Nutshell

    Joris Gillis Guest

    Tempore 20:01:16, die Thursday 13 January 2005 AD, hinc in foro {comp.text.xml} scripsit Nutshell <>:

    > 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,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Vincit omnia simplicitas
    Keep it simple
     
    Joris Gillis, Jan 13, 2005
    #2
    1. Advertising

  3. Nutshell

    Joris Gillis Guest

    > <xsl:if test="name()='par' and count(parent::node()) = 1"> does not work for
    > 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.

    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Vincit omnia simplicitas
    Keep it simple
     
    Joris Gillis, Jan 13, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roy
    Replies:
    2
    Views:
    3,123
  2. mark
    Replies:
    1
    Views:
    576
  3. metal
    Replies:
    8
    Views:
    512
    Bruno Desthuilliers
    Oct 30, 2009
  4. efelnavarro09
    Replies:
    2
    Views:
    948
    efelnavarro09
    Jan 26, 2011
  5. Bill S.
    Replies:
    2
    Views:
    220
    Bill S.
    May 8, 2004
Loading...

Share This Page