469,356 Members | 1,893 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

How to find out the Parent or the Ancestor Node.

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.
Jul 20 '05 #1
3 9658
In article <4f**************************@posting.google.com >,
Eshrath <es*****@gmail.com> 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
Jul 20 '05 #2
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!
Jul 20 '05 #3
On 27 Oct 2004 00:17:36 GMT, Eshrath Ali Khan <es*****@gmail.com> 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 -- ke****@alum.bu.edu

Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Gert Brinkmann | last post: by
2 posts views Thread by dannielum | last post: by
4 posts views Thread by darrel | last post: by
2 posts views Thread by Raed Sawalha | last post: by
3 posts views Thread by Valvalis | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.