473,396 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

XSL help needed for dummy

Hey All,

I am totally dazed and confused. I am new to XML/XSL and was thrown a
toughy.

Don't shoot me for the xml, it is provided to us and cannot change.
Below is an example of the structure:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="people.xsl"?>
<LIST>
<STATE>
<WA>
<PERSON>
<NAME>Mark Wilson</NAME>
</PERSON>
<PERSON>
<NAME>Tracey Wilson</NAME>
</PERSON>
<PERSON>
<NAME>Jodie Foster</NAME>
</PERSON>
</WA>
<ID>
<PERSON>
<NAME>Lorrin Maughan</NAME>
</PERSON>
<PERSON>
<NAME>Steve Rachel</NAME>
</PERSON>
</ID>
</STATE>
<ADDRESSLIST>
<ADDRESSITEM>
<ADDRESSID>1</ADDRESSID>
<ADDRESS>911 Somewhere Circle, Canberra, Australia</ADDRESS>
</ADDRESSITEM>
<ADDRESSITEM>
<ADDRESSID>2</ADDRESSID>
<ADDRESS>121 Zootle Road, Cape Town, South Africa</ADDRESS>
</ADDRESSITEM>
<ADDRESSITEM>
<ADDRESSID>3</ADDRESSID>
<ADDRESS>30 Animal Road, New York, USA</ADDRESS>
</ADDRESSITEM>
<ADDRESSITEM>
<ADDRESSID>4</ADDRESSID>
<ADDRESS>1143 Winners Lane, London, UK</ADDRESS>
</ADDRESSITEM>
<ADDRESSITEM>
<ADDRESSID>5</ADDRESSID>
<ADDRESS>90210 Beverly Hills, California, USA</ADDRESS>
</ADDRESSITEM>
</ADDRESSLIST>
</LIST>

I need to have it look like this:

<LIST>
<STATE>
<WA>
<PERSON>
<NAME>Mark Wilson</NAME>
<ADDRESS>911 Somewhere Circle, Canberra, Australia</ADDRESS>
</PERSON>
<PERSON>
<NAME>Tracey Wilson</NAME>
<ADDRESS>121 Zootle Road, Cape Town, South Africa</ADDRESS>
</PERSON>
<PERSON>
<NAME>Jodie Foster</NAME>
<ADDRESS>30 Animal Road, New York, USA</ADDRESS>
</PERSON>
</WA>
....

basically associate the address with the person. (note that the list
of items in each state can be a different count)

Any help would be much apprecated!!!!!!!

Frazzled Gina_M

May 30 '07 #1
2 1829

Gina_Marano wrote:
Don't shoot me for the xml, it is provided to us and
cannot change. Below is an example of the structure:
[...]
I need to have it look like this:
[...]
basically associate the address with the person. (note
that the list of items in each state can be a different
count)
This is a fairly typical entry-level problem. Identity with
exclusion is a good starting point for tasks like this.
Slightly over-engineered solution follows:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:key name="address"
match="/LIST/ADDRESSLIST/ADDRESSITEM/ADDRESS"
use="../ADDRESSID"/>

<xsl:template name="calc-num">
<xsl:value-of
select=
"
count
(
.|
preceding::*
[
name()=name(current()) and
namespace-uri()=namespace-uri(current())
]
)
"/>
</xsl:template>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="PERSON">
<xsl:variable name="num">
<xsl:call-template name="calc-num"/>
</xsl:variable>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:apply-templates select="key('address',$num)"/>
</xsl:copy>
</xsl:template>

<xsl:template match="ADDRESSLIST"/>
</xsl:stylesheet>

Key points are aforementioned identity+exclusion (google if
in doubt), calculation of person's position in a document
and injection of the desired sub-tree from elsewhere into
the resulting tree.

--
roy axenov

If only we smelled each other's asses, there wouldn't be any
war.--Dustin Hoffman
May 30 '07 #2
Many, many thanks Roy!

On May 30, 4:59 pm, roy axenov <r_axe...@mail.ruwrote:
Gina_Marano wrote:
Don't shoot me for the xml, it is provided to us and
cannot change. Below is an example of the structure:

[...]
I need to have it look like this:

[...]
basically associate the address with the person. (note
that the list of items in each state can be a different
count)

This is a fairly typical entry-level problem. Identity with
exclusion is a good starting point for tasks like this.
Slightly over-engineered solution follows:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:key name="address"
match="/LIST/ADDRESSLIST/ADDRESSITEM/ADDRESS"
use="../ADDRESSID"/>

<xsl:template name="calc-num">
<xsl:value-of
select=
"
count
(
.|
preceding::*
[
name()=name(current()) and
namespace-uri()=namespace-uri(current())
]
)
"/>
</xsl:template>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="PERSON">
<xsl:variable name="num">
<xsl:call-template name="calc-num"/>
</xsl:variable>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:apply-templates select="key('address',$num)"/>
</xsl:copy>
</xsl:template>

<xsl:template match="ADDRESSLIST"/>
</xsl:stylesheet>

Key points are aforementioned identity+exclusion (google if
in doubt), calculation of person's position in a document
and injection of the desired sub-tree from elsewhere into
the resulting tree.

--
roy axenov

If only we smelled each other's asses, there wouldn't be any
war.--Dustin Hoffman

May 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Huzefa | last post by:
I am working on a amll project in Java that includes many classes. Each of the classes has a Logger object. I have associated a FileHandler with each of these Logger objects. The file is the same...
0
by: Jose Vicente Nunez Z | last post by:
Greetings, I wrote a couple of custom dummy extensions in Python (one a pure Python and the other a C) and i managed to compile and install them without a problem: $ make python2 setup.py...
31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
13
by: Mike | last post by:
Normally scheduling a job is a very elementary operation but for some hidden reason I've been unable to schedule a job which runs on a 28 day cycle, even though I have at least 16 other jobs...
1
by: ZZZ | last post by:
int dummy=Convert.ToInt32(das.Tables.Rows.ItemArray.ToString()); MessageBox.Show(dummy.ToString()); this.combo_opNaam.SelectedValue=dummy; ...
1
by: John Martin | last post by:
I've made an .NET application where the contents of a datagrid (dgr1) is changed when the user changes the selection in a drop-down list (drd1). This has works fine as a beginning. Then I want to...
5
by: Niklas Norrthon | last post by:
I've been banging my head in the wall for some time now over a little problem having to do with partial specialization of function templates. The real problem is more complex than this, but...
3
by: Ole Nielsby | last post by:
I have objects that sometimes overwrite themselves by something else, using placement new. I will spare you for the details, but the objects serve as stack frames and are placed contiguosly, and...
3
by: Simon Brooke | last post by:
As various people will have noticed, I've been having a lot of trouble with XSL lately. Brief history: I wrote myself an XML toolkit back in 2000, and it worked well enough for me, so it's been...
2
by: korean_dave | last post by:
Hi. I need a dummy's explanation to utilizing the win32com component to access Microsoft Excel. So far, I have this code. import win32com.client xl =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.