xsl-list
[Top] [All Lists]

Re: [xsl] Generically Finding Parent Elements

2008-05-06 11:27:51
Still not sure if the approach is right/optimal.... but if you want
the unique values there is a function called distinct-values() in
XPath 2.0.

Example:
distinct-values(descendant::*[*])

As a suggestion (I am guessing you're just getting started with XSLT
and XPath)  There is a book called XSLT cookbook and it has a chapter
on XPath.  If you read this chapter, I think it will get you started
with thinking about how to construct XPaths.  And as a companion, I
recommend getting Michael Kay's new book.
http://www.amazon.com/XSLT-2-0-XPath-Programmers-Reference/dp/0470192747/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1210098105&sr=8-1
 (I have the previous edition and found it invaluable... and I am
expecting this 4th edition to be arriving today from Amazon.)  A book
that I found useful for learning XSLT is Beginning XSLT by Jeni
Tennison.  (I have the 1st edition Beginning XSLT... I think there is
another edition out now.)

On Tue, May 6, 2008 at 2:04 PM, Tim Dexter 
<timothy(_dot_)dexter(_at_)gmail(_dot_)com> wrote:
OK, so I have working iwht this more

 I now have a nodeset in a variable using

 <xsl:variable name="pval" select="descendant::*[*]">

 to find all the nodes that have children, in the case of my XML I get
 a node-set thus:


 ROOT
  DEPTS
  DEPT
  EMPLOYEES
   EMPLOYEE
   EMPLOYEE
   EMPLOYEE
  DEPT
   EMPLOYEES
    EMPLOYEE
    EMPLOYEE
    EMPLOYEE

 Indenting only for readabilty. Now Im after the unique values in that tree ie


 ROOT
  DEPTS
  DEPT
  EMPLOYEES
   EMPLOYEE

 I guess I want to test the current node with the preceding node name
 but I have got stuck.

 If Im waaay off here please let me know

 Regards

 Tim



 On Tue, May 6, 2008 at 11:14 AM, Tim Dexter 
<timothy(_dot_)dexter(_at_)gmail(_dot_)com> wrote:
 > My apologies for not explaining myself fully - I appreciate your time
 > and effort.
 >
 > We have a product that will have XML data coming in from various other
 > products via a service that will describe the setup of that product
 > e.g. accounts, company hierarchy, etc. The XML will be hierarchical in
 > nature - but we will not know how many levels there might be in that
 > hierarchy. The only fixed point of reference will be the root element
 > ie we will know its name.
 >
 > We want to create a single generic template that can be applied to
 > these XMLs. There are going to be limitations in the layout but I
 > wanted to investigate the feasibility of being able to dynamically
 > loop thru the levels from top to bottom.
 >
 > What I wanted to do was to create a variable that would hold the tree
 > of parent node elements. The XML I posted was just an example of one
 > such XML - I just wanted to get across the hierarchical nature of the
 > XML. I would want to generate a tree as follows.
 >  <ROOT>
 >   <DEPTS>
 >    <DEPT>
 >    <EMPLOYEES>
 >     <EMPLOYEE>
 > I would then be able to read the tree and 'for-each' over each level
 > and render the content in a tabular format. We have developed a
 > template to render the contents of the data at each layer, if present.
 >
 > Another input XML may have more or less levels in the hierarchy and
 > the element names will be different.
 >
 > I have been trying to get this tree using axes only looking for nodes
 > under the root node that have children to create the tree.
 >
 > Hope that is clearer and thanks again for the input
 >
 > Regards
 >
 > tim
 >
 >
 >
 >
 > On Mon, May 5, 2008 at 2:48 PM, Darcy Parker 
<darcyparker(_at_)gmail(_dot_)com> wrote:
 > > 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(_dot_)dexter(_at_)gmail(_dot_)com> 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
 > > >
 > > >  --~------------------------------------------------------------------
 > > >  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
 > > >  To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
 > > >  or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
 > > >  --~--
 > > >
 > > >
 > >
 > > --~------------------------------------------------------------------
 > > XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
 > > To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
 > > or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
 > > --~--
 > >
 > >
 >

 --~------------------------------------------------------------------
 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
 To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
 or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
 --~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--