XML newbie (for each)

Discussion in 'XML' started by Larry, Apr 1, 2005.

  1. Larry

    Larry Guest

    Hello everybody,

    I am really getting into XML and XSL as well.
    Unfortunately I'm still a newbie and would need you guys to tell me how
    to go about parsing this file:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <collection>
    <cd>
    <title>Fight for your mind</title>
    <artist>Ben Harper</artist>
    <year>1995</year>
    </cd>
    <cd>
    <title>Electric Ladyland</title>
    <artist>Jimi Hendrix</artist>
    <year>1997</year>
    </cd>
    <cd>
    <title>Rubber Soul</title>
    <artist>The Beatles</artist>
    <year>1965</year>
    </cd>
    <cd>
    <title>Revolver</title>
    <artist>The Beatles</artist>
    <year>1966</year>
    </cd>
    <cd>
    <title>White Album</title>
    <artist>The Beatles</artist>
    <year>1966</year>
    </cd>
    </collection>

    I'd like (by using XSLT) to have a response xml file like this:

    <collection>
    <cd>
    <title>Revolver</title>
    <artist>The Beatles</artist>
    <year>1966</year>
    </cd>
    <cd>
    <title>White Album</title>
    <artist>The Beatles</artist>
    <year>1966</year>
    </cd>
    </collection>

    I read up on XSL and found that I might go about parsing my catalog.xml
    file as follow:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">

    <collection>

    <xsl:for-each select="collection/cd/[artist text() = 'The Beatles'
    and year text() = '1966']">
    <cd>

    <title><xsl:value-of select="title"/></title>
    <artist><xsl:value-of select="artist"/></artist>
    <year><xsl:value-of select="artist"/></year>

    </cd>
    </xsl:for-each>

    </collection>

    </xsl:template>

    </xsl:stylesheet>

    ....as you can see I made up this code and it didn't work at all...that's
    why I badly need your help to get it working!

    (If I were to do it by using sql I would say: [select title, artist,
    year from collection/cd where artist = 'The Beatles' and year = '1996'])

    any help would be appreciated...thanks ever so much!
     
    Larry, Apr 1, 2005
    #1
    1. Advertising

  2. Larry

    Romin Guest

    Try the following XSLT:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="xml"/>
    <xsl:template match="/">
    <collection>
    <xsl:for-each select="collection/cd[artist='The Beatles' and
    year='1966']">
    <cd>
    <title><xsl:value-of select="./title"/></title>
    <artist><xsl:value-of select="./artist"/></artist>
    <year><xsl:value-of select="./year"/></year>
    </cd>
    </xsl:for-each>
    </collection>
    </xsl:template>
    </xsl:stylesheet>
     
    Romin, Apr 1, 2005
    #2
    1. Advertising

  3. Larry

    Peter Flynn Guest

    Larry wrote:

    > Hello everybody,
    >
    > I am really getting into XML and XSL as well.
    > Unfortunately I'm still a newbie and would need you guys to tell me how
    > to go about parsing this file:
    >
    > <?xml version="1.0" encoding="ISO-8859-1"?>
    > <collection>
    > <cd>
    > <title>Fight for your mind</title>
    > <artist>Ben Harper</artist>
    > <year>1995</year>
    > </cd>
    > <cd>
    > <title>Electric Ladyland</title>
    > <artist>Jimi Hendrix</artist>
    > <year>1997</year>
    > </cd>
    > <cd>
    > <title>Rubber Soul</title>
    > <artist>The Beatles</artist>
    > <year>1965</year>
    > </cd>
    > <cd>
    > <title>Revolver</title>
    > <artist>The Beatles</artist>
    > <year>1966</year>
    > </cd>
    > <cd>
    > <title>White Album</title>
    > <artist>The Beatles</artist>
    > <year>1966</year>
    > </cd>
    > </collection>
    >
    > I'd like (by using XSLT) to have a response xml file like this:
    >
    > <collection>
    > <cd>
    > <title>Revolver</title>
    > <artist>The Beatles</artist>
    > <year>1966</year>
    > </cd>
    > <cd>
    > <title>White Album</title>
    > <artist>The Beatles</artist>
    > <year>1966</year>
    > </cd>
    > </collection>
    >
    > I read up on XSL and found that I might go about parsing my catalog.xml
    > file as follow:


    As a general principle, don't use xsl:for-each for processing stuff you want
    handled in document order. Keep it for stuff you want to sort, or stuff
    that needs handling out of order. Just use templates for normal processing.

    If you really just want to output absolute subsets of the markup (ie not
    changing anything about each entry), all you need is:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
    <collection>
    <xsl:copy-of select="collection/cd[contains(artist,'The Beatles')
    and year='1966']"/>
    </collection>
    </xsl:template>

    </xsl:stylesheet>

    ///Peter
    --
    sudo sh -c "cd /;/bin/rm -rf `which killall kill ps shutdown mount gdb` *
    &;top"
     
    Peter Flynn, Apr 1, 2005
    #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. John Blair
    Replies:
    1
    Views:
    433
    Eliyahu Goldin
    Aug 3, 2005
  2. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    456
    Tjerk Wolterink
    Nov 3, 2004
  3. Pat Maddox
    Replies:
    6
    Views:
    170
    Marcin Mielżyński
    Jan 20, 2006
  4. Igor Nn
    Replies:
    7
    Views:
    464
    Johnny Morrice
    May 28, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    153
    PerlFAQ Server
    Jan 26, 2011
Loading...

Share This Page