Newbie XML transformation question using XSL

Discussion in 'XML' started by icedgar, Feb 7, 2005.

  1. icedgar

    icedgar Guest

    I have just started working with XSL to transform XSL to XLM docs. I
    am sure for most of you this is pretty easy and I know I am missing
    something small. Here is a sample of my XML below:

    <CD>
    <key>Track ID</key>
    <integer>37</integer>
    <key>Name</key>
    <string>Elevation</string>
    <key>Artist</key>
    <string>U2</string>
    <key>Composer</key>
    <string>Bono</string>
    </CD>

    I want to trasnform this into:

    <CD
    Track ID=37
    Name=Elevation
    Artist=U2
    Composer=Bono>

    I don't have a problem using XPath and for-each commands to extract the
    data out of the XML. The trouble I am having is taking each <key> and
    assigning it to it's trailing element.

    Also does anyone know how I can format the XML as above with all the
    elements within the CD tag?

    Thanks for help out the newbie.
     
    icedgar, Feb 7, 2005
    #1
    1. Advertising

  2. icedgar

    Joris Gillis Guest

    Tempore 19:17:11, die Monday 07 February 2005 AD, hinc in foro {comp.text.xml} scripsit icedgar <>:

    > <CD>
    > <key>Track ID</key>
    > <integer>37</integer>
    > <key>Name</key>
    > <string>Elevation</string>
    > <key>Artist</key>
    > <string>U2</string>
    > <key>Composer</key>
    > <string>Bono</string>
    > </CD>
    >
    > I want to transform this into:
    >
    > <CD
    > Track ID=37
    > Name=Elevation
    > Artist=U2
    > Composer=Bono>
    >

    Hi,

    the above is not well-formed xml; i'll assume you rather want an output like this:

    <CD Track_ID="37" Name="Elevation" Artist="U2" Composer="Bono"/>

    Such output can be obtained by applying the following stylesheet to the input XML:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:eek:utput indent="yes"/>

    <xsl:template match="CD">
    <xsl:copy>
    <xsl:apply-templates select="key"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="key">
    <xsl:attribute name="{translate(.,' ','_')}">
    <xsl:apply-templates select="following-sibling::*[1]"/>
    </xsl:attribute>
    </xsl:template>

    </xsl:stylesheet>


    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    "In the land of the blind, the one-eyed man is king"
     
    Joris Gillis, Feb 7, 2005
    #2
    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. Hugo Ferreira
    Replies:
    0
    Views:
    392
    Hugo Ferreira
    Jul 14, 2004
  2. CK
    Replies:
    7
    Views:
    541
    Richard Tobin
    Oct 13, 2003
  3. Replies:
    1
    Views:
    3,689
    A. Bolmarcich
    May 27, 2005
  4. Replies:
    0
    Views:
    560
  5. raj
    Replies:
    0
    Views:
    386
Loading...

Share This Page