Re: [xsl] Generically Finding Parent Elements

Subject: Re: [xsl] Generically Finding Parent Elements
From: "Darcy Parker" <darcyparker@xxxxxxxxx>
Date: Mon, 5 May 2008 16:48:07 -0400
Not sure I understand the question... (Also I am confused when you say
looping... it sounds like you want to match="//EMPLOYEE" or something
like that, then look up it's parent and perform some templates based
on it...  That could make the performance poor and the code difficult
to read.  Are you familiar with using predicates in XPath? Are you
familiar with xsl:key and key()?  There wasn't enough info in your
question... something doesn't sound right.)

But to select the root node, it depends on the context the variable is
being declared.

Perhaps one of these statements will work?
<xsl:variable name="root" select=".."/>
<xsl:variable name="root" select="parent::ROOT"/>
<xsl:variable name="root" select="ancestor::root"/>
<xsl:variable name="root" select="/"/>

Darcy
On Mon, May 5, 2008 at 4:24 PM, Tim Dexter <timothy.dexter@xxxxxxxxx> wrote:
> Hi All
>
>  Im struggling with a problem. Assume I have the following XML
>
>  <?xml version="1.0"?>
>  <ROOT>
>   <DEPTS>
>   <DEPT>
>    <DEPTNO>10</DEPTNO>
>    <DEPTNAME>Accounting</DEPTNAME>
>    <EMPLOYEES>
>     <EMPLOYEE>
>      <EMPNUM>10001</EMPNUM>
>      <EMPNAME>Jo Bloggs</EMPNAME>
>      <SALARY>100000</SALARY>
>     </EMPLOYEE>
>     <EMPLOYEE>
>      <EMPNUM>10002</EMPNUM>
>      <EMPNAME>Jo Smother</EMPNAME>
>      <SALARY>220000</SALARY>
>     </EMPLOYEE>
>     <EMPLOYEE>
>      <EMPNUM>10003</EMPNUM>
>      <EMPNAME>James Dean</EMPNAME>
>      <SALARY>1000</SALARY>
>     </EMPLOYEE>
>    </EMPLOYEES>
>   </DEPT>
>   <DEPT>
>    <DEPTNO>20</DEPTNO>
>    <DEPTNAME>Shipping</DEPTNAME>
>    <EMPLOYEES>
>     <EMPLOYEE>
>      <EMPNUM>20001</EMPNUM>
>      <EMPNAME>Dave Gibbons</EMPNAME>
>      <SALARY>100000</SALARY>
>     </EMPLOYEE>
>     <EMPLOYEE>
>      <EMPNUM>20002</EMPNUM>
>      <EMPNAME>John Fisher</EMPNAME>
>      <SALARY>220000</SALARY>
>     </EMPLOYEE>
>     <EMPLOYEE>
>      <EMPNUM>20003</EMPNUM>
>      <EMPNAME>Marti Johnson</EMPNAME>
>      <SALARY>1000</SALARY>
>     </EMPLOYEE>
>    </EMPLOYEES>
>   </DEPT>
>   </DEPTS>
>  </ROOT>
>
>  This is just a sample XML the XSL template needs to handle any number
>  of levels in the XML - the only known element is the ROOT name. I need
>  to be able to pull the parent node tree into a variable so I can then
>  loop over them. The looping I can handle, any help on grabbing the
>  parent element names in a tree gratefully received.
>
>  Thanks
>
>  Tim

Current Thread