How to find out the Parent or the Ancestor Node.

Discussion in 'XML' started by Eshrath, Oct 26, 2004.

  1. Eshrath

    Eshrath Guest

    Hi,

    I have an xsl where a particular child node (<sub1> and <sub2> )can
    occur in two type of parent nodes (<A> and <B>).

    like

    <A>
    <sub1>11</sub1>
    <sub2>22</sub2>
    </A>

    <B>
    <sub1>33</sub1>
    <sub2>44</sub2>
    </B>

    I am planning to write a common template for this child elements like

    <xsl:template match="sub1">
    ...........
    ...........
    ...........
    <xsl:template />

    Inside this template I want to do different processing depending upon
    the parent like if the parent is <A> or if the parent is <B> I need to
    do different processing. Like if the parent is <A> then I need to print
    "AAAAA" in the template and if the parent is B then I want to print
    "BBBBB". Also depeneding upon the ancestor element (not the value) I
    want to do different processing. Can you please let me know the statment
    or the Xpath expression that is used to find out this?

    Thanks in Advance,

    -Eshrath.
    Eshrath, Oct 26, 2004
    #1
    1. Advertising

  2. In article <>,
    Eshrath <> wrote:

    >Inside this template I want to do different processing depending upon
    >the parent like if the parent is <A> or if the parent is <B> I need to
    >do different processing.


    <xsl:if test="parent::A">... A stuff ...</xsl:if>
    <xsl:if test="parent::B">... B stuff ...</xsl:if>

    -- Richard
    Richard Tobin, Oct 26, 2004
    #2
    1. Advertising

  3. Hi ,

    It is working out fine but I have a doubt in this case.

    The input XML is

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="parent.xsl"?>
    <AAAAA>
    <A>
    <sub1>SUBA1 </sub1>
    <sub2>SUBA2 </sub2>
    </A>
    <B>
    <sub1>SUBB1 </sub1>
    <sub2>SUBB2 </sub2>
    <sub3>SUBB3 </sub3>
    </B>
    </AAAAA>

    The Stylesheet for this is

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="sub3">
    <xsl:choose>
    <xsl:when test="parent::A">
    <xsl:text > Hi I am a child of A ******* </xsl:text>
    </xsl:when>
    <xsl:when test="parent::B">
    <xsl:text >Hi I am a child of B ******* </xsl:text>
    </xsl:when>
    </xsl:choose>
    </xsl:template>
    </xsl:stylesheet>

    The output obtained is

    SUBA1 SUBA2 SUBB1 SUBB2 Hi I am a child of B *******

    I really don't understand the output. The template I have written is
    only for match="sub3" and I was expecting a output

    Hi I am child of B *******

    I donno where did "SUBA1 SUBA2 SUBB1 SUBB2" came from. How does the flow
    of the execution work? Can you please explain this.

    Thanks
    -Eshrath.

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Eshrath Ali Khan, Oct 27, 2004
    #3
  4. On 27 Oct 2004 00:17:36 GMT, Eshrath Ali Khan <> wrote:
    >Hi ,
    >
    >It is working out fine but I have a doubt in this case.
    >
    >The input XML is
    >
    ><?xml version="1.0"?>
    ><?xml-stylesheet type="text/xsl" href="parent.xsl"?>
    ><AAAAA>
    ><A>
    > <sub1>SUBA1 </sub1>
    > <sub2>SUBA2 </sub2>
    ></A>
    ><B>
    > <sub1>SUBB1 </sub1>
    > <sub2>SUBB2 </sub2>
    > <sub3>SUBB3 </sub3>
    ></B>
    ></AAAAA>
    >
    >The Stylesheet for this is
    >
    ><?xml version="1.0" encoding="UTF-8"?>
    ><xsl:stylesheet version="1.0"
    >xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    ><xsl:template match="sub3">
    ><xsl:choose>
    > <xsl:when test="parent::A">
    > <xsl:text > Hi I am a child of A ******* </xsl:text>
    > </xsl:when>
    > <xsl:when test="parent::B">
    > <xsl:text >Hi I am a child of B ******* </xsl:text>
    > </xsl:when>
    ></xsl:choose>
    ></xsl:template>
    ></xsl:stylesheet>
    >
    >The output obtained is
    >
    >SUBA1 SUBA2 SUBB1 SUBB2 Hi I am a child of B *******
    >
    >I really don't understand the output. The template I have written is
    >only for match="sub3" and I was expecting a output
    >
    >Hi I am child of B *******
    >
    >I donno where did "SUBA1 SUBA2 SUBB1 SUBB2" came from. How does the flow
    >of the execution work? Can you please explain this.


    You only wrote a template to match "sub3", so the default templates are
    used for all other nodes in your XML document. In particular, the
    default template for a text node is to output the value of that node.
    To keep this from happening, put the following empty template somewhere
    in your stylesheet:

    <xsl:template match="text()"/>

    This will suppress the default output of text nodes.
    --
    Morris M. Keesan --
    Morris M. Keesan, Oct 27, 2004
    #4
    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. Replies:
    0
    Views:
    1,414
  2. Tjerk Wolterink
    Replies:
    2
    Views:
    1,406
    Dimitre Novatchev
    Aug 24, 2006
  3. Replies:
    2
    Views:
    815
    Martin Honnen
    Mar 17, 2008
  4. John Bankhead

    Null parent node on custom tree node after populate on demand

    John Bankhead, Dec 4, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    271
    John Bankhead
    Dec 4, 2006
  5. harryajh
    Replies:
    4
    Views:
    243
    Thomas 'PointedEars' Lahn
    Feb 1, 2008
Loading...

Share This Page