Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSL help needed for dummy

Reply
Thread Tools

XSL help needed for dummy

 
 
Gina_Marano
Guest
Posts: n/a
 
      05-30-2007
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

 
Reply With Quote
 
 
 
 
Gina_Marano
Guest
Posts: n/a
 
      05-30-2007
Many, many thanks Roy!

On May 30, 4:59 pm, roy axenov <(E-Mail Removed)> wrote:
> 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
> xmlnssl="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



 
Reply With Quote
 
 
 
 
roy axenov
Guest
Posts: n/a
 
      05-30-2007

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
xmlnssl="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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dummy explanation to win32com needed korean_dave Python 2 10-23-2008 07:53 AM
Help! 'dummy.xsl' ? Simon Brooke XML 3 03-20-2007 11:29 PM
Help I'm a Java Dummy, Please Help Gessa_tech Java 1 10-09-2006 11:16 AM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
Dummy needs help with MapPath C not so sharp ASP .Net 4 06-11-2004 04:43 AM



Advertisments