472,352 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 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 9744
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...
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...
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...
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...
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...
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...
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"...
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.