Re: [xsl] Reading Attributes in XSL

Subject: Re: [xsl] Reading Attributes in XSL
From: pankaj.ahuja@xxxxxxxxxxxx
Date: Tue, 15 Nov 2005 09:40:34 -0700
This will tell you exactly what I am trying to do:

Problem:

XML to break into pages or into tables having 4 records each:


<?xml version="1.0" encoding="UTF-8"?>
<Person race="Asian" Sex="Male">
<Person1 FirstName="Pankaj" LastName="Ahuja">
<CharateristicsCollection>
<Characteristics Profession="Programmer"
jobs="3jobs">
<Detail Address="6680 S" Zip="84121"
Salary="80000"/>
<Detail Address="6681 S" Zip="84121"
Salary="60000"/>
<Detail Address="6682 S" Zip="84121"
Salary="70000"/>
</Characteristics>
<Characteristics Profession="Programmer1"
jobs="2jobs">
<Detail Address="6680 S" Zip="84121"
Salary="80000"/>
<Detail Address="6681 S" Zip="84121"
Salary="60000"/>
</Characteristics>
<Characteristics Profession="Programmer2"
jobs="1jobs">
<Detail Address="6680 S" Zip="84121"
Salary="80000"/>
</Characteristics>
<Characteristics Profession="Programme3r"
jobs="3jobs">
<Detail Address="6680 S" Zip="84121"
Salary="80000"/>
<Detail Address="6681 S" Zip="84121"
Salary="60000"/>
<Detail Address="6682 S" Zip="84121"
Salary="70000"/>
</Characteristics>
<Characteristics Profession="Programmer4"
jobs="3jobs">
<Detail Address="6680 S" Zip="84121"
Salary="80000"/>
<Detail Address="6681 S" Zip="84121"
Salary="60000"/>
<Detail Address="6682 S" Zip="84121"
Salary="70000"/>
</Characteristics>
<Characteristics Profession="Programmer5"
jobs="3jobs">
<Detail Address="6680 S" Zip="84121"
Salary="80000"/>
<Detail Address="6681 S" Zip="84121"
Salary="60000"/>
<Detail Address="6682 S" Zip="84121"
Salary="70000"/>
</Characteristics>
</CharateristicsCollection>
</Person1>
</Person>

Result desired:
1. Want to break this into table using XSL(XPATH) having 4 records
each (Including Attributes of Characteristics as well as Child
elements of Characteristics)
2.I want structure like this:



Asian

Male
Pankaj Ahuja

Programmer--(This one is the 1st attribute of
Characteristics)
6680 S 84121 80000
6681 S 84121 60000
6682 S 84121 70000

(Page Break after this as this shd break after 4 records)

3jobs --(This one is the 2nd attribute of Characteristics)
Programmer1--(This one is the 1st attribute of
Characteristics)
6680 S 84121 80000
6681 S 84121 60000

(Page Break after this as this shd break after 4 records)

2jobs --(This one is the 2nd attribute of Characteristics)
Programmer2
6680 S 84121 80000
1jobs

(Page Break after this as this shd break after 4 records)

and so on with each page

Problem Solving::

Have tried this :
CharateristicsCollection/Characteristics/Detail[position()
mod 4 = 1]
--
--
(. | following-sibling::* |../following-sibling::*/*)[position
()
&lt;= 4]">


but helps only if
XML is like this:

<?xml version="1.0" encoding="UTF-8"?>
<Person race="Asian" Sex="Male">
<Person1 FirstName="Pankaj" LastName="Ahuja">
<CharateristicsCollection>
<Characteristics>
<Detail Address="6680 S" Zip="84121"
Salary="80000"/>
<Detail Address="6681 S" Zip="84121"
Salary="60000"/>
<Detail Address="6682 S" Zip="84121"
Salary="70000"/>
<Detail Address="6680 S" Zip="84121"
Salary="80000"/>
<Detail Address="6681 S" Zip="84121"
Salary="60000"/>
<Detail Address="6680 S" Zip="84121"
Salary="80000"/>
<Detail Address="6680 S" Zip="84121"
Salary="80000"/>
<Detail Address="6681 S" Zip="84121"
Salary="60000"/>
<Detail Address="6682 S" Zip="84121"
Salary="70000"/>
<Detail Address="6680 S" Zip="84121"
Salary="80000"/>
<Detail Address="6681 S" Zip="84121"
Salary="60000"/>
<Detail Address="6682 S" Zip="84121"
Salary="70000"/>
<Detail Address="6680 S" Zip="84121"
Salary="80000"/>
<Detail Address="6681 S" Zip="84121"
Salary="60000"/>
<Detail Address="6682 S" Zip="84121"
Salary="70000"/>
</Characteristics>
</CharateristicsCollection>
</Person1>
</Person>



This works only if there are no multiple Detail collection in
separate groups.
--------------------------------------------------------------
-------------------------------------

I cannot get this to work where it is needed to first read
the first attribute and then all attributes of the child and then
back to reading the second attribute of the parent element. Also how
to keep track of next items.




                                                                                                                                       
                      Jon Gorman                                                                                                       
                      <jonathan.gorman@        To:       xsl-list@xxxxxxxxxxxxxxxxxxxxxx                                               
                      gmail.com>               cc:                                                                                     
                                               Subject:  Re: [xsl] Reading Attributes in XSL                                           
                      11/15/2005 09:34                                                                                                 
                      AM                                                                                                               
                      Please respond to                                                                                                
                      xsl-list                                                                                                         
                                                                                                                                       
                                                                                                                                       




On 11/15/05, pankaj.ahuja@xxxxxxxxxxxx <pankaj.ahuja@xxxxxxxxxxxx> wrote:
> Hi:
>
> I am trying to read attributes of current node such that I read the first
> Attribute and then read all attributes of child elements (as many child
> elements there are) and then back to reading the second attribute of the
> parent node.

Considering attributes are unordered in XML this could be risky.  Most
XML parsers I believe will return them in document order but not
necessarily all.  A better way might be to take the name of the
attribute and match on that.

Also, do you really mean children or descendants?  If just children
you can get away with a for-each loop, otherwise you'll either need
some sort of grouping approach or recursive templates.


Jon Gorman

Current Thread