[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Re: Re-arranging an XML file


Subject: Re: [xsl] Re: Re-arranging an XML file
From: George Cristian Bina <george@xxxxxxxxxxxxx>
Date: Tue, 20 Jan 2009 10:41:51 +0200

Hi Mike,

I do not understand what is the part you have difficulties with. You can define a key to get the Author elements from their id and then just use that, see below:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" />
<xsl:key name="artistById" match="Artist" use="Number"/>
<xsl:template match="/">
<xsl:apply-templates select="*/AddedAlbums"/>
</xsl:template>
<xsl:template match="AddedAlbums">
<AddedAlbums><xsl:apply-templates/></AddedAlbums>
</xsl:template>
<xsl:template match="Album">
<Album><xsl:apply-templates/></Album>
</xsl:template>
<xsl:template match="AlbumName">
<AlbumName><xsl:value-of select="."/></AlbumName>
</xsl:template>
<xsl:template match="ArtistNumber">
<Name><xsl:value-of select="key('artistById', .)/Name"/></Name>
</xsl:template>
<xsl:template match="*|text()"/>
</xsl:stylesheet>


Best Regards,
George
--
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Mike Stroud wrote:
Dear Group,

Further to my mail of Date: Tue, 13 Jan 2009: I am still struggling
with this one. Here is an adaptation of a real-world example:

<?xml version="1.0" encoding="UTF-8"?>
<wc:COLLECTION xmlns:wc="http://www.ptc.com/infoengine/1.0">
<DeletedArtists NAME="DeletedArtists" TYPE="Unknown" STATUS="0">
</DeletedArtists>
<AddedArtists NAME="AddedArtists" TYPE="Unknown" STATUS="0">
<Artist>
    <LastChangedBy>Mike</LastChangedBy>
    <Number>0000000015</Number>
    <Name>Bob Dylan</Name>
</Artist>
<Artist>
    <LastChangedBy>Mike</LastChangedBy>
    <Number>0000000016</Number>
    <Name>Bonnie Tyler</Name>
</Artist>
</AddedArtists>
<DeletedAlbums NAME="DeletedAlbums" TYPE="Unknown" STATUS="0">
</DeletedAlbums>
<AddedAlbums NAME="AddedAlbums" TYPE="Unknown" STATUS="0">
<Album>
    <ArtistNumber>0000000015</ArtistNumber>
    <AlbumNumber>0000000010</AlbumNumber>
    <AlbumName>Blood on the Tracks</AlbumName>
</Album>
<Album>
    <ArtistNumber>0000000015</ArtistNumber>
    <AlbumNumber>0000000011</AlbumNumber>
    <AlbumName>Empire Burlesque</AlbumName>
</Album>
<Album>
    <ArtistNumber>0000000016</ArtistNumber>
    <AlbumNumber>0000000020</AlbumNumber>
    <AlbumName>Hide Your Heart</AlbumName>
</Album>
</AddedAlbums>
</wc:COLLECTION>

How can I turn that into something like this using XSL?

<AddedAlbums>
  <Album>
    <Name>Bob Dylan</Name>
    <AlbumName>Blood on the Tracks</AlbumName>
  </Album>
  <Album>
    <Name>Bob Dylan</Name>
    <AlbumName>Empire Burlesque</AlbumName>
  </Album>
  <Album>
    <Name>Bonnie Tyler</Name>
    <AlbumName>Hide Your Heart</AlbumName>
  </Album>
</AddedAlbums>

Many thanks,

Mike in SA
ps: Please don't think that I'm a great fan of Bonnie Tyler! I don't
think I could even name one of her songs!
M.


Current Thread