How to find out the Parent or the Ancestor Node.

E

Eshrath

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.
 
R

Richard Tobin

Eshrath said:
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
 
E

Eshrath Ali Khan

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.
 
M

Morris M. Keesan

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,833
Latest member
BettyeMacf

Latest Threads

Top