473,698 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9805
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.xs l"?>
<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:styleshe et 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.xs l"?>
<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:styleshee t version="1.0"
xmlns:xsl="htt p://www.w3.org/1999/XSL/Transform">
<xsl:templat e 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
18832
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 selfhtml reference, too: http://de.selfhtml.org/navigation/css.htm#positionierung In fact firefox and konqueror do position the block relativ to the window border. This is explained here, too: http://www.w3schools.com/dhtml/dhtml_css.asp
9
1956
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
5305
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 pointer to the parent node, the program will be inefficient and slow. It works fine at first. However, when I started to build the remove function, it destroys the tree when I delete a node. I already changed the parent pointer whenever I delete a...
4
2638
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 an element, and I want to get the parent attribute that contains an actual value up the chain. This might be the immediate parent's attribute, the grandparent's attribute or the great grandparent's attribute (max of 3 levels).
2
8442
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 <LasName>lname</LastName> <MiddleName>mName<MiddleName> </Name>
4
2918
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 child element with an attribute set at a given value; maybe an example is more self-explaining... Given the following input XML document:
3
1479
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. <text class="read-only"> ... <text.item class=""/> //this element should inherit the class attribute "read-only" from text <foo class="read-write"> <text.item class="" /> //this element should inherit the class attribute...
4
3456
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
2179
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 don't want to go through the hassle of creating events in the user control, and then let the parent handle the event. What is the easiest way to find a control in the parent page? Right now, I am simply manually traversing it from the user...
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.