XSL help needed for dummy

Discussion in 'XML' started by Gina_Marano, May 30, 2007.

  1. Gina_Marano

    Gina_Marano Guest

    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
     
    Gina_Marano, May 30, 2007
    #1
    1. Advertising

  2. Gina_Marano

    Gina_Marano Guest

    Many, many thanks Roy!

    On May 30, 4:59 pm, roy axenov <> 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
    > 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
     
    Gina_Marano, May 30, 2007
    #2
    1. Advertising

  3. Gina_Marano

    roy axenov Guest

    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
     
    roy axenov, May 31, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C not so sharp

    Dummy needs help with MapPath

    C not so sharp, Jun 9, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    421
    clintonG
    Jun 11, 2004
  2. Replies:
    1
    Views:
    3,656
    A. Bolmarcich
    May 27, 2005
  3. Gessa_tech

    Help I'm a Java Dummy, Please Help

    Gessa_tech, Oct 9, 2006, in forum: Java
    Replies:
    1
    Views:
    401
    Andrew Thompson
    Oct 9, 2006
  4. Simon Brooke

    Help! 'dummy.xsl' ?

    Simon Brooke, Mar 18, 2007, in forum: XML
    Replies:
    3
    Views:
    2,069
    David Carlisle
    Mar 20, 2007
  5. korean_dave

    Dummy explanation to win32com needed

    korean_dave, Oct 22, 2008, in forum: Python
    Replies:
    2
    Views:
    325
    Tim Golden
    Oct 23, 2008
Loading...

Share This Page