473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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 9789
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Gert Brinkmann | last post by:
Hello, as I understand the specs http://www.w3.org/TR/CSS21/visuren.html#x19 an absolute positioned block element is positioned relative to its parent element. This is told in the german...
9
by: Bernd.Moos | last post by:
Given the following XML document: <text> <p> <w>Ronaldo</w> <w>scoredw> <w>the</w> <w>1</w> <c>:</c> <w>1</w>
2
by: dannielum | last post by:
Hi all, I am trying to write a Binary Search Tree that each of its node will have 3 node pointers: left, right and parent. I need a parent pointer for some the purpose of my project. Without the...
4
by: darrel | last post by:
Is there a way in XSLT to write this XSL:Value-of select statement (which I've written in plain english): value of the attribute of the first ascendent to have a value. In otherwords, I have...
2
by: Raed Sawalha | last post by:
I have following XML: How can I find the depth of XML? in other word max level of the XML <Root> ----- level 0 <Name>----- level 1 <FirstName>name</FirstName> ---- level 2...
4
by: patrizio.trinchini | last post by:
Hi all, I'm new to XSLT and maybe my problem have a very trivial answer, but I need an expert that point me in the right direction. What I would obtain is to remove all the elements that have a...
3
by: Valvalis | last post by:
Hello, I am trying to set a class attribute of a text.item element to the value of its nearest ancestor. I want to do this in the case that the class of the text.item is currently a blank string....
4
by: Muthu08 | last post by:
I'm trying to Xpath EXprn for a current node(name unknown).I tried using the below syntax,but it fails .Pls help. <xsl:variable name= "xpath" select ="name(ancestor-or-self::(.))"/>
5
by: gnewsgroup | last post by:
In my user control, I would like to find a Label control in the parent page (the page that uses my user control). I need to update that Label.Text when something happens in the user control. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.